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.

updatePropertyValue()   A
last analyzed

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
cc 3
eloc 6
nc 3
nop 2
dl 0
loc 9
ccs 0
cts 7
cp 0
crap 12
rs 10
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace EdmondsCommerce\DoctrineStaticMeta\Entity\Traits;
6
7
use Doctrine\Common\Collections\ArrayCollection;
8
use Doctrine\Common\PropertyChangedListener;
9
use Doctrine\ORM\EntityManagerInterface;
10
use Doctrine\ORM\Mapping\ClassMetadata;
11
use Doctrine\ORM\PersistentCollection;
12
use EdmondsCommerce\DoctrineStaticMeta\Entity\Interfaces\EntityInterface;
13
use EdmondsCommerce\DoctrineStaticMeta\Exception\ValidationException;
14
15
/**
16
 * Trait ImplementNotifyChangeTrackingPolicy
17
 *
18
 * @see https://www.doctrine-project.org/projects/doctrine-orm/en/2.6/reference/change-tracking-policies.html#notify
19
 */
20
trait ImplementNotifyChangeTrackingPolicy
21
{
22
23
    /**
24
     * @var array PropertyChangedListener[]
25
     */
26
    private $notifyChangeTrackingListeners = [];
27
28
    /**
29
     * Set a notify change tracking listener (Unit of Work basically). Use the spl_object_hash to protect against
30
     * registering the same UOW more than once
31
     *
32
     * @param PropertyChangedListener $listener
33
     */
34
    public function addPropertyChangedListener(PropertyChangedListener $listener): void
35
    {
36
        $this->notifyChangeTrackingListeners[spl_object_hash($listener)] = $listener;
37
    }
38
39
    /**
40
     * If we want to totally disable the notify change, for example in bulk operations
41
     */
42
    public function removePropertyChangedListeners(): void
43
    {
44
        $this->notifyChangeTrackingListeners = [];
45
    }
46
47
    /**
48
     * The meta data is set to the entity when the meta data is loaded, however if metadata is cached that wont happen
49
     * This call ensures that the meta data is set
50
     *
51
     * @param EntityManagerInterface $entityManager
52
     *
53
     */
54
    public function ensureMetaDataIsSet(EntityManagerInterface $entityManager): void
55
    {
56
        self::getDoctrineStaticMeta()->setMetaData($entityManager->getClassMetadata(self::class));
57
    }
58
59
    /**
60
     * This notifies the embeddable properties on the owning Entity
61
     *
62
     * @param string      $embeddablePropertyName
63
     * @param null|string $propName
64
     * @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...
65
     * @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...
66
     */
67
    public function notifyEmbeddablePrefixedProperties(
68
        string $embeddablePropertyName,
69
        ?string $propName = null,
70
        $oldValue = null,
71
        $newValue = null
72
    ): void {
73
        if ($oldValue !== null && $oldValue === $newValue) {
0 ignored issues
show
introduced by
The condition $oldValue !== null is always false.
Loading history...
74
            return;
75
        }
76
        /**
77
         * @var ClassMetadata $metaData
78
         */
79
        $metaData = self::getDoctrineStaticMeta()->getMetaData();
80
        foreach ($metaData->getFieldNames() as $fieldName) {
81
            if (
82
                true === \ts\stringStartsWith($fieldName, $embeddablePropertyName)
83
                && false !== \ts\stringContains($fieldName, '.')
84
            ) {
85
                if ($fieldName !== null && $fieldName !== "$embeddablePropertyName.$propName") {
86
                    continue;
87
                }
88
                foreach ($this->notifyChangeTrackingListeners as $listener) {
89
                    //wondering if we can get away with not passing in the values?
90
                    $listener->propertyChanged($this, $fieldName, $oldValue, $newValue);
91
                }
92
            }
93
        }
94
    }
95
96
97
    /**
98
     * To be called from all set methods
99
     *
100
     * This method updates the property value, then it runs this through validation
101
     * If validation fails, it sets the old value back and throws the caught exception
102
     * If validation passes, it then performs the Doctrine notification for property change
103
     *
104
     * @param string $propName
105
     * @param mixed  $newValue
106
     *
107
     */
108
    private function updatePropertyValue(string $propName, $newValue): void
109
    {
110
        if ($this->$propName === $newValue) {
111
            return;
112
        }
113
        $oldValue        = $this->$propName;
114
        $this->$propName = $newValue;
115
        foreach ($this->notifyChangeTrackingListeners as $listener) {
116
            $listener->propertyChanged($this, $propName, $oldValue, $newValue);
117
        }
118
    }
119
120
    /**
121
     * Called from the Has___Entities Traits
122
     *
123
     * @param string          $propName
124
     * @param EntityInterface $entity
125
     */
126
    private function removeFromEntityCollectionAndNotify(string $propName, EntityInterface $entity): void
127
    {
128
        if ($this->$propName === null) {
129
            $this->$propName = new ArrayCollection();
130
        }
131
        if ($this->$propName instanceof PersistentCollection) {
132
            $this->$propName->initialize();
133
        }
134
        if (!$this->$propName->contains($entity)) {
135
            return;
136
        }
137
        $oldValue = $this->$propName;
138
        $this->$propName->removeElement($entity);
139
        $newValue = $this->$propName;
140
        foreach ($this->notifyChangeTrackingListeners as $listener) {
141
            $listener->propertyChanged($this, $propName, $oldValue, $newValue);
142
        }
143
    }
144
145
    /**
146
     * Called from the Has___Entities Traits
147
     *
148
     * @param string          $propName
149
     * @param EntityInterface $entity
150
     */
151
    private function addToEntityCollectionAndNotify(string $propName, EntityInterface $entity): void
152
    {
153
        if ($this->$propName === null) {
154
            $this->$propName = new ArrayCollection();
155
        }
156
        if ($this->$propName->contains($entity)) {
157
            return;
158
        }
159
        $oldValue = $this->$propName;
160
        $this->$propName->add($entity);
161
        $newValue = $this->$propName;
162
        foreach ($this->notifyChangeTrackingListeners as $listener) {
163
            $listener->propertyChanged($this, $propName, $oldValue, $newValue);
164
        }
165
    }
166
167
    /**
168
     * Called from the Has___Entity Traits
169
     *
170
     * @param string               $propName
171
     * @param EntityInterface|null $entity
172
     */
173
    private function setEntityAndNotify(string $propName, ?EntityInterface $entity): void
174
    {
175
        if ($this->$propName === $entity) {
176
            return;
177
        }
178
        $oldValue        = $this->$propName;
179
        $this->$propName = $entity;
180
        foreach ($this->notifyChangeTrackingListeners as $listener) {
181
            $listener->propertyChanged($this, $propName, $oldValue, $entity);
182
        }
183
    }
184
}
185