Passed
Push — master ( a3f8a5...450809 )
by Simon
03:16 queued 55s
created

SubsiteState::addSubsiteFilter()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 3
c 1
b 0
f 0
dl 0
loc 5
ccs 4
cts 4
cp 1
rs 10
cc 2
nc 2
nop 2
crap 2
1
<?php
2
/**
3
 * Class SubsiteState|Firesphere\SolrSubsites\States\SubsiteState Enable each subsite to be indexed independently by
4
 * switching the SiteState
5
 * {@see \Firesphere\SolrSearch\States\SiteState} and {@see \Firesphere\SolrSearch\Interfaces\SiteStateInterface}
6
 *
7
 * @package Firesphere\SolrSubsites\States
8
 * @author Simon `Firesphere` Erkelens; Marco `Sheepy` Hermo
9
 * @copyright Copyright (c) 2018 - now() Firesphere & Sheepy
10
 */
11
12
13
namespace Firesphere\SolrSubsites\States;
14
15
use Firesphere\SolrSearch\Interfaces\SiteStateInterface;
16
use Firesphere\SolrSearch\Queries\BaseQuery;
17
use Firesphere\SolrSearch\States\SiteState;
18
use SilverStripe\Core\ClassInfo;
19
use SilverStripe\ORM\DataObject;
20
use SilverStripe\Subsites\Model\Subsite;
21
22
/**
23
 * Class \Firesphere\SolrSubsites\States\SubsiteState
24
 *
25
 * Apply states for Subsites
26
 *
27
 * @package Firesphere\SolrSubsites\States
28
 */
29
class SubsiteState extends SiteState implements SiteStateInterface
30
{
31
32
    /**
33
     * Is this state applicable to this extension
34
     * In case of subsites, only apply if there actually are subsites
35
     *
36
     * @param int|string $state
37
     * @return bool
38
     */
39 1
    public function stateIsApplicable($state): bool
40
    {
41 1
        return Subsite::get()->byID($state) !== null;
0 ignored issues
show
Bug introduced by
It seems like $state can also be of type string; however, parameter $id of SilverStripe\ORM\DataList::byID() does only seem to accept integer, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

41
        return Subsite::get()->byID(/** @scrutinizer ignore-type */ $state) !== null;
Loading history...
42
    }
43
44
    /**
45
     * Reset the SiteState to it's default state
46
     * In case of subsites, we don't care about it, as it's handled at query time
47
     *
48
     * @param string|null $state
49
     * @return mixed
50
     */
51 1
    public function setDefaultState($state = null)
52
    {
53 1
        Subsite::changeSubsite($state);
0 ignored issues
show
Bug introduced by
It seems like $state can also be of type string; however, parameter $subsite of SilverStripe\Subsites\Mo...ubsite::changeSubsite() does only seem to accept SilverStripe\Subsites\Model\Subsite|integer, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

53
        Subsite::changeSubsite(/** @scrutinizer ignore-type */ $state);
Loading history...
54 1
    }
55
56
    /**
57
     * Return the current state of the site
58
     * The current state does not need to be reset in any way for pages
59
     *
60
     * @return string|null
61
     */
62 1
    public function currentState()
63
    {
64 1
        $subsite = Subsite::currentSubsite();
65 1
        return $subsite ? $subsite->ID : null;
0 ignored issues
show
introduced by
$subsite is of type SilverStripe\ORM\DataObject, thus it always evaluated to true.
Loading history...
66
    }
67
68
    /**
69
     * Activate a given state. This should only be done if the state is applicable
70
     * States don't need to be activated, as we index all pages anyway, so set it to disabled
71
     *
72
     * @param string $state
73
     * @return mixed
74
     * @todo This needs to properly set the state
75
     */
76 1
    public function activateState($state)
77
    {
78 1
        Subsite::$disable_subsite_filter = true;
79 1
    }
80
81
    /**
82
     * Method to alter the query. Can be no-op.
83
     *
84
     * @param BaseQuery $query
85
     * @return mixed
86
     */
87 1
    public function updateQuery(&$query)
88
    {
89
        // Only add a Subsite filter if there are actually subsites to filter on
90 1
        if (!Subsite::$disable_subsite_filter && Subsite::currentSubsite()) {
91 1
            foreach ($query->getClasses() as $class) {
92 1
                $this->addSubsiteFilter($query, $class);
93
            }
94
        }
95 1
    }
96
97
    /**
98
     * Add the Subsite specific filter if the class has the extension applied
99
     *
100
     * @param BaseQuery $query
101
     * @param string $class
102
     */
103 1
    protected function addSubsiteFilter(&$query, $class): void
104
    {
105 1
        if (DataObject::getSchema()->hasOneComponent($class, 'Subsite')) {
106 1
            $class = ClassInfo::shortName($class);
107 1
            $query->addFilter($class . '.SubsiteID', Subsite::currentSubsite()->ID);
108
        }
109 1
    }
110
}
111