Completed
Push — develop ( 3f9c00...d25215 )
by Nate
02:35
created

SiteAttributeTrait   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 90
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 90
ccs 0
cts 48
cp 0
rs 10
c 0
b 0
f 0

7 Methods

Rating   Name   Duplication   Size   Complexity  
A siteAttributes() 0 6 1
A siteAttributeLabels() 0 6 1
A internalSetSiteId() 0 5 1
A internalGetSiteId() 0 7 2
A resolveSite() 0 8 2
A resolveSiteFromRelation() 0 14 3
A getSiteRecord() 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\models\Site as SiteModel;
13
use craft\records\Site as SiteRecord;
14
use flipbox\craft\ember\models\SiteRulesTrait;
15
use flipbox\craft\ember\objects\SiteMutatorTrait;
16
use yii\db\ActiveQueryInterface;
17
18
/**
19
 * Intended to be used on an ActiveRecord, this class provides `$this->siteId` attribute along with 'getters'
20
 * and 'setters' to ensure continuity between the Id and Object.  A site 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 SiteRecord $siteRecord
27
 */
28
trait SiteAttributeTrait
29
{
30
    use ActiveRecordTrait,
31
        SiteRulesTrait,
32
        SiteMutatorTrait;
33
34
    /**
35
     * @inheritdoc
36
     */
37
    public function siteAttributes(): array
38
    {
39
        return [
40
            'siteId'
41
        ];
42
    }
43
44
    /**
45
     * @inheritdoc
46
     */
47
    public function siteAttributeLabels(): array
48
    {
49
        return [
50
            'siteId' => Craft::t('app', 'Site Id')
51
        ];
52
    }
53
54
    /**
55
     * @inheritDoc
56
     */
57
    protected function internalSetSiteId(int $id = null)
58
    {
59
        $this->setAttribute('siteId', $id);
60
        return $this;
61
    }
62
63
    /**
64
     * @inheritDoc
65
     */
66
    protected function internalGetSiteId()
67
    {
68
        if (null === ($id = $this->getAttribute('siteId'))) {
69
            return null;
70
        }
71
        return (int)$id;
72
    }
73
74
    /**
75
     * @return SiteModel|null
76
     * @throws \yii\base\InvalidArgumentException
77
     */
78
    protected function resolveSite()
79
    {
80
        if ($site = $this->resolveSiteFromRelation()) {
81
            return $site;
82
        }
83
84
        return $this->resolveSiteFromId();
85
    }
86
87
    /**
88
     * @return SiteModel|null
89
     */
90
    private function resolveSiteFromRelation()
91
    {
92
        if (false === $this->isRelationPopulated('siteRecord')) {
93
            return null;
94
        }
95
96
        if (null === ($record = $this->getRelation('siteRecord'))) {
97
            return null;
98
        }
99
100
        /** @var SiteRecord $record */
101
102
        return Craft::$app->getSites()->getSiteById($record->id);
103
    }
104
105
    /**
106
     * Get the associated Site
107
     *
108
     * @return ActiveQueryInterface
109
     */
110
    public function getSiteRecord()
111
    {
112
        return $this->hasOne(
113
            SiteRecord::class,
114
            ['id' => 'siteId']
115
        );
116
    }
117
}
118