Completed
Push — master ( 3f9c00...9927a4 )
by Nate
04:25 queued 03:23
created

ElementMutatorTrait::verifyElement()   A

Complexity

Conditions 5
Paths 5

Size

Total Lines 20

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 30

Importance

Changes 0
Metric Value
dl 0
loc 20
ccs 0
cts 16
cp 0
rs 9.2888
c 0
b 0
f 0
cc 5
nc 5
nop 1
crap 30
1
<?php
2
3
/**
4
 * @copyright  Copyright (c) Flipbox Digital Limited
5
 * @license    https://github.com/flipboxfactory/craft-ember/blob/master/LICENSE
6
 * @link       https://github.com/flipboxfactory/craft-ember/
7
 */
8
9
namespace flipbox\craft\ember\objects;
10
11
use Craft;
12
use craft\base\Element;
13
use craft\base\ElementInterface;
14
15
/**
16
 * This trait accepts both an ElementInterface or and Element Id and ensures that the both
17
 * the ElementInterface and the Id are in sync. If one changes (and does not match the other) it
18
 * resolves (removes / updates) the other.
19
 *
20
 * In addition, this trait is primarily useful when a new Element is set and saved; the Element
21
 * Id can be retrieved without needing to explicitly set the newly created Id.
22
 *
23
 * @property Element|ElementInterface|null $element
24
 *
25
 * @author Flipbox Factory <[email protected]>
26
 * @since 2.0.0
27
 */
28
trait ElementMutatorTrait
29
{
30
    /**
31
     * @var Element|null
32
     */
33
    private $element;
34
35
    /**
36
     * Internally set the Element Id.  This can be overridden. A record for example
37
     * should use `setAttribute`.
38
     *
39
     * @param int|null $id
40
     * @return $this
41
     */
42
    abstract protected function internalSetElementId(int $id = null);
43
44
    /**
45
     * Internally get the Element Id.  This can be overridden.  A record for example
46
     * should use `getAttribute`.
47
     *
48
     * @return int|null
49
     */
50
    abstract protected function internalGetElementId();
51
52
    /**
53
     * @return bool
54
     */
55
    public function isElementSet(): bool
56
    {
57
        return null !== $this->element;
58
    }
59
60
    /**
61
     * Set associated elementId
62
     *
63
     * @param $id
64
     * @return $this
65
     */
66
    public function setElementId(int $id = null)
67
    {
68
        $this->internalSetElementId($id);
69
70
        if (null !== $this->element && $id !== $this->element->id) {
71
            $this->element = null;
72
        }
73
74
        return $this;
75
    }
76
77
    /**
78
     * Get associated elementId
79
     *
80
     * @return int|null
81
     */
82
    public function getElementId()
83
    {
84
        if (null === $this->internalGetElementId() && null !== $this->element) {
85
            $this->setElementId($this->element->id);
86
        }
87
88
        return $this->internalGetElementId();
89
    }
90
91
    /**
92
     * Associate a element
93
     *
94
     * @param mixed $element
95
     * @return $this
96
     */
97
    public function setElement($element = null)
98
    {
99
        $this->element = null;
100
        $this->internalSetElementId(null);
101
102
        if (null !== ($element = $this->verifyElement($element))) {
103
            /** @var Element element */
104
            $this->element = $element;
0 ignored issues
show
Documentation Bug introduced by
It seems like $element of type object<craft\base\ElementInterface> is incompatible with the declared type object<craft\base\Element>|null of property $element.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
105
            $this->internalSetElementId($element->id);
106
        }
107
108
        return $this;
109
    }
110
111
    /**
112
     * @return ElementInterface|null
113
     */
114
    public function getElement()
115
    {
116
        if ($this->element === null) {
117
            $element = $this->resolveElement();
118
            $this->setElement($element);
119
            return $element;
120
        }
121
122
        $elementId = $this->internalGetElementId();
123
        if ($elementId !== null && $elementId !== $this->element->id) {
124
            $this->element = null;
125
            return $this->getElement();
126
        }
127
128
        return $this->element;
129
    }
130
131
    /**
132
     * @return ElementInterface|null
133
     */
134
    protected function resolveElement()
135
    {
136
        if ($element = $this->resolveElementFromId()) {
137
            return $element;
138
        }
139
140
        return null;
141
    }
142
143
    /**
144
     * @return ElementInterface|null
145
     */
146
    private function resolveElementFromId()
147
    {
148
        if (null === ($elementId = $this->internalGetElementId())) {
149
            return null;
150
        }
151
152
        return Craft::$app->getElements()->getElementById($elementId);
153
    }
154
155
    /**
156
     * @param mixed $element
157
     * @return ElementInterface|Element|null
158
     */
159
    protected function verifyElement($element = null)
160
    {
161
        if (null === $element) {
162
            return null;
163
        }
164
165
        if ($element instanceof ElementInterface) {
166
            return $element;
167
        }
168
169
        if (is_numeric($element)) {
170
            return Craft::$app->getElements()->getElementById($element);
171
        }
172
173
        if (is_string($element)) {
174
            return Craft::$app->getElements()->getElementByUri($element);
175
        }
176
177
        return null;
178
    }
179
}
180