OrganizationSitesTrait   A
last analyzed

Complexity

Total Complexity 12

Size/Duplication

Total Lines 72
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 3

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 12
lcom 0
cbo 3
dl 0
loc 72
ccs 0
cts 41
cp 0
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A getEnabledSiteIds() 0 15 4
A getSiteIds() 0 16 3
A getSiteUrl() 0 4 2
A showSites() 0 14 3
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\organizations\cp\controllers;
10
11
use Craft;
12
use flipbox\organizations\elements\Organization as OrganizationElement;
13
use flipbox\organizations\Organizations;
14
15
/**
16
 * @author Flipbox Factory <[email protected]>
17
 * @since 1.0.0
18
 */
19
trait OrganizationSitesTrait
20
{
21
    /**
22
     * @param OrganizationElement $organization
23
     * @return int[]
24
     * @throws \craft\errors\SiteNotFoundException
25
     */
26
    protected function getEnabledSiteIds(OrganizationElement $organization): array
27
    {
28
        if ($organization->id !== null) {
29
            return Craft::$app->getElements()->getEnabledSiteIdsForElement($organization->id);
30
        }
31
32
        $enabledSiteIds = [];
33
        foreach (Organizations::getInstance()->getSettings()->getSiteSettings() as $siteSettings) {
34
            if ($siteSettings->enabledByDefault) {
35
                $enabledSiteIds[] = $siteSettings->getSiteId();
36
            }
37
        }
38
39
        return $enabledSiteIds;
40
    }
41
42
    /**
43
     * @return array
44
     * @throws \craft\errors\SiteNotFoundException
45
     */
46
    protected function getSiteIds(): array
47
    {
48
        if (Craft::$app->getIsMultiSite() === false) {
49
            return [];
50
        }
51
52
        $sites = Organizations::getInstance()->getSettings()->getSiteSettings();
53
54
        if (count($sites) <= 1) {
55
            return [];
56
        }
57
58
        return array_keys(
59
            Organizations::getInstance()->getSettings()->getSiteSettings()
60
        );
61
    }
62
63
    /**
64
     * @param OrganizationElement $organization
65
     * @return string
66
     */
67
    protected function getSiteUrl(OrganizationElement $organization): string
68
    {
69
        return Organizations::getInstance()->getUniqueId() . '/' . ($organization->getId() ?: 'new');
70
    }
71
72
    /**
73
     * @return bool
74
     * @throws \craft\errors\SiteNotFoundException
75
     */
76
    protected function showSites(): bool
77
    {
78
        if (Craft::$app->getIsMultiSite() === false) {
79
            return false;
80
        }
81
82
        $sites = Organizations::getInstance()->getSettings()->getSiteSettings();
83
84
        if (count($sites) <= 1) {
85
            return false;
86
        }
87
88
        return true;
89
    }
90
}
91