Completed
Push — develop ( 914847...b47c28 )
by Nate
02:30
created

TargetAttributeTrait::getTargetId()   A

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-element-lists/LICENSE
6
 * @link       https://github.com/flipboxfactory/craft-element-lists/
7
 */
8
9
namespace flipbox\craft\element\lists\records;
10
11
use Craft;
12
use craft\base\Element;
13
use craft\base\ElementInterface;
14
use flipbox\craft\ember\records\ActiveRecordTrait;
15
use yii\base\Model;
16
17
/**
18
 * This trait assists w/ managing a 'targetId' attribute and a 'target' object, keeping them
19
 * in sync if one of the other is altered.  In addition, depending on the operation process, a
20
 * newly created object could be set and saved.  Subsequent calls to retrieve the Id would return
21
 * the newly created object Id.
22
 *
23
 * @author Flipbox Factory <[email protected]>
24
 * @since 3.0.0
25
 */
26
trait TargetAttributeTrait
27
{
28
    use ActiveRecordTrait;
29
30
    /**
31
     * @var Element|ElementInterface|null
32
     */
33
    private $target;
34
35
    /**
36
     * @param string $attribute
37
     * @return Element|ElementInterface|null
38
     */
39
    abstract protected function resolveElementFromIdAttribute(string $attribute);
40
41
    /**
42
     * @param mixed $element
43
     * @return Element|ElementInterface|null
44
     */
45
    abstract protected function resolveElement($element = null);
46
47
    /**
48
     * @return array
49
     */
50
    protected function targetRules(): array
51
    {
52
        return [
53
            [
54
                [
55
                    'targetId'
56
                ],
57
                'number',
58
                'integerOnly' => true
59
            ],
60
            [
61
                [
62
                    'targetId',
63
                    'target'
64
                ],
65
                'safe',
66
                'on' => [
67
                    Model::SCENARIO_DEFAULT
68
                ]
69
            ]
70
        ];
71
    }
72
73
    /**
74
     * @inheritdoc
75
     */
76
    public function targetAttributeLabels(): array
77
    {
78
        return [
79
            'targetId' => Craft::t('app', 'Target Id')
80
        ];
81
    }
82
83
    /**
84
     * @param int|null $id
85
     * @return $this
86
     */
87
    public function setTargetId(int $id = null)
88
    {
89
        $this->setAttribute('targetId', $id);
90
91
        if (null !== $this->target && $id !== $this->target->getId()) {
92
            $this->target = null;
93
        }
94
        return $this;
95
    }
96
97
    /**
98
     * Get associated targetId
99
     *
100
     * @return int|null
101
     */
102
    public function getTargetId()
103
    {
104
        if (null === $this->getAttribute('targetId') && null !== $this->target) {
105
            $this->setTargetId($this->target->getId());
106
        }
107
108
        return $this->getAttribute('targetId');
109
    }
110
111
    /**
112
     * @param mixed $target
113
     * @return $this
114
     */
115
    public function setTarget($target = null)
116
    {
117
        $this->target = null;
118
        $this->setAttribute('targetId', null);
119
120
        if ($target = $this->resolveElement($target)) {
121
            $this->target = $target;
122
            $this->setAttribute('targetId', $target->getId());
123
        }
124
125
        return $this;
126
    }
127
128
    /**
129
     * @return Element|ElementInterface|null
130
     */
131
    public function getTarget()
132
    {
133
        if ($this->target === null) {
134
            $target = $this->resolveElementFromIdAttribute('targetId');
135
            $this->setTarget($target);
136
            return $target;
137
        }
138
139
        $targetId = $this->getAttribute('targetId');
140
        if ($targetId !== null && $targetId !== $this->target->getId()) {
141
            $this->target = null;
142
            return $this->getTarget();
143
        }
144
145
        return $this->target;
146
    }
147
}
148