Passed
Push — master ( 06e2d0...7d38e6 )
by Simon
03:39
created

SubsiteState::activateState()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
eloc 1
c 0
b 0
f 0
dl 0
loc 3
rs 10
ccs 2
cts 2
cp 1
cc 1
nc 1
nop 1
crap 1
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
     */
75 1
    public function activateState($state)
76
    {
77 1
        Subsite::$disable_subsite_filter = true;
78 1
    }
79
80
    /**
81
     * Method to alter the query. Can be no-op.
82
     *
83
     * @param BaseQuery $query
84
     * @return mixed
85
     */
86
    public function updateQuery(&$query)
87
    {
88
        // Only add a Subsite filter if there are actually subsites to filter on
89
        if (!Subsite::$disable_subsite_filter && Subsite::get()->exists()) {
90
            foreach ($query->getClasses() as $class) {
91
                if (DataObject::getSchema()->hasOneComponent($class, 'Subsite')) {
92
                    $class = ClassInfo::shortName($class);
93
                    $query->addFilter($class . '_SubsiteID', Subsite::getSubsiteIDForDomain());
94
                }
95
            }
96
        }
97
    }
98
}
99