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