Passed
Push — master ( 2b8aa5...886616 )
by Simon
03:40 queued 01:46
created

SubsiteState::setDefaultState()   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\Subsites\Model\Subsite;
19
20
/**
21
 * Class \Firesphere\SolrSubsites\States\SubsiteState
22
 *
23
 * Apply states for Subsites
24
 *
25
 * @package Firesphere\SolrSubsites\States
26
 */
27
class SubsiteState extends SiteState implements SiteStateInterface
28
{
29
30
    /**
31
     * Is this state applicable to this extension
32
     * In case of subsites, only apply if there actually are subsites
33
     *
34
     * @param int|string $state
35
     * @return bool
36
     */
37 1
    public function stateIsApplicable($state): bool
38
    {
39 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

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

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