ElementAttributeTrait   A
last analyzed

Complexity

Total Complexity 11

Size/Duplication

Total Lines 89
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 5

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 11
lcom 1
cbo 5
dl 0
loc 89
ccs 0
cts 48
cp 0
rs 10
c 0
b 0
f 0

7 Methods

Rating   Name   Duplication   Size   Complexity  
A elementAttributes() 0 6 1
A elementAttributeLabels() 0 6 1
A internalSetElementId() 0 5 1
A internalGetElementId() 0 7 2
A resolveElement() 0 8 2
A resolveElementFromRelation() 0 14 3
A getElementRecord() 0 7 1
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\records;
10
11
use Craft;
12
use craft\base\ElementInterface;
13
use craft\records\Element as ElementRecord;
14
use flipbox\craft\ember\models\ElementRulesTrait;
15
use flipbox\craft\ember\objects\ElementMutatorTrait;
16
use yii\db\ActiveQueryInterface;
17
18
/**
19
 * Intended to be used on an ActiveRecord, this class provides `$this->elementId` attribute along with 'getters'
20
 * and 'setters' to ensure continuity between the Id and Object.  An element object is lazy loaded when called.
21
 * In addition, ActiveRecord rules are available.
22
 *
23
 * @author Flipbox Factory <[email protected]>
24
 * @since 2.0.0
25
 *
26
 * @property ElementRecord $elementRecord
27
 */
28
trait ElementAttributeTrait
29
{
30
    use ActiveRecordTrait,
31
        ElementRulesTrait,
32
        ElementMutatorTrait;
33
34
    /**
35
     * @inheritdoc
36
     */
37
    public function elementAttributes(): array
38
    {
39
        return [
40
            'elementId'
41
        ];
42
    }
43
44
    /**
45
     * @inheritdoc
46
     */
47
    public function elementAttributeLabels(): array
48
    {
49
        return [
50
            'elementId' => Craft::t('app', 'Element Id')
51
        ];
52
    }
53
54
    /**
55
     * @inheritDoc
56
     */
57
    protected function internalSetElementId(int $id = null)
58
    {
59
        $this->setAttribute('elementId', $id);
60
        return $this;
61
    }
62
63
    /**
64
     * @inheritDoc
65
     */
66
    protected function internalGetElementId()
67
    {
68
        if (null === ($id = $this->getAttribute('elementId'))) {
69
            return null;
70
        }
71
        return (int)$id;
72
    }
73
74
    /**
75
     * @return ElementInterface|null
76
     */
77
    protected function resolveElement()
78
    {
79
        if ($model = $this->resolveElementFromRelation()) {
80
            return $model;
81
        }
82
83
        return $this->resolveElementFromId();
84
    }
85
86
    /**
87
     * @return ElementInterface|null
88
     */
89
    private function resolveElementFromRelation()
90
    {
91
        if (false === $this->isRelationPopulated('elementRecord')) {
92
            return null;
93
        }
94
95
        if (null === ($record = $this->getRelation('elementRecord'))) {
96
            return null;
97
        }
98
99
        /** @var ElementRecord $record */
100
101
        return Craft::$app->getElements()->getElementById($record->id);
102
    }
103
104
    /**
105
     * Get the associated Element
106
     *
107
     * @return ActiveQueryInterface
108
     */
109
    public function getElementRecord(): ActiveQueryInterface
110
    {
111
        return $this->hasOne(
112
            ElementRecord::class,
113
            ['id' => 'elementId']
114
        );
115
    }
116
}
117