Completed
Branch develop (9e5d0f)
by Nate
01:59
created

SiteHelper   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 59
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 3

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 9
lcom 0
cbo 3
dl 0
loc 59
ccs 0
cts 36
cp 0
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A get() 0 8 2
B resolve() 0 26 5
A resolveSiteId() 0 8 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\ember\helpers;
10
11
use Craft;
12
use craft\models\Site as SiteModel;
13
use craft\records\Site as SiteRecord;
14
15
/**
16
 * @author Flipbox Factory <[email protected]>
17
 * @since 1.0.0
18
 */
19
class SiteHelper
20
{
21
    /**
22
     * @param null $site
23
     * @return SiteModel
24
     */
25
    public static function get($site = null): SiteModel
26
    {
27
        if (null === $site) {
28
            return Craft::$app->getSites()->currentSite;
29
        }
30
31
        return static::resolve($site);
32
    }
33
34
    /**
35
     * @param $site
36
     * @return SiteModel
37
     */
38
    public static function resolve($site = null): SiteModel
39
    {
40
        if ($site instanceof SiteModel) {
41
            return $site;
42
        }
43
44
        if (is_numeric($site)) {
45
            return Craft::$app->getSites()->getSiteById($site);
46
        }
47
48
        if (is_string($site)) {
49
            return Craft::$app->getSites()->getSiteByHandle($site);
50
        }
51
52
        try {
53
            $object = Craft::createObject(SiteModel::class, [$site]);
54
        } catch (\Exception $e) {
55
            $object = new SiteModel();
56
            ObjectHelper::populate(
57
                $object,
58
                $site
59
            );
60
        }
61
62
        return $object;
63
    }
64
65
    /**
66
     * @param int|null $siteId
67
     * @return int
68
     */
69
    public static function resolveSiteId(int $siteId = null): int
70
    {
71
        if (is_null($siteId)) {
72
            $siteId = Craft::$app->getSites()->currentSite->id;
73
        }
74
75
        return $siteId;
76
    }
77
}
78