edmondscommerce /
doctrine-static-meta
| 1 | <?php declare(strict_types=1); |
||
| 2 | |||
| 3 | namespace EdmondsCommerce\DoctrineStaticMeta\Entity\Factory; |
||
| 4 | |||
| 5 | use Doctrine\Common\NotifyPropertyChanged; |
||
| 6 | use Doctrine\ORM\EntityManagerInterface; |
||
| 7 | use EdmondsCommerce\DoctrineStaticMeta\Entity\Interfaces\EntityInterface; |
||
| 8 | use EdmondsCommerce\DoctrineStaticMeta\Entity\Validation\EntityValidatorFactory; |
||
| 9 | |||
| 10 | class EntityFactory |
||
| 11 | { |
||
| 12 | /** |
||
| 13 | * @var EntityValidatorFactory |
||
| 14 | */ |
||
| 15 | private $entityValidatorFactory; |
||
| 16 | /** |
||
| 17 | * @var EntityManagerInterface |
||
| 18 | */ |
||
| 19 | private $entityManager; |
||
| 20 | |||
| 21 | 19 | public function __construct(EntityValidatorFactory $entityValidatorFactory, EntityManagerInterface $entityManager) |
|
| 22 | { |
||
| 23 | 19 | $this->entityValidatorFactory = $entityValidatorFactory; |
|
| 24 | 19 | $this->entityManager = $entityManager; |
|
| 25 | 19 | } |
|
| 26 | |||
| 27 | /** |
||
| 28 | * Build a new entity with the validator factory preloaded |
||
| 29 | * |
||
| 30 | * Optionally pass in an array of property=>value |
||
| 31 | * |
||
| 32 | * @param string $entityFqn |
||
| 33 | * |
||
| 34 | * @param array $values |
||
| 35 | * |
||
| 36 | * @return mixed |
||
| 37 | */ |
||
| 38 | 18 | public function create(string $entityFqn, array $values = []) |
|
| 39 | { |
||
| 40 | 18 | $entity = $this->createEntity($entityFqn); |
|
| 41 | 18 | $entity->ensureMetaDataIsSet($this->entityManager); |
|
| 42 | 18 | $this->addListenerToEntityIfRequired($entity); |
|
| 43 | 18 | $this->setEntityValues($entity, $values); |
|
| 44 | |||
| 45 | 17 | return $entity; |
|
| 46 | } |
||
| 47 | |||
| 48 | /** |
||
| 49 | * Create the Entity |
||
| 50 | * |
||
| 51 | * @param string $entityFqn |
||
| 52 | * |
||
| 53 | * @return EntityInterface |
||
| 54 | */ |
||
| 55 | 18 | private function createEntity(string $entityFqn): EntityInterface |
|
| 56 | { |
||
| 57 | 18 | return new $entityFqn($this->entityValidatorFactory); |
|
| 58 | } |
||
| 59 | |||
| 60 | /** |
||
| 61 | * Set all the values, if there are any |
||
| 62 | * |
||
| 63 | * @param EntityInterface $entity |
||
| 64 | * @param array $values |
||
| 65 | */ |
||
| 66 | 18 | private function setEntityValues(EntityInterface $entity, array $values): void |
|
| 67 | { |
||
| 68 | 18 | if ([] === $values) { |
|
| 69 | 16 | return; |
|
| 70 | } |
||
| 71 | 2 | foreach ($values as $property => $value) { |
|
| 72 | 2 | $setter = 'set'.$property; |
|
| 73 | 2 | if (!method_exists($entity, $setter)) { |
|
| 74 | 1 | throw new \InvalidArgumentException( |
|
| 75 | 1 | 'The entity '.\get_class($entity).' does not have the setter method '.$setter |
|
| 76 | ); |
||
| 77 | } |
||
| 78 | 1 | $entity->$setter($value); |
|
| 79 | } |
||
| 80 | 1 | } |
|
| 81 | |||
| 82 | /** |
||
| 83 | * Generally DSM Entities are using the Notify change tracking policy. |
||
| 84 | * This ensures that they are fully set up for that |
||
| 85 | * |
||
| 86 | * @param EntityInterface $entity |
||
| 87 | */ |
||
| 88 | 18 | private function addListenerToEntityIfRequired(EntityInterface $entity): void |
|
| 89 | { |
||
| 90 | 18 | if (!$entity instanceof NotifyPropertyChanged) { |
|
|
0 ignored issues
–
show
introduced
by
Loading history...
|
|||
| 91 | return; |
||
| 92 | } |
||
| 93 | 18 | $listener = $this->entityManager->getUnitOfWork(); |
|
| 94 | 18 | $entity->addPropertyChangedListener($listener); |
|
| 95 | 18 | } |
|
| 96 | } |
||
| 97 |