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