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.
Passed
Pull Request — master (#154)
by joseph
25:46
created

ensureMetaDataIsSet()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

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