1
|
|
|
<?php declare(strict_types=1); |
2
|
|
|
|
3
|
|
|
namespace EdmondsCommerce\DoctrineStaticMeta\Entity\Traits; |
4
|
|
|
|
5
|
|
|
use EdmondsCommerce\DoctrineStaticMeta\DoctrineStaticMeta; |
6
|
|
|
use EdmondsCommerce\DoctrineStaticMeta\Entity\Factory\EntityFactoryInterface; |
7
|
|
|
use EdmondsCommerce\DoctrineStaticMeta\Entity\Interfaces\DataTransferObjectInterface; |
8
|
|
|
use EdmondsCommerce\DoctrineStaticMeta\Entity\Interfaces\Validation\EntityDataValidatorInterface; |
9
|
|
|
use EdmondsCommerce\DoctrineStaticMeta\Exception\ValidationException; |
10
|
|
|
use Ramsey\Uuid\UuidInterface; |
11
|
|
|
|
12
|
|
|
trait AlwaysValidTrait |
13
|
|
|
{ |
14
|
|
|
/** |
15
|
|
|
* @var EntityDataValidatorInterface |
16
|
|
|
*/ |
17
|
|
|
private $entityDataValidator; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* This is a special property that is manipulated via Reflection in the Entity factory. |
21
|
|
|
* |
22
|
|
|
* Whilst a transaction is running, validation is suspended, and then at the end of a transaction the full |
23
|
|
|
* validation is performed |
24
|
|
|
* |
25
|
|
|
* @var bool |
26
|
|
|
*/ |
27
|
|
|
private $creationTransactionRunning = false; |
28
|
|
|
|
29
|
|
|
final public static function create( |
30
|
|
|
EntityFactoryInterface $factory, |
31
|
|
|
DataTransferObjectInterface $dto = null |
32
|
|
|
): self { |
33
|
|
|
$entity = new static(); |
34
|
|
|
$factory->initialiseEntity($entity); |
|
|
|
|
35
|
|
|
if (null !== $dto) { |
36
|
|
|
$entity->update($dto); |
37
|
|
|
|
38
|
|
|
return $entity; |
39
|
|
|
} |
40
|
|
|
$entity->getValidator()->validate(); |
41
|
|
|
|
42
|
|
|
return $entity; |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* Update and validate the Entity. |
47
|
|
|
* |
48
|
|
|
* The DTO can |
49
|
|
|
* - contain data not related to this Entity, it will be ignored |
50
|
|
|
* - not have to have all the data for this Entity, it will only update where the DTO has the setter |
51
|
|
|
* |
52
|
|
|
* The entity state after update will be validated |
53
|
|
|
* |
54
|
|
|
* Will roll back all updates if validation fails |
55
|
|
|
* |
56
|
|
|
* @param DataTransferObjectInterface $dto |
57
|
|
|
* |
58
|
|
|
* @throws ValidationException |
59
|
|
|
* @SuppressWarnings(PHPMD.CyclomaticComplexity) |
60
|
|
|
*/ |
61
|
|
|
final public function update(DataTransferObjectInterface $dto): void |
62
|
|
|
{ |
63
|
|
|
$backup = []; |
64
|
|
|
$setters = self::getDoctrineStaticMeta()->getSetters(); |
65
|
|
|
try { |
66
|
|
|
foreach ($setters as $getterName => $setterName) { |
67
|
|
|
if (false === method_exists($dto, $getterName)) { |
68
|
|
|
continue; |
69
|
|
|
} |
70
|
|
|
$dtoValue = $dto->$getterName(); |
71
|
|
|
if ($dtoValue instanceof UuidInterface && (string)$dtoValue === (string)$this->$getterName()) { |
72
|
|
|
continue; |
73
|
|
|
} |
74
|
|
|
if (false === $this->creationTransactionRunning) { |
75
|
|
|
$gotValue = null; |
|
|
|
|
76
|
|
|
try { |
77
|
|
|
$gotValue = $this->$getterName(); |
78
|
|
|
} catch (\TypeError $e) { |
79
|
|
|
//Required items will type error on the getter as they have no value |
80
|
|
|
} |
81
|
|
|
if ($dtoValue === $gotValue) { |
82
|
|
|
continue; |
83
|
|
|
} |
84
|
|
|
$backup[$setterName] = $gotValue; |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
$this->$setterName($dtoValue); |
88
|
|
|
} |
89
|
|
|
if (true === $this->creationTransactionRunning) { |
90
|
|
|
return; |
91
|
|
|
} |
92
|
|
|
$this->getValidator()->validate(); |
93
|
|
|
} catch (ValidationException | \TypeError $e) { |
94
|
|
|
$reflectionClass = $this::getDoctrineStaticMeta()->getReflectionClass(); |
95
|
|
|
foreach ($backup as $setterName => $backupValue) { |
96
|
|
|
/** |
97
|
|
|
* We have to use reflection here because required property setter will not accept nulls |
98
|
|
|
* which may be the backup value, especially on new object creation |
99
|
|
|
*/ |
100
|
|
|
$propertyName = $this::getDoctrineStaticMeta()->getPropertyNameFromSetterName($setterName); |
101
|
|
|
$reflectionProperty = $reflectionClass->getProperty($propertyName); |
102
|
|
|
$reflectionProperty->setAccessible(true); |
103
|
|
|
$reflectionProperty->setValue($this, $backupValue); |
104
|
|
|
} |
105
|
|
|
throw $e; |
106
|
|
|
} |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
private function getValidator(): EntityDataValidatorInterface |
110
|
|
|
{ |
111
|
|
|
if (!$this->entityDataValidator instanceof EntityDataValidatorInterface) { |
|
|
|
|
112
|
|
|
throw new \RuntimeException( |
113
|
|
|
'You must call injectDataValidator before being able to update an Entity' |
114
|
|
|
); |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
return $this->entityDataValidator; |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
abstract public static function getDoctrineStaticMeta(): DoctrineStaticMeta; |
121
|
|
|
|
122
|
|
|
/** |
123
|
|
|
* This method is called automatically by the EntityFactory when initialisig the Entity, by way of the |
124
|
|
|
* EntityDependencyInjector |
125
|
|
|
* |
126
|
|
|
* @param EntityDataValidatorInterface $entityDataValidator |
127
|
|
|
*/ |
128
|
|
|
public function injectEntityDataValidator(EntityDataValidatorInterface $entityDataValidator) |
129
|
|
|
{ |
130
|
|
|
$this->entityDataValidator = $entityDataValidator; |
131
|
|
|
$this->entityDataValidator->setEntity($this); |
|
|
|
|
132
|
|
|
} |
133
|
|
|
} |
134
|
|
|
|