Completed
Push — master ( eae9dd...13bbf2 )
by Nate
06:11
created

ElementMutatorTrait::isElementSet()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 0
cts 4
cp 0
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
crap 2
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
 * @property int|null $elementId
17
 * @property Element|ElementInterface|null $element
18
 *
19
 * @author Flipbox Factory <[email protected]>
20
 * @since 2.0.0
21
 */
22
trait ElementMutatorTrait
23
{
24
    /**
25
     * @var Element|null
26
     */
27
    private $element;
28
29
    /**
30
     * @return bool
31
     */
32
    public function isElementSet(): bool
33
    {
34
        return null !== $this->element;
35
    }
36
37
    /**
38
     * Set associated elementId
39
     *
40
     * @param $id
41
     * @return $this
42
     */
43
    public function setElementId(int $id)
44
    {
45
        $this->elementId = $id;
46
        return $this;
47
    }
48
49
    /**
50
     * Get associated elementId
51
     *
52
     * @return int|null
53
     */
54
    public function getElementId()
55
    {
56
        if (null === $this->elementId && null !== $this->element) {
57
            $this->elementId = $this->element->id;
58
        }
59
60
        return $this->elementId;
61
    }
62
63
    /**
64
     * Associate a element
65
     *
66
     * @param mixed $element
67
     * @return $this
68
     */
69
    public function setElement($element = null)
70
    {
71
        $this->element = null;
72
73
        if (!$element = $this->internalResolveElement($element)) {
74
            $this->element = $this->elementId = null;
75
        } else {
76
            /** @var Element $element */
77
            $this->elementId = $element->id;
78
            $this->element = $element;
79
        }
80
81
        return $this;
82
    }
83
84
    /**
85
     * @return ElementInterface|null
86
     */
87
    public function getElement()
88
    {
89
        /** @var Element $element */
90
        if ($this->element === null) {
91
            $element = $this->resolveElement();
92
            $this->setElement($element);
93
            return $element;
94
        }
95
96
        $elementId = $this->elementId;
97
        if ($elementId !== null &&
98
            $elementId !== $this->element->id
99
        ) {
100
            $this->element = null;
101
            return $this->getElement();
102
        }
103
104
        return $this->element;
105
    }
106
107
    /**
108
     * @return ElementInterface|null
109
     */
110
    protected function resolveElement()
111
    {
112
        if ($model = $this->resolveElementFromId()) {
113
            return $model;
114
        }
115
116
        return null;
117
    }
118
119
    /**
120
     * @return ElementInterface|null
121
     */
122
    private function resolveElementFromId()
123
    {
124
        if (null === $this->elementId) {
125
            return null;
126
        }
127
128
        return Craft::$app->getElements()->getElementById($this->elementId);
129
    }
130
131
    /**
132
     * @param mixed $element
133
     * @return ElementInterface|Element|null
134
     */
135
    protected function internalResolveElement($element = null)
136
    {
137
        if ($element instanceof ElementInterface) {
138
            return $element;
139
        }
140
141
        if (is_numeric($element)) {
142
            return Craft::$app->getElements()->getElementById($element);
143
        }
144
145
        if (is_string($element)) {
146
            return Craft::$app->getElements()->getElementByUri($element);
147
        }
148
149
        return Craft::$app->getElements()->createElement($element);
150
    }
151
}
152