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\CodeGeneration\NamespaceHelper; |
8
|
|
|
use EdmondsCommerce\DoctrineStaticMeta\Entity\Interfaces\EntityInterface; |
9
|
|
|
use EdmondsCommerce\DoctrineStaticMeta\Entity\Validation\EntityValidatorFactory; |
10
|
|
|
use EdmondsCommerce\DoctrineStaticMeta\EntityManager\Mapping\GenericFactoryInterface; |
11
|
|
|
|
12
|
|
|
class EntityFactory implements GenericFactoryInterface |
13
|
|
|
{ |
14
|
|
|
/** |
15
|
|
|
* @var NamespaceHelper |
16
|
|
|
*/ |
17
|
|
|
protected $namespaceHelper; |
18
|
|
|
/** |
19
|
|
|
* @var EntityValidatorFactory |
20
|
|
|
*/ |
21
|
|
|
private $entityValidatorFactory; |
22
|
|
|
/** |
23
|
|
|
* @var EntityManagerInterface |
24
|
|
|
*/ |
25
|
|
|
private $entityManager; |
26
|
|
|
|
27
|
|
|
public function __construct(EntityValidatorFactory $entityValidatorFactory, NamespaceHelper $namespaceHelper) |
28
|
|
|
{ |
29
|
|
|
$this->entityValidatorFactory = $entityValidatorFactory; |
30
|
|
|
$this->namespaceHelper = $namespaceHelper; |
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
public function setEntityManager(EntityManagerInterface $entityManager): void |
34
|
|
|
{ |
35
|
|
|
$this->entityManager = $entityManager; |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* Get an instance of the specific Entity Factory for a specified Entity |
40
|
|
|
* |
41
|
|
|
* Not type hinting the return because the whole point of this is to have an entity specific method, which we |
42
|
|
|
* can't hint for |
43
|
|
|
* |
44
|
|
|
* @param string $entityFqn |
45
|
|
|
* |
46
|
|
|
* @return mixed |
47
|
|
|
*/ |
48
|
1 |
|
public function createFactoryForEntity(string $entityFqn) |
49
|
|
|
{ |
50
|
1 |
|
$this->assertEntityManagerSet(); |
51
|
1 |
|
$factoryFqn = $this->namespaceHelper->getFactoryFqnFromEntityFqn($entityFqn); |
52
|
|
|
|
53
|
1 |
|
return new $factoryFqn($this, $this->entityManager); |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
private function assertEntityManagerSet() |
57
|
|
|
{ |
58
|
|
|
if (!$this->entityManager instanceof EntityManagerInterface) { |
|
|
|
|
59
|
|
|
throw new \RuntimeException( |
60
|
|
|
'No EntityManager set, this must be set first using setEntityManager()' |
61
|
|
|
); |
62
|
|
|
} |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
public function getEntity(string $className) |
66
|
|
|
{ |
67
|
|
|
return $this->create($className); |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* Build a new entity with the validator factory preloaded |
72
|
|
|
* |
73
|
|
|
* Optionally pass in an array of property=>value |
74
|
|
|
* |
75
|
|
|
* @param string $entityFqn |
76
|
|
|
* |
77
|
|
|
* @param array $values |
78
|
|
|
* |
79
|
|
|
* @return mixed |
80
|
|
|
*/ |
81
|
3 |
|
public function create(string $entityFqn, array $values = []) |
82
|
|
|
{ |
83
|
3 |
|
$this->assertEntityManagerSet(); |
84
|
3 |
|
$entity = $this->createEntity($entityFqn); |
85
|
3 |
|
$this->initialiseEntity($entity, $values); |
86
|
|
|
|
87
|
2 |
|
return $entity; |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* Create the Entity |
92
|
|
|
* |
93
|
|
|
* @param string $entityFqn |
94
|
|
|
* |
95
|
|
|
* @return EntityInterface |
96
|
|
|
*/ |
97
|
|
|
private function createEntity(string $entityFqn): EntityInterface |
98
|
|
|
{ |
99
|
|
|
return new $entityFqn($this->entityValidatorFactory); |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
/** |
103
|
|
|
* Take an already instantiated Entity and perform the final initialisation steps |
104
|
|
|
* |
105
|
|
|
* @param EntityInterface $entity |
106
|
|
|
* @param array $values |
107
|
|
|
*/ |
108
|
|
|
public function initialiseEntity(EntityInterface $entity, array $values = []): void |
109
|
|
|
{ |
110
|
|
|
$entity->ensureMetaDataIsSet($this->entityManager); |
111
|
|
|
$this->addListenerToEntityIfRequired($entity); |
112
|
|
|
$this->setEntityValues($entity, $values); |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
/** |
116
|
|
|
* Generally DSM Entities are using the Notify change tracking policy. |
117
|
|
|
* This ensures that they are fully set up for that |
118
|
|
|
* |
119
|
|
|
* @param EntityInterface $entity |
120
|
|
|
*/ |
121
|
|
|
private function addListenerToEntityIfRequired(EntityInterface $entity): void |
122
|
|
|
{ |
123
|
|
|
if (!$entity instanceof NotifyPropertyChanged) { |
|
|
|
|
124
|
|
|
return; |
125
|
|
|
} |
126
|
|
|
$listener = $this->entityManager->getUnitOfWork(); |
127
|
|
|
$entity->addPropertyChangedListener($listener); |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
/** |
131
|
|
|
* Set all the values, if there are any |
132
|
|
|
* |
133
|
|
|
* @param EntityInterface $entity |
134
|
|
|
* @param array $values |
135
|
|
|
*/ |
136
|
|
|
private function setEntityValues(EntityInterface $entity, array $values): void |
137
|
|
|
{ |
138
|
|
|
if ([] === $values) { |
139
|
|
|
return; |
140
|
|
|
} |
141
|
|
|
foreach ($values as $property => $value) { |
142
|
|
|
$setter = 'set' . $property; |
143
|
|
|
if (!method_exists($entity, $setter)) { |
144
|
|
|
throw new \InvalidArgumentException( |
145
|
|
|
'The entity ' . \get_class($entity) . ' does not have the setter method ' . $setter |
146
|
|
|
. "\n\nmethods: " . \print_r(get_class_methods($entity), true) |
147
|
|
|
); |
148
|
|
|
} |
149
|
|
|
$entity->$setter($value); |
150
|
|
|
} |
151
|
|
|
} |
152
|
|
|
} |
153
|
|
|
|