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