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 (#96)
by Ross
15:04 queued 12:23
created

setEntityCollectionAndNotify()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 9
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 0
Metric Value
eloc 6
dl 0
loc 9
ccs 0
cts 6
cp 0
rs 10
c 0
b 0
f 0
cc 3
nc 3
nop 2
crap 12
1
<?php declare(strict_types=1);
2
3
namespace EdmondsCommerce\DoctrineStaticMeta\Entity\Traits;
4
5
use Doctrine\Common\Collections\ArrayCollection;
6
use Doctrine\Common\Collections\Collection;
7
use Doctrine\Common\PropertyChangedListener;
8
use Doctrine\ORM\EntityManagerInterface;
9
use Doctrine\ORM\Mapping\ClassMetadata;
10
use EdmondsCommerce\DoctrineStaticMeta\Entity\Interfaces\EntityInterface;
11
use EdmondsCommerce\DoctrineStaticMeta\Entity\Interfaces\ValidatedEntityInterface;
12
use EdmondsCommerce\DoctrineStaticMeta\Exception\ValidationException;
13
14
/**
15
 * Trait ImplementNotifyChangeTrackingPolicy
16
 *
17
 * @see     https://www.doctrine-project.org/projects/doctrine-orm/en/2.6/reference/change-tracking-policies.html#notify
18
 *
19
 * @package EdmondsCommerce\DoctrineStaticMeta\Entity\Traits
20
 */
21
trait ImplementNotifyChangeTrackingPolicy
22
{
23
    /**
24
     * @var array PropertyChangedListener[]
25
     */
26
    private $notifyChangeTrackingListeners = [];
27
28
    /**
29
     * @param PropertyChangedListener $listener
30 55
     */
31
    public function addPropertyChangedListener(PropertyChangedListener $listener): void
32 55
    {
33 55
        $this->notifyChangeTrackingListeners[] = $listener;
34
    }
35
36
    /**
37
     * The meta data is set to the entity when the meta data is loaded
38
     *
39
     * This call will load the meta data if it has not already been set, which will in turn set it against the Entity
40
     *
41
     * @param EntityManagerInterface $entityManager
42 18
     */
43
    public function ensureMetaDataIsSet(EntityManagerInterface $entityManager): void
44 18
    {
45 9
        if (self::$metaData instanceof ClassMetadata) {
46
            return;
47 9
        }
48 9
        self::$metaData = $entityManager->getClassMetadata(self::class);
0 ignored issues
show
Bug Best Practice introduced by
The property metaData does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
49
    }
50
51
    /**
52
     * This notifies the embeddable properties on the owning Entity
53
     *
54
     * @param string      $embeddablePropertyName
55
     * @param null|string $propName
56
     * @param null        $oldValue
0 ignored issues
show
Documentation Bug introduced by
Are you sure the doc-type for parameter $oldValue is correct as it would always require null to be passed?
Loading history...
57
     * @param null        $newValue
0 ignored issues
show
Documentation Bug introduced by
Are you sure the doc-type for parameter $newValue is correct as it would always require null to be passed?
Loading history...
58 4
     */
59
    public function notifyEmbeddablePrefixedProperties(
60
        string $embeddablePropertyName,
61
        ?string $propName = null,
62
        $oldValue = null,
63
        $newValue = null
64 4
    ): void {
65
        if ($oldValue !== null && $oldValue === $newValue) {
66
            return;
67
        }
68
        /**
69
         * @var ClassMetadata $metaData
70 4
         */
71 4
        $metaData = static::$metaData;
72 4
        foreach ($metaData->getFieldNames() as $fieldName) {
73 4
            if (true === \ts\stringStartsWith($fieldName, $embeddablePropertyName)
74
                && false !== \ts\stringContains($fieldName, '.')
75 4
            ) {
76 4
                if ($fieldName !== null && $fieldName !== "$embeddablePropertyName.$propName") {
77
                    continue;
78 4
                }
79
                foreach ($this->notifyChangeTrackingListeners as $listener) {
80 4
                    //wondering if we can get away with not passing in the values?
81
                    $listener->propertyChanged($this, $fieldName, $oldValue, $newValue);
82
                }
83
            }
84 4
        }
85
    }
86
87
88
    /**
89
     * To be called from all set methods
90
     *
91
     * This method updates the property value, then it runs this through validation
92
     * If validation fails, it sets the old value back and throws the caught exception
93
     * If validation passes, it then performs the Doctrine notification for property change
94
     *
95
     * @param string $propName
96
     * @param mixed  $newValue
97
     *
98
     * @throws ValidationException
99 44
     */
100
    private function updatePropertyValueThenValidateAndNotify(string $propName, $newValue): void
101 44
    {
102 3
        if ($this->$propName === $newValue) {
103
            return;
104 41
        }
105 41
        $oldValue        = $this->$propName;
106 41
        $this->$propName = $newValue;
107
        if ($this instanceof ValidatedEntityInterface) {
108 41
            try {
109 2
                $this->validateProperty($propName);
110 2
            } catch (ValidationException $e) {
111 2
                $this->$propName = $oldValue;
112
                throw $e;
113
            }
114
        }
115 39
116 6
        foreach ($this->notifyChangeTrackingListeners as $listener) {
117
            $listener->propertyChanged($this, $propName, $oldValue, $newValue);
118 39
        }
119
    }
120
121
    /**
122
     * Called from the Has___Entities Traits
123
     *
124
     * @param string                       $propName
125
     * @param Collection|EntityInterface[] $entities
126
     */
127
    private function setEntityCollectionAndNotify(string $propName, Collection $entities): void
128
    {
129
        if ($this->$propName === $entities) {
130
            return;
131
        }
132
        $oldValue        = $this->$propName;
133
        $this->$propName = $entities;
134
        foreach ($this->notifyChangeTrackingListeners as $listener) {
135
            $listener->propertyChanged($this, $propName, $oldValue, $entities);
136
        }
137
    }
138
139
    /**
140
     * Called from the Has___Entities Traits
141
     *
142
     * @param string          $propName
143
     * @param EntityInterface $entity
144 1
     */
145
    private function addToEntityCollectionAndNotify(string $propName, EntityInterface $entity): void
146 1
    {
147
        if ($this->$propName === null) {
148
            $this->$propName = new ArrayCollection();
149 1
        }
150 1
        if ($this->$propName->contains($entity)) {
151 1
            return;
152 1
        }
153 1
        $oldValue = $this->$propName;
154
        $this->$propName->add($entity);
155 1
        $newValue = $this->$propName;
156
        foreach ($this->notifyChangeTrackingListeners as $listener) {
157
            $listener->propertyChanged($this, $propName, $oldValue, $newValue);
158
        }
159
    }
160
161
    /**
162
     * Called from the Has___Entities Traits
163
     *
164
     * @param string          $propName
165
     * @param EntityInterface $entity
166
     */
167
    private function removeFromEntityCollectionAndNotify(string $propName, EntityInterface $entity): void
168
    {
169
        if ($this->$propName === null) {
170
            $this->$propName = new ArrayCollection();
171
        }
172
        if (!$this->$propName->contains($entity)) {
173
            return;
174
        }
175
        $oldValue = $this->$propName;
176
        $this->$propName->removeElement($entity);
177
        $newValue = $this->$propName;
178
        foreach ($this->notifyChangeTrackingListeners as $listener) {
179
            $listener->propertyChanged($this, $propName, $oldValue, $newValue);
180
        }
181
    }
182 1
183
    /**
184 1
     * Called from the Has___Entity Traits
185
     *
186
     * @param string               $propName
187 1
     * @param EntityInterface|null $entity
188 1
     */
189 1
    private function setEntityAndNotify(string $propName, ?EntityInterface $entity): void
190 1
    {
191
        if ($this->$propName === $entity) {
192 1
            return;
193
        }
194
        $oldValue        = $this->$propName;
195
        $this->$propName = $entity;
196
        foreach ($this->notifyChangeTrackingListeners as $listener) {
197
            $listener->propertyChanged($this, $propName, $oldValue, $entity);
198
        }
199
    }
200
}
201