Completed
Push — master ( eae9dd...13bbf2 )
by Nate
06:11
created

SiteMutatorTrait::isSiteSet()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

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