Passed
Push — master ( fa7cf7...2cee16 )
by Thomas
12:02
created

HasSubsites::getSubsiteId()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 4
nc 2
nop 0
dl 0
loc 7
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace LeKoala\Admini\Subsites;
4
5
use SilverStripe\Core\Convert;
6
use SilverStripe\ORM\ArrayList;
7
use SilverStripe\ORM\DataObject;
8
use SilverStripe\View\ArrayData;
9
use SilverStripe\View\Requirements;
10
use SilverStripe\Subsites\Model\Subsite;
0 ignored issues
show
Bug introduced by
The type SilverStripe\Subsites\Model\Subsite was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
11
use SilverStripe\Subsites\State\SubsiteState;
0 ignored issues
show
Bug introduced by
The type SilverStripe\Subsites\State\SubsiteState was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
12
13
/**
14
 * Allows you to have a common api regardless of Subsite module being installed
15
 */
16
trait HasSubsites
17
{
18
    /**
19
     * @return Subsite
20
     */
21
    public function CurrentSubsite()
22
    {
23
        if (!class_exists(SubsiteState::class)) {
24
            return false;
0 ignored issues
show
Bug Best Practice introduced by
The expression return false returns the type false which is incompatible with the documented return type SilverStripe\Subsites\Model\Subsite.
Loading history...
25
        }
26
        $class = SubsiteState::class;
27
        $id = $class::singleton()->getSubsiteId();
28
        return DataObject::get_by_id(Subsite::class, $id);
0 ignored issues
show
Bug Best Practice introduced by
The expression return SilverStripe\ORM\...el\Subsite::class, $id) also could return the type SilverStripe\ORM\DataObject which is incompatible with the documented return type SilverStripe\Subsites\Model\Subsite.
Loading history...
29
    }
30
31
    /**
32
     * Remove the need for SubsiteMenuExtension
33
     *
34
     * @return bool
35
     */
36
    public function subsiteCMSShowInMenu()
37
    {
38
        return true;
39
    }
40
41
    /**
42
     * @return int
43
     */
44
    public function getSubsiteId()
45
    {
46
        $subsite = $this->CurrentSubsite();
47
        if ($subsite) {
0 ignored issues
show
introduced by
$subsite is of type SilverStripe\Subsites\Model\Subsite, thus it always evaluated to true.
Loading history...
48
            return $subsite->ID;
49
        }
50
        return 0;
51
    }
52
53
    /**
54
     * @return ArrayList
55
     */
56
    public function ListSubsitesExpanded()
57
    {
58
        if (!class_exists(SubsiteState::class)) {
59
            return false;
0 ignored issues
show
Bug Best Practice introduced by
The expression return false returns the type false which is incompatible with the documented return type SilverStripe\ORM\ArrayList.
Loading history...
60
        }
61
62
        $class = Subsite::class;
63
        $list = $class::all_accessible_sites();
64
        if ($list == null || $list->count() == 1 && $list->first()->DefaultSite == true) {
65
            return false;
0 ignored issues
show
Bug Best Practice introduced by
The expression return false returns the type false which is incompatible with the documented return type SilverStripe\ORM\ArrayList.
Loading history...
66
        }
67
68
        $currentSubsite = $this->CurrentSubsite();
69
        $output = ArrayList::create();
70
71
        foreach ($list as $subsite) {
72
            $currentState = $currentSubsite && $subsite->ID == $currentSubsite->ID ? 'selected' : '';
73
74
            $SiteConfig = $subsite->SiteConfig();
75
            if (!$SiteConfig) {
76
                continue;
77
            }
78
            $PrimaryColor = $SiteConfig->dbObject('PrimaryColor');
79
80
            $output->push(ArrayData::create([
81
                'CurrentState' => $currentState,
82
                'ID' => $subsite->ID,
83
                'Title' => Convert::raw2xml($subsite->Title),
84
                'BackgroundColor' => $PrimaryColor->Color(),
85
                'Color' => $PrimaryColor->ContrastColor(),
86
            ]));
87
        }
88
89
        return $output;
90
    }
91
92
    public function blockSubsiteRequirements()
93
    {
94
        // We need this since block will try to resolve module path :-(
95
        if (!class_exists(SubsiteState::class)) {
96
            return;
97
        }
98
        Requirements::block('silverstripe/subsites:client/css/LeftAndMain_Subsites.css');
99
        Requirements::block('silverstripe/subsites:css/LeftAndMain_Subsites.css');
100
        Requirements::block('silverstripe/subsites:client/javascript/LeftAndMain_Subsites.js');
101
        Requirements::block('silverstripe/subsites:javascript/LeftAndMain_Subsites.js');
102
        Requirements::block('silverstripe/subsites:client/javascript/VirtualPage_Subsites.js');
103
        Requirements::block('silverstripe/subsites:javascript/VirtualPage_Subsites.js');
104
    }
105
}
106