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
Pull Request — master (#69)
by joseph
15:46
created

addToEntityCollectionAndNotify()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 10
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 3.0175

Importance

Changes 0
Metric Value
eloc 7
dl 0
loc 10
rs 10
c 0
b 0
f 0
ccs 7
cts 8
cp 0.875
cc 3
nc 3
nop 2
crap 3.0175
1
<?php declare(strict_types=1);
2
3
namespace EdmondsCommerce\DoctrineStaticMeta\Entity\Traits;
4
5
use Doctrine\Common\Collections\Collection;
6
use Doctrine\Common\PropertyChangedListener;
7
use EdmondsCommerce\DoctrineStaticMeta\Entity\Interfaces\EntityInterface;
8
use EdmondsCommerce\DoctrineStaticMeta\Entity\Interfaces\ValidatedEntityInterface;
9
use EdmondsCommerce\DoctrineStaticMeta\Exception\ValidationException;
10
11
/**
12
 * Trait ImplementNotifyChangeTrackingPolicy
13
 *
14
 * @see     https://www.doctrine-project.org/projects/doctrine-orm/en/2.6/reference/change-tracking-policies.html#notify
15
 *
16
 * @package EdmondsCommerce\DoctrineStaticMeta\Entity\Traits
17
 */
18
trait ImplementNotifyChangeTrackingPolicy
19
{
20
    /**
21
     * @var array PropertyChangedListener[]
22
     */
23
    private $notifyChangeTrackingListeners = [];
24
25
    /**
26
     * @param PropertyChangedListener $listener
27
     */
28 43
    public function addPropertyChangedListener(PropertyChangedListener $listener): void
29
    {
30 43
        $this->notifyChangeTrackingListeners[] = $listener;
31 43
    }
32
33
    /**
34
     * To be called from all set methods
35
     *
36
     * This method updates the property value, then it runs this through validation
37
     * If validation fails, it sets the old value back and throws the caught exception
38
     * If validation passes, it then performs the Doctrine notification for property change
39
     *
40
     * @param string $propName
41
     * @param        $newValue
42
     *
43
     * @throws ValidationException
44
     */
45 42
    private function updatePropertyValueThenValidateAndNotify(string $propName, $newValue): void
46
    {
47 42
        if ($this->$propName === $newValue) {
48 2
            return;
49
        }
50 40
        $oldValue        = $this->$propName;
51 40
        $this->$propName = $newValue;
52 40
        if ($this instanceof ValidatedEntityInterface) {
53
            try {
54 40
                $this->validateProperty($propName);
55 2
            } catch (ValidationException $e) {
56 2
                $this->$propName = $oldValue;
57 2
                throw $e;
58
            }
59
        }
60 38
        foreach ($this->notifyChangeTrackingListeners as $listener) {
61 2
            $listener->propertyChanged($this, $propName, $oldValue, $newValue);
62
        }
63 38
    }
64
65
    /**
66
     * Called from the Has___Entities Traits
67
     *
68
     * @param string                       $propName
69
     * @param Collection|EntityInterface[] $entities
70
     */
71
    private function setEntityCollectionAndNotify(string $propName, Collection $entities): void
72
    {
73
        if ($this->$propName === $entities) {
74
            return;
75
        }
76
        $oldValue        = $this->$propName;
77
        $this->$propName = $entities;
78
        foreach ($this->notifyChangeTrackingListeners as $listener) {
79
            $listener->propertyChanged($this, $propName, $oldValue, $entities);
80
        }
81
    }
82
83
    /**
84
     * Called from the Has___Entities Traits
85
     *
86
     * @param string          $propName
87
     * @param EntityInterface $entity
88
     */
89 1
    private function addToEntityCollectionAndNotify(string $propName, EntityInterface $entity): void
90
    {
91 1
        if ($this->$propName->contains($entity)) {
92
            return;
93
        }
94 1
        $oldValue = $this->$propName;
95 1
        $this->$propName->add($entity);
96 1
        $newValue = $this->$propName;
97 1
        foreach ($this->notifyChangeTrackingListeners as $listener) {
98 1
            $listener->propertyChanged($this, $propName, $oldValue, $newValue);
99
        }
100 1
    }
101
102
    /**
103
     * Called from the Has___Entities Traits
104
     *
105
     * @param string          $propName
106
     * @param EntityInterface $entity
107
     */
108
    private function removeFromEntityCollectionAndNotify(string $propName, EntityInterface $entity): void
109
    {
110
        if (!$this->$propName->contains($entity)) {
111
            return;
112
        }
113
        $oldValue = $this->$propName;
114
        $this->$propName->removeElement($entity);
115
        $newValue = $this->$propName;
116
        foreach ($this->notifyChangeTrackingListeners as $listener) {
117
            $listener->propertyChanged($this, $propName, $oldValue, $newValue);
118
        }
119
    }
120
121
    /**
122
     * Called from the Has___Entity Traits
123
     *
124
     * @param string               $propName
125
     * @param EntityInterface|null $entity
126
     */
127 1
    private function setEntityAndNotify(string $propName, ?EntityInterface $entity): void
128
    {
129 1
        if ($this->$propName === $entity) {
130
            return;
131
        }
132 1
        $oldValue        = $this->$propName;
133 1
        $this->$propName = $entity;
134 1
        foreach ($this->notifyChangeTrackingListeners as $listener) {
135 1
            $listener->propertyChanged($this, $propName, $oldValue, $entity);
136
        }
137 1
    }
138
}
139