Passed
Push — master ( d8828e...96d5b6 )
by Simon
09:12
created

SubsiteState::stateIsApplicable()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 1
b 0
f 0
nc 1
nop 1
dl 0
loc 3
rs 10
1
<?php
2
3
4
namespace Firesphere\SolrSubsites\States;
5
6
7
use Firesphere\SolrSearch\Interfaces\SiteStateInterface;
8
use Firesphere\SolrSearch\Queries\BaseQuery;
9
use Firesphere\SolrSearch\States\SiteState;
10
use SilverStripe\Subsites\Model\Subsite;
11
12
/**
13
 * Class \Firesphere\SolrSubsites\States\SubsiteState
14
 *
15
 * Apply states for Subsites
16
 *
17
 * @package Firesphere\SolrSubsites\States
18
 */
19
class SubsiteState extends SiteState implements SiteStateInterface
20
{
21
22
    /**
23
     * Is this state applicable to this extension
24
     * In case of subsites, only apply if there actually are subsites
25
     *
26
     * @param string $state
27
     * @return bool
28
     */
29
    public function stateIsApplicable($state): bool
30
    {
31
        return Subsite::get()->byID($state) !== null;
0 ignored issues
show
Bug introduced by
$state of type string is incompatible with the type integer expected by parameter $id of SilverStripe\ORM\DataList::byID(). ( Ignorable by Annotation )

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

31
        return Subsite::get()->byID(/** @scrutinizer ignore-type */ $state) !== null;
Loading history...
32
    }
33
34
    /**
35
     * Reset the SiteState to it's default state
36
     * In case of subsites, we don't care about it, as it's handled at query time
37
     *
38
     * @param string|null $state
39
     * @return mixed
40
     */
41
    public function setDefaultState($state = null)
42
    {
43
        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

43
        Subsite::changeSubsite(/** @scrutinizer ignore-type */ $state);
Loading history...
44
    }
45
46
    /**
47
     * Return the current state of the site
48
     * The current state does not need to be reset in any way for pages
49
     *
50
     * @return string|null
51
     */
52
    public function currentState()
53
    {
54
        $subsite = Subsite::currentSubsite();
55
        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...
56
    }
57
58
    /**
59
     * Activate a given state. This should only be done if the state is applicable
60
     * States don't need to be activated, as we index all pages anyway, so set it to disabled
61
     *
62
     * @param string $state
63
     * @return mixed
64
     */
65
    public function activateState($state)
66
    {
67
        Subsite::$disable_subsite_filter = true;
68
    }
69
70
    /**
71
     * Method to alter the query. Can be no-op.
72
     *
73
     * @param BaseQuery $query
74
     * @return mixed
75
     */
76
    public function updateQuery(&$query)
77
    {
78
        if (Subsite::$disable_subsite_filter !== false) {
79
            $query->addFilter('SubsiteID', Subsite::getSubsiteIDForDomain());
80
        }
81
    }
82
}
83