OwnerMutator::getOwnerId()   A
last analyzed

Complexity

Conditions 3
Paths 2

Size

Total Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

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