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 ( a48bd8...83d092 )
by joseph
16:18 queued 18s
created

AlwaysValidTrait   A

Complexity

Total Complexity 16

Size/Duplication

Total Lines 110
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
eloc 41
dl 0
loc 110
ccs 0
cts 63
cp 0
rs 10
c 0
b 0
f 0
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 37 11
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);
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

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

120
        $this->entityDataValidator->setEntity(/** @scrutinizer ignore-type */ $this);
Loading history...
121
    }
122
}
123