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

SourceAttributeTrait::getSource()   A

Complexity

Conditions 4
Paths 3

Size

Total Lines 16

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 20

Importance

Changes 0
Metric Value
dl 0
loc 16
ccs 0
cts 14
cp 0
rs 9.7333
c 0
b 0
f 0
cc 4
nc 3
nop 0
crap 20
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 'sourceId' attribute and a 'source' 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 SourceAttributeTrait
27
{
28
    use ActiveRecordTrait;
29
30
    /**
31
     * @var Element|ElementInterface|null
32
     */
33
    private $source;
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 sourceRules(): array
51
    {
52
        return [
53
            [
54
                [
55
                    'sourceId'
56
                ],
57
                'number',
58
                'integerOnly' => true
59
            ],
60
            [
61
                [
62
                    'sourceId',
63
                    'source'
64
                ],
65
                'safe',
66
                'on' => [
67
                    Model::SCENARIO_DEFAULT
68
                ]
69
            ]
70
        ];
71
    }
72
73
    /**
74
     * @inheritdoc
75
     */
76
    public function sourceAttributeLabels(): array
77
    {
78
        return [
79
            'sourceId' => Craft::t('app', 'Source Id')
80
        ];
81
    }
82
83
    /**
84
     * @param int|null $id
85
     * @return $this
86
     */
87
    public function setSourceId(int $id = null)
88
    {
89
        $this->setAttribute('sourceId', $id);
90
91
        if (null !== $this->source && $id !== $this->source->getId()) {
92
            $this->source = null;
93
        }
94
        return $this;
95
    }
96
97
    /**
98
     * Get associated sourceId
99
     *
100
     * @return int|null
101
     */
102
    public function getSourceId()
103
    {
104
        if (null === $this->getAttribute('sourceId') && null !== $this->source) {
105
            $this->setSourceId($this->source->getId());
106
        }
107
108
        return $this->getAttribute('sourceId');
109
    }
110
111
    /**
112
     * @param mixed $source
113
     * @return $this
114
     */
115
    public function setSource($source = null)
116
    {
117
        $this->source = null;
118
        $this->setAttribute('sourceId', null);
119
120
        if ($source = $this->resolveElement($source)) {
121
            $this->source = $source;
122
            $this->setAttribute('sourceId', $source->getId());
123
        }
124
125
        return $this;
126
    }
127
128
    /**
129
     * @return Element|ElementInterface|null
130
     */
131
    public function getSource()
132
    {
133
        if ($this->source === null) {
134
            $source = $this->resolveElementFromIdAttribute('sourceId');
135
            $this->setSource($source);
136
            return $source;
137
        }
138
139
        $sourceId = $this->getAttribute('sourceId');
140
        if ($sourceId !== null && $sourceId !== $this->source->getId()) {
141
            $this->source = null;
142
            return $this->getSource();
143
        }
144
145
        return $this->source;
146
    }
147
}
148