Completed
Push — master ( e3199e...dd66f1 )
by Nate
08:31 queued 06:54
created

SiteMutator::getSite()   A

Complexity

Conditions 4
Paths 3

Size

Total Lines 18
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 20

Importance

Changes 0
Metric Value
dl 0
loc 18
ccs 0
cts 16
cp 0
rs 9.2
c 0
b 0
f 0
cc 4
eloc 11
nc 3
nop 0
crap 20
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\traits;
10
11
use Craft;
12
use craft\models\Site as SiteModel;
13
use flipbox\ember\helpers\SiteHelper;
14
15
/**
16
 * @property int|null $siteId
17
 *
18
 * @author Flipbox Factory <[email protected]>
19
 * @since 1.0.0
20
 */
21
trait SiteMutator
22
{
23
    /**
24
     * @var SiteModel|null
25
     */
26
    private $site;
27
28
    /**
29
     * Set associated siteId
30
     *
31
     * @param $id
32
     * @return $this
33
     */
34
    public function setSiteId(int $id)
35
    {
36
        $this->siteId = $id;
37
        return $this;
38
    }
39
40
    /**
41
     * Get associated siteId
42
     *
43
     * @return int|null
44
     */
45
    public function getSiteId()
46
    {
47
        if (null === $this->siteId && null !== $this->site) {
48
            $this->siteId = $this->site->id;
49
        }
50
51
        return $this->siteId;
52
    }
53
54
    /**
55
     * Associate a site
56
     *
57
     * @param mixed $site
58
     * @return $this
59
     */
60
    public function setSite($site = null)
61
    {
62
        $this->site = null;
63
64
        if (($site = SiteHelper::resolve($site)) === null) {
65
            $this->site = $this->siteId = null;
66
        } else {
67
            $this->siteId = $site->id;
68
            $this->site = $site;
69
        }
70
71
        return $this;
72
    }
73
74
    /**
75
     * @return SiteModel|null
76
     */
77
    public function getSite()
78
    {
79
        if ($this->site === null) {
80
            $site = $this->resolveSite();
81
            $this->setSite($site);
82
            return $site;
83
        }
84
85
        $siteId = $this->siteId;
86
        if ($siteId !== null &&
87
            $siteId !== $this->site->id
88
        ) {
89
            $this->site = null;
90
            return $this->getSite();
91
        }
92
93
        return $this->site;
94
    }
95
96
    /**
97
     * @return SiteModel|null
98
     */
99
    protected function resolveSite()
100
    {
101
        if ($site = $this->resolveSiteFromId()) {
102
            return $site;
103
        }
104
105
        return null;
106
    }
107
108
    /**
109
     * @return SiteModel|null
110
     */
111
    private function resolveSiteFromId()
112
    {
113
        if (null === $this->siteId) {
114
            return null;
115
        }
116
117
        return Craft::$app->getSites()->getSiteById($this->siteId);
118
    }
119
}
120