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.
Completed
Push — master ( 2b80ff...4a8ee3 )
by joseph
21s queued 13s
created

AlwaysValidTrait   A

Complexity

Total Complexity 16

Size/Duplication

Total Lines 120
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
eloc 45
dl 0
loc 120
ccs 0
cts 45
cp 0
rs 10
c 0
b 0
f 0
wmc 16

4 Methods

Rating   Name   Duplication   Size   Complexity  
A create() 0 14 2
A injectEntityDataValidator() 0 4 1
A getValidator() 0 9 2
B update() 0 45 11
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);
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

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

131
        $this->entityDataValidator->setEntity(/** @scrutinizer ignore-type */ $this);
Loading history...
132
    }
133
}
134