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 (#75)
by joseph
14:44
created

ensureMetaDataIsSet()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 2

Importance

Changes 0
Metric Value
eloc 3
dl 0
loc 6
ccs 4
cts 4
cp 1
rs 10
c 0
b 0
f 0
cc 2
nc 2
nop 1
crap 2
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 Doctrine\ORM\EntityManagerInterface;
8
use Doctrine\ORM\Mapping\ClassMetadata;
9
use EdmondsCommerce\DoctrineStaticMeta\Entity\Interfaces\EntityInterface;
10
use EdmondsCommerce\DoctrineStaticMeta\Entity\Interfaces\ValidatedEntityInterface;
11
use EdmondsCommerce\DoctrineStaticMeta\Exception\ValidationException;
12
13
/**
14
 * Trait ImplementNotifyChangeTrackingPolicy
15
 *
16
 * @see     https://www.doctrine-project.org/projects/doctrine-orm/en/2.6/reference/change-tracking-policies.html#notify
17
 *
18
 * @package EdmondsCommerce\DoctrineStaticMeta\Entity\Traits
19
 */
20
trait ImplementNotifyChangeTrackingPolicy
21
{
22
    /**
23
     * @var array PropertyChangedListener[]
24
     */
25
    private $notifyChangeTrackingListeners = [];
26
27
    /**
28
     * @param PropertyChangedListener $listener
29
     */
30 54
    public function addPropertyChangedListener(PropertyChangedListener $listener): void
31
    {
32 54
        $this->notifyChangeTrackingListeners[] = $listener;
33 54
    }
34
35
    /**
36
     * The meta data is set to the entity when the meta data is loaded
37
     *
38
     * This call will load the meta data if it has not already been set, which will in turn set it against the Entity
39
     *
40
     * @param EntityManagerInterface $entityManager
41
     */
42 18
    public function ensureMetaDataIsSet(EntityManagerInterface $entityManager): void
43
    {
44 18
        if (self::$metaData instanceof ClassMetadata) {
45 9
            return;
46
        }
47 9
        $entityManager->getClassMetadata(self::class);
48 9
    }
49
50
    /**
51
     * This notifies the embeddable properties on the owning Entity
52
     *
53
     * @param string      $embeddablePropertyName
54
     * @param null|string $propName
55
     * @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...
56
     * @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...
57
     */
58 4
    public function notifyEmbeddablePrefixedProperties(
59
        string $embeddablePropertyName,
60
        ?string $propName = null,
61
        $oldValue = null,
62
        $newValue = null
63
    ): void {
64 4
        if ($oldValue !== null && $oldValue === $newValue) {
65
            return;
66
        }
67
        /**
68
         * @var ClassMetadata $metaData
69
         */
70 4
        $metaData = static::$metaData;
71 4
        foreach ($metaData->getFieldNames() as $fieldName) {
72
            if (
73 4
                true === \ts\stringStartsWith($fieldName, $embeddablePropertyName)
74 4
                && false !== \ts\stringContains($fieldName, '.')
75
            ) {
76 4
                if ($fieldName !== null && $fieldName !== "$embeddablePropertyName.$propName") {
77 4
                    continue;
78
                }
79 4
                foreach ($this->notifyChangeTrackingListeners as $listener) {
80
                    //wondering if we can get away with not passing in the values?
81 4
                    $listener->propertyChanged($this, $fieldName, $oldValue, $newValue);
82
                }
83
            }
84
        }
85 4
    }
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
     */
100 42
    private function updatePropertyValueThenValidateAndNotify(string $propName, $newValue): void
101
    {
102 42
        if ($this->$propName === $newValue) {
103 5
            return;
104
        }
105 37
        $oldValue        = $this->$propName;
106 37
        $this->$propName = $newValue;
107 37
        if ($this instanceof ValidatedEntityInterface) {
108
            try {
109 37
                $this->validateProperty($propName);
110 2
            } catch (ValidationException $e) {
111 2
                $this->$propName = $oldValue;
112 2
                throw $e;
113
            }
114
        }
115
116 35
        foreach ($this->notifyChangeTrackingListeners as $listener) {
117 6
            $listener->propertyChanged($this, $propName, $oldValue, $newValue);
118
        }
119 35
    }
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
     */
145 1
    private function addToEntityCollectionAndNotify(string $propName, EntityInterface $entity): void
146
    {
147 1
        if ($this->$propName->contains($entity)) {
148
            return;
149
        }
150 1
        $oldValue = $this->$propName;
151 1
        $this->$propName->add($entity);
152 1
        $newValue = $this->$propName;
153 1
        foreach ($this->notifyChangeTrackingListeners as $listener) {
154 1
            $listener->propertyChanged($this, $propName, $oldValue, $newValue);
155
        }
156 1
    }
157
158
    /**
159
     * Called from the Has___Entities Traits
160
     *
161
     * @param string          $propName
162
     * @param EntityInterface $entity
163
     */
164
    private function removeFromEntityCollectionAndNotify(string $propName, EntityInterface $entity): void
165
    {
166
        if (!$this->$propName->contains($entity)) {
167
            return;
168
        }
169
        $oldValue = $this->$propName;
170
        $this->$propName->removeElement($entity);
171
        $newValue = $this->$propName;
172
        foreach ($this->notifyChangeTrackingListeners as $listener) {
173
            $listener->propertyChanged($this, $propName, $oldValue, $newValue);
174
        }
175
    }
176
177
    /**
178
     * Called from the Has___Entity Traits
179
     *
180
     * @param string               $propName
181
     * @param EntityInterface|null $entity
182
     */
183 1
    private function setEntityAndNotify(string $propName, ?EntityInterface $entity): void
184
    {
185 1
        if ($this->$propName === $entity) {
186
            return;
187
        }
188 1
        $oldValue        = $this->$propName;
189 1
        $this->$propName = $entity;
190 1
        foreach ($this->notifyChangeTrackingListeners as $listener) {
191 1
            $listener->propertyChanged($this, $propName, $oldValue, $entity);
192
        }
193 1
    }
194
}
195