GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.

AlwaysValidTrait   A
last analyzed

Complexity

Total Complexity 16

Size/Duplication

Total Lines 121
Duplicated Lines 0 %

Test Coverage

Coverage 42.22%

Importance

Changes 4
Bugs 0 Features 0
Metric Value
eloc 45
c 4
b 0
f 0
dl 0
loc 121
rs 10
ccs 19
cts 45
cp 0.4222
wmc 16

4 Methods

Rating   Name   Duplication   Size   Complexity  
A injectEntityDataValidator() 0 4 1
A create() 0 14 2
A getValidator() 0 9 2
B update() 0 45 11
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
$entity of type EdmondsCommerce\Doctrine...Traits\AlwaysValidTrait is incompatible with the type EdmondsCommerce\Doctrine...erfaces\EntityInterface expected by parameter $entity of EdmondsCommerce\Doctrine...ace::initialiseEntity(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

38
        $factory->initialiseEntity(/** @scrutinizer ignore-type */ $entity);
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
Unused Code introduced by
The assignment to $gotValue is dead and can be removed.
Loading history...
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
introduced by
$this->entityDataValidator is always a sub-type of EdmondsCommerce\Doctrine...yDataValidatorInterface.
Loading history...
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
Bug introduced by
$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 ignore-type  annotation

136
        $this->entityDataValidator->setEntity(/** @scrutinizer ignore-type */ $this);
Loading history...
137 4
    }
138
}
139