Passed
Push — master ( 886616...06e2d0 )
by Simon
01:58
created

SubsiteState::updateQuery()   A

Complexity

Conditions 4
Paths 3

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 20

Importance

Changes 0
Metric Value
eloc 4
c 0
b 0
f 0
dl 0
loc 7
rs 10
ccs 0
cts 5
cp 0
cc 4
nc 3
nop 1
crap 20
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\Subsites\Model\Subsite;
20
21
/**
22
 * Class \Firesphere\SolrSubsites\States\SubsiteState
23
 *
24
 * Apply states for Subsites
25
 *
26
 * @package Firesphere\SolrSubsites\States
27
 */
28
class SubsiteState extends SiteState implements SiteStateInterface
29
{
30
31
    /**
32
     * Is this state applicable to this extension
33
     * In case of subsites, only apply if there actually are subsites
34
     *
35
     * @param int|string $state
36
     * @return bool
37
     */
38 1
    public function stateIsApplicable($state): bool
39
    {
40 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

40
        return Subsite::get()->byID(/** @scrutinizer ignore-type */ $state) !== null;
Loading history...
41
    }
42
43
    /**
44
     * Reset the SiteState to it's default state
45
     * In case of subsites, we don't care about it, as it's handled at query time
46
     *
47
     * @param string|null $state
48
     * @return mixed
49
     */
50 1
    public function setDefaultState($state = null)
51
    {
52 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

52
        Subsite::changeSubsite(/** @scrutinizer ignore-type */ $state);
Loading history...
53 1
    }
54
55
    /**
56
     * Return the current state of the site
57
     * The current state does not need to be reset in any way for pages
58
     *
59
     * @return string|null
60
     */
61 1
    public function currentState()
62
    {
63 1
        $subsite = Subsite::currentSubsite();
64 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...
65
    }
66
67
    /**
68
     * Activate a given state. This should only be done if the state is applicable
69
     * States don't need to be activated, as we index all pages anyway, so set it to disabled
70
     *
71
     * @param string $state
72
     * @return mixed
73
     */
74 1
    public function activateState($state)
75
    {
76 1
        Subsite::$disable_subsite_filter = true;
77 1
    }
78
79
    /**
80
     * Method to alter the query. Can be no-op.
81
     *
82
     * @param BaseQuery $query
83
     * @return mixed
84
     */
85
    public function updateQuery(&$query)
86
    {
87
        // Only add a Subsite filter if there are actually subsites to filter on
88
        if (!Subsite::$disable_subsite_filter && Subsite::get()->exists()) {
89
            foreach ($query->getClasses() as $class) {
90
                $class = ClassInfo::shortName($class);
91
                $query->addFilter($class . '_SubsiteID', Subsite::getSubsiteIDForDomain());
92
            }
93
        }
94
    }
95
}
96