Completed
Branch develop (cd8f66)
by Nate
03:44
created

SiteAttribute   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 62
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 4

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 8
lcom 2
cbo 4
dl 0
loc 62
ccs 0
cts 29
cp 0
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A getSiteId() 0 9 3
A resolveSite() 0 8 2
A resolveSiteFromRelation() 0 8 2
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\ember\records\traits;
10
11
use craft\models\Site as SiteModel;
12
use craft\records\Site as SiteRecord;
13
use flipbox\ember\helpers\SiteHelper;
14
use flipbox\ember\traits\SiteMutator;
15
use flipbox\ember\traits\SiteRules;
16
use yii\db\ActiveQueryInterface;
17
18
/**
19
 * @author Flipbox Factory <[email protected]>
20
 * @since 1.0.0
21
 *
22
 * @method SiteModel parentResolveSite()
23
 */
24
trait SiteAttribute
25
{
26
    use ActiveRecord,
27
        SiteRules,
28
        SiteMutator {
29
            resolveSite as parentResolveSite;
30
    }
31
32
    /**
33
     * Get associated siteId
34
     *
35
     * @return int|null
36
     */
37
    public function getSiteId()
38
    {
39
        $siteId = $this->getAttribute('siteId');
40
        if (null === $siteId && null !== $this->site) {
41
            $siteId = $this->siteId = $this->site->id;
42
        }
43
44
        return $siteId;
45
    }
46
47
    /**
48
     * @return SiteModel|null
49
     * @throws \yii\base\InvalidArgumentException
50
     */
51
    protected function resolveSite()
52
    {
53
        if ($site = $this->resolveSiteFromRelation()) {
54
            return $site;
55
        }
56
57
        return $this->parentResolveSite();
58
    }
59
60
    /**
61
     * @return SiteModel|null
62
     * @throws \yii\base\InvalidArgumentException
63
     */
64
    private function resolveSiteFromRelation()
65
    {
66
        if (false === $this->isRelationPopulated('siteRecord')) {
67
            return null;
68
        }
69
70
        return SiteHelper::resolve($this->getRelation('siteRecord'));
71
    }
72
73
    /**
74
     * Get the associated Site
75
     *
76
     * @return ActiveQueryInterface
77
     */
78
    public function getSiteRecord()
79
    {
80
        return $this->hasOne(
81
            SiteRecord::class,
82
            ['siteId' => 'id']
83
        );
84
    }
85
}
86