SiteHelper::resolve()   B
last analyzed

Complexity

Conditions 6
Paths 6

Size

Total Lines 30

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 42

Importance

Changes 0
Metric Value
dl 0
loc 30
ccs 0
cts 25
cp 0
rs 8.8177
c 0
b 0
f 0
cc 6
nc 6
nop 1
crap 42
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\helpers;
10
11
use Craft;
12
use craft\models\Site as SiteModel;
13
14
/**
15
 * @author Flipbox Factory <[email protected]>
16
 * @since 2.0.0
17
 */
18
class SiteHelper
19
{
20
    /**
21
     * @param $site
22
     * @return SiteModel
23
     */
24
    public static function resolve($site = null): SiteModel
25
    {
26
        if ($site instanceof SiteModel) {
27
            return $site;
28
        }
29
30
        if (null === $site) {
31
            return Craft::$app->getSites()->currentSite;
32
        }
33
34
        if (is_numeric($site)) {
35
            return Craft::$app->getSites()->getSiteById($site);
36
        }
37
38
        if (is_string($site)) {
39
            return Craft::$app->getSites()->getSiteByHandle($site);
40
        }
41
42
        try {
43
            $object = Craft::createObject(SiteModel::class, [$site]);
44
        } catch (\Exception $e) {
45
            $object = new SiteModel();
46
            ObjectHelper::populate(
47
                $object,
48
                $site
49
            );
50
        }
51
52
        return $object;
53
    }
54
55
    /**
56
     * @param int|null $siteId
57
     * @return int
58
     */
59
    public static function ensureSiteId(int $siteId = null): int
60
    {
61
        if (null === $siteId) {
62
            $siteId = (int)Craft::$app->getSites()->currentSite->id;
63
        }
64
65
        return (int)$siteId;
66
    }
67
}
68