Last active
February 18, 2024 20:42
-
-
Save zavodnoyapl1992/ce70a643d38cf3d7bcf8a8d366946afc to your computer and use it in GitHub Desktop.
Before/After
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
class FactoryA implements FactoryInterface { | |
public function __construct( | |
private readonly ARepository $aRepository, | |
private readonly ExtractorFactory $extractorFactory | |
) { } | |
public static function getType(): ObjectType { | |
return ObjectType::A; | |
} | |
/** | |
* @throws ExtractorException | |
*/ | |
public function createByParameters(array $parameters): ObjectA { | |
$extractor = $this->extractorFactory->createByArray($parameters); | |
$id = $extractor->getIntByKey('id'); | |
$data = $this->aRepository->findSomeParamsById($id); | |
if (count($data) === 0) { | |
$data = $extractor->getArrayByKey('default'); | |
} | |
return new ObjectA($data); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
interface FactoryInterface { | |
public function createByParameters(array $parameters): ObjectInterface; | |
public static function getType(): ObjectType; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
interface ObjectInterface { } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
class SomeRegistry { | |
/** @var array<int, FactoryInterface> */ | |
private readonly array $factories; | |
/** | |
* @param FactoryInterface[] $factories | |
*/ | |
public function __construct(array $factories) { | |
$mappedFactory = []; | |
foreach ($factories as $factory) { | |
if (array_key_exists($factory::getType()->value, $mappedFactory)) { | |
throw new RuntimeException('Duplicate message'); | |
} | |
$mappedFactory[$factory::getType()->value] = $factory; | |
} | |
$this->factories = $mappedFactory; | |
} | |
public function createByParams(ObjectType $type, array $parameters): ObjectInterface { | |
$factory = $this->factories[$type->value] ?? null; | |
if ($factory === null) { | |
throw new RuntimeException('Not found exception'); | |
} | |
return $factory->createByParameters($parameters); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
class SomeFactory { | |
public function __construct( | |
private readonly ARepository $aRepository, | |
) { } | |
/** | |
* @throws ErrorException | |
*/ | |
public function createByParameters(ObjectType $type, array $parameters): ObjectE|ObjectD|ObjectA|ObjectB|ObjectC { | |
switch ($type) { | |
case ObjectType::A: | |
if (!array_key_exists('id', $parameters) || !is_int($parameters['id'])) { | |
throw new ErrorException('Some message'); | |
} | |
$aEntity = $this->aRepository->findById($parameters['id']); | |
$data = []; | |
if ($aEntity !== null) { | |
$data = $aEntity->getSomeParams(); | |
} | |
if (count($data) === 0) { | |
if (array_key_exists('default', $parameters) && is_array($parameters['default'])) { | |
$data = $parameters['default']; | |
} else { | |
throw new ErrorException('Some message'); | |
} | |
} | |
return new ObjectA($data); | |
case ObjectType::B: | |
// some code | |
return new ObjectB($parameters); | |
case ObjectType::C: | |
// some code | |
return new ObjectC($parameters); | |
case ObjectType::D: | |
// some code | |
return new ObjectD($parameters); | |
case ObjectType::E: | |
// some code | |
return new ObjectE($parameters); | |
} | |
throw new RuntimeException('some message'); | |
} | |
} | |
// вызов | |
$someFactory->createByParameters(ObjectType::A, $parameters); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment