Completed
Push — master ( 0a7f4f...30aa45 )
by Nate
08:01 queued 34s
created

resolveSiteFromRelation()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 14

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 0
Metric Value
dl 0
loc 14
ccs 0
cts 10
cp 0
rs 9.7998
c 0
b 0
f 0
cc 3
nc 3
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\models\Site as SiteModel;
13
use craft\records\Site as SiteRecord;
14
use craft\validators\SiteIdValidator;
15
use flipbox\craft\ember\helpers\ModelHelper;
16
use flipbox\craft\ember\helpers\SiteHelper;
17
use flipbox\craft\ember\records\ActiveRecordTrait;
18
use yii\db\ActiveQueryInterface;
19
20
/**
21
 * @author Flipbox Factory <[email protected]>
22
 * @since 2.0.0
23
 *
24
 * @property int|null $sourceSiteId
25
 *
26
 * @method SiteModel parentResolveSite()
27
 */
28
trait SourceSiteAttributeTrait
29
{
30
    use ActiveRecordTrait;
31
32
    /**
33
     * @var SiteModel|null
34
     */
35
    private $site;
36
37
    /**
38
     * @return array
39
     */
40
    protected function sourceSiteRules(): array
41
    {
42
        return [
43
            [
44
                [
45
                    'sourceSiteId'
46
                ],
47
                'number',
48
                'integerOnly' => true
49
            ],
50
            [
51
                [
52
                    'sourceSiteId'
53
                ],
54
                SiteIdValidator::class
55
            ],
56
            [
57
                [
58
                    'sourceSiteId',
59
                    'sourceSite',
60
                    'siteId',
61
                    'site'
62
                ],
63
                'safe',
64
                'on' => [
65
                    ModelHelper::SCENARIO_DEFAULT
66
                ]
67
            ]
68
        ];
69
    }
70
71
    /**
72
     * @inheritdoc
73
     */
74
    public function sourceSiteAttributes(): array
75
    {
76
        return [
77
            'sourceSiteId'
78
        ];
79
    }
80
81
    /**
82
     * @inheritdoc
83
     */
84
    public function sourceSiteAttributeLabels(): array
85
    {
86
        return [
87
            'sourceSiteId' => Craft::t('app', 'Site Id')
88
        ];
89
    }
90
91
    /**
92
     * @param int $id
93
     * @return SourceSiteAttributeTrait
0 ignored issues
show
Comprehensibility Bug introduced by
The return type SourceSiteAttributeTrait is a trait, and thus cannot be used for type-hinting in PHP. Maybe consider adding an interface and use that for type-hinting?

In PHP traits cannot be used for type-hinting as they do not define a well-defined structure. This is because any class that uses a trait can rename that trait’s methods.

If you would like to return an object that has a guaranteed set of methods, you could create a companion interface that lists these methods explicitly.

Loading history...
94
     */
95
    public function setSiteId(int $id)
96
    {
97
        return $this->setSourceSiteId($id);
98
    }
99
100
    /**
101
     * @return int|null
102
     */
103
    public function getSiteId()
104
    {
105
        return $this->getSourceSiteId();
106
    }
107
108
    /**
109
     * Associate a site
110
     *
111
     * @param mixed $site
112
     * @return $this
113
     */
114
    public function setSite($site = null)
115
    {
116
        return $this->setSourceSite($site);
117
    }
118
119
    /**
120
     * @return SiteModel|null
121
     */
122
    public function getSite()
123
    {
124
        return $this->getSourceSite();
125
    }
126
127
    /**
128
     * Set associated sourceSiteId
129
     *
130
     * @param $id
131
     * @return $this
132
     */
133
    public function setSourceSiteId(int $id)
134
    {
135
        $this->sourceSiteId = $id;
136
        return $this;
137
    }
138
139
    /**
140
     * Get associated sourceSiteId
141
     *
142
     * @return int|null
143
     */
144
    public function getSourceSiteId()
145
    {
146
        $siteId = $this->getAttribute('sourceSiteId');
147
        if (null === $siteId && null !== $this->site) {
148
            $siteId = $this->sourceSiteId = $this->site->id;
149
        }
150
151
        return $siteId;
152
    }
153
154
    /**
155
     * Associate a site
156
     *
157
     * @param mixed $site
158
     * @return $this
159
     */
160
    public function setSourceSite($site = null)
161
    {
162
        $this->site = null;
163
164
        if (($site = SiteHelper::resolve($site)) === null) {
165
            $this->site = $this->sourceSiteId = null;
166
        } else {
167
            $this->sourceSiteId = $site->id;
168
            $this->site = $site;
169
        }
170
171
        return $this;
172
    }
173
174
    /**
175
     * @return SiteModel|null
176
     */
177
    public function getSourceSite()
178
    {
179
        if ($this->site === null) {
180
            $site = $this->resolveSite();
181
            $this->setSourceSite($site);
182
            return $site;
183
        }
184
185
        $sourceSiteId = $this->sourceSiteId;
186
        if ($sourceSiteId !== null &&
187
            $sourceSiteId !== $this->site->id
188
        ) {
189
            $this->site = null;
190
            return $this->getSite();
191
        }
192
193
        return $this->site;
194
    }
195
196
    /**
197
     * @return SiteModel|null
198
     * @throws \yii\base\InvalidArgumentException
199
     */
200
    protected function resolveSite()
201
    {
202
        if ($site = $this->resolveSiteFromRelation()) {
203
            return $site;
204
        }
205
206
        if ($site = $this->resolveSiteFromId()) {
207
            return $site;
208
        }
209
210
        return null;
211
    }
212
213
    /**
214
     * @return SiteModel|null
215
     * @throws \yii\base\InvalidArgumentException
216
     */
217
    private function resolveSiteFromRelation()
218
    {
219
        if (false === $this->isRelationPopulated('siteRecord')) {
220
            return null;
221
        }
222
223
        if (null === ($record = $this->getRelation('siteRecord'))) {
224
            return null;
225
        }
226
227
        /** @var SiteRecord $record */
228
229
        return Craft::$app->getSites()->getSiteById($record->id);
230
    }
231
232
    /**
233
     * @return SiteModel|null
234
     */
235
    private function resolveSiteFromId()
236
    {
237
        if (null === $this->sourceSiteId) {
238
            return null;
239
        }
240
241
        return Craft::$app->getSites()->getSiteById($this->sourceSiteId);
242
    }
243
244
    /**
245
     * Get the associated Site
246
     *
247
     * @return ActiveQueryInterface
248
     */
249
    public function getSiteRecord()
250
    {
251
        return $this->hasOne(
252
            SiteRecord::class,
253
            ['sourceSiteId' => 'id']
254
        );
255
    }
256
}
257