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