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 |
|
$factoryFqn = $this->namespaceHelper->getFactoryFqnFromEntityFqn($entityFqn); |
51
|
|
|
|
52
|
1 |
|
return new $factoryFqn($this); |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
public function getEntity(string $className) |
56
|
|
|
{ |
57
|
|
|
return $this->create($className); |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* Build a new entity with the validator factory preloaded |
62
|
|
|
* |
63
|
|
|
* Optionally pass in an array of property=>value |
64
|
|
|
* |
65
|
|
|
* @param string $entityFqn |
66
|
|
|
* |
67
|
|
|
* @param array $values |
68
|
|
|
* |
69
|
|
|
* @return mixed |
70
|
|
|
*/ |
71
|
3 |
|
public function create(string $entityFqn, array $values = []) |
72
|
|
|
{ |
73
|
3 |
|
if (!$this->entityManager instanceof EntityManagerInterface) { |
|
|
|
|
74
|
|
|
throw new \RuntimeException( |
75
|
|
|
'No EntityManager set, this must be set first using setEntityManager()' |
76
|
|
|
); |
77
|
|
|
} |
78
|
3 |
|
$entity = $this->createEntity($entityFqn); |
79
|
3 |
|
$entity->ensureMetaDataIsSet($this->entityManager); |
80
|
3 |
|
$this->addListenerToEntityIfRequired($entity); |
81
|
3 |
|
$this->setEntityValues($entity, $values); |
82
|
|
|
|
83
|
2 |
|
return $entity; |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* Create the Entity |
88
|
|
|
* |
89
|
|
|
* @param string $entityFqn |
90
|
|
|
* |
91
|
|
|
* @return EntityInterface |
92
|
|
|
*/ |
93
|
|
|
private function createEntity(string $entityFqn): EntityInterface |
94
|
|
|
{ |
95
|
|
|
return new $entityFqn($this->entityValidatorFactory); |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* Generally DSM Entities are using the Notify change tracking policy. |
100
|
|
|
* This ensures that they are fully set up for that |
101
|
|
|
* |
102
|
|
|
* @param EntityInterface $entity |
103
|
|
|
*/ |
104
|
|
|
private function addListenerToEntityIfRequired(EntityInterface $entity): void |
105
|
|
|
{ |
106
|
|
|
if (!$entity instanceof NotifyPropertyChanged) { |
|
|
|
|
107
|
|
|
return; |
108
|
|
|
} |
109
|
|
|
$listener = $this->entityManager->getUnitOfWork(); |
110
|
|
|
$entity->addPropertyChangedListener($listener); |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
/** |
114
|
|
|
* Set all the values, if there are any |
115
|
|
|
* |
116
|
|
|
* @param EntityInterface $entity |
117
|
|
|
* @param array $values |
118
|
|
|
*/ |
119
|
|
|
private function setEntityValues(EntityInterface $entity, array $values): void |
120
|
|
|
{ |
121
|
|
|
if ([] === $values) { |
122
|
|
|
return; |
123
|
|
|
} |
124
|
|
|
foreach ($values as $property => $value) { |
125
|
|
|
$setter = 'set' . $property; |
126
|
|
|
if (!method_exists($entity, $setter)) { |
127
|
|
|
throw new \InvalidArgumentException( |
128
|
|
|
'The entity ' . \get_class($entity) . ' does not have the setter method ' . $setter |
129
|
|
|
. "\n\nmethods: " . \print_r(get_class_methods($entity), true) |
130
|
|
|
); |
131
|
|
|
} |
132
|
|
|
$entity->$setter($value); |
133
|
|
|
} |
134
|
|
|
} |
135
|
|
|
} |
136
|
|
|
|