Completed
Push — master ( 11971b...eeafb3 )
by Nate
01:16
created

ElementMutatorTrait::resolveElementFromId()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

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