Passed
Push — master ( 72198b...d1ce69 )
by Thomas
02:46
created

HasSubsites::CurrentSubsite()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 4
c 1
b 0
f 0
nc 2
nop 0
dl 0
loc 7
rs 10
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
trait HasSubsites
14
{
15
    /**
16
     * @return Subsite
17
     */
18
    public function CurrentSubsite()
19
    {
20
        if (!class_exists(SubsiteState::class)) {
21
            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...
22
        }
23
        $id = SubsiteState::singleton()->getSubsiteId();
24
        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...
25
    }
26
27
    public function ListSubsitesExpanded()
28
    {
29
        if (!class_exists(SubsiteState::class)) {
30
            return false;
31
        }
32
33
        $list = Subsite::all_accessible_sites();
34
        if ($list == null || $list->count() == 1 && $list->first()->DefaultSite == true) {
35
            return false;
36
        }
37
38
        $currentSubsite = $this->CurrentSubsite();
39
        $output = ArrayList::create();
40
41
        foreach ($list as $subsite) {
42
            $currentState = $currentSubsite && $subsite->ID == $currentSubsite->ID ? 'selected' : '';
43
44
            $SiteConfig = $subsite->SiteConfig();
45
            if (!$SiteConfig) {
46
                continue;
47
            }
48
            $PrimaryColor = $SiteConfig->dbObject('PrimaryColor');
49
50
            $output->push(ArrayData::create([
51
                'CurrentState' => $currentState,
52
                'ID' => $subsite->ID,
53
                'Title' => Convert::raw2xml($subsite->Title),
54
                'BackgroundColor' => $PrimaryColor->Color(),
55
                'Color' => $PrimaryColor->ContrastColor(),
56
            ]));
57
        }
58
59
        return $output;
60
    }
61
62
    public function blockSubsiteRequirements()
63
    {
64
        Requirements::block('silverstripe/subsites:css/LeftAndMain_Subsites.css');
65
        Requirements::block('silverstripe/subsites:javascript/LeftAndMain_Subsites.js');
66
        Requirements::block('silverstripe/subsites:javascript/VirtualPage_Subsites.js');
67
    }
68
}
69