Passed
Push — hans/Index-all-fluent-options ( 15df6e...534962 )
by Simon
06:48
created

SiteState   A

Complexity

Total Complexity 18

Size/Duplication

Total Lines 157
Duplicated Lines 0 %

Test Coverage

Coverage 25%

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 29
c 2
b 0
f 0
dl 0
loc 157
ccs 9
cts 36
cp 0.25
rs 10
wmc 18

9 Methods

Rating   Name   Duplication   Size   Complexity  
A addStates() 0 3 1
A addState() 0 3 1
A appliesToEnvironment() 0 3 1
A hasExtension() 0 9 3
A setStates() 0 3 1
A getStates() 0 3 1
A variants() 0 18 6
A currentStates() 0 7 2
A withState() 0 8 2
1
<?php
2
3
4
namespace Firesphere\SolrSearch\States;
5
6
use Firesphere\SolrSearch\Compat\FluentExtension;
7
use Firesphere\SolrSearch\Helpers\FieldResolver;
8
use ReflectionClass;
9
use SilverStripe\Core\ClassInfo;
10
use SilverStripe\ORM\DataObject;
11
12
/**
13
 * Class SiteState
14
 *
15
 * Determine and apply the state of the site currently. This is used at index-time to figure out what state to index.
16
 * An example of this is the FluentSearchVariant extension from Fluent.
17
 *
18
 * Fluent uses the old SearchVariant method, which is actually not that bad a concept. These "Variants", now called
19
 * "States" set the state of the site to a required setting for each available state.
20
 *
21
 * States SHOULD add their own states through an extension, otherwise it won't be called.
22
 * {@see FluentExtension::onBeforeInit()}
23
 *
24
 * States, options, etc. are simplified for a more streamlined approach
25
 *
26
 * @package Firesphere\SolrSearch\States
27
 */
28
abstract class SiteState
29
{
30
31
    /**
32
     * States that can be applied
33
     *
34
     * @var array
35
     */
36
    public static $states = [
37
        'default',
38
    ];
39
40
    /**
41
     * Variants of SiteState that can be activated
42
     *
43
     * @var array
44
     */
45
    public static $variants;
46
47
    /**
48
     * @var bool Is this State enabled
49
     */
50
    public $enabled = true;
51
52
    /**
53
     * Does this state apply to the current object/environment settings
54
     *
55
     * @return bool
56
     */
57
    public function appliesToEnvironment(): bool
58
    {
59
        return $this->enabled;
60
    }
61
62
    /**
63
     * Get available states
64
     *
65
     * @return array
66
     */
67 11
    public static function getStates(): array
68
    {
69 11
        return self::$states;
70
    }
71
72
    /**
73
     * Set states
74
     *
75
     * @param array $states
76
     */
77
    public static function setStates(array $states): void
78
    {
79
        self::$states = $states;
80
    }
81
82
    /**
83
     * Add a state
84
     *
85
     * @param $state
86
     */
87
    public static function addState($state): void
88
    {
89
        self::$states[] = $state;
90
    }
91
92
    /**
93
     * Add multiple states
94
     *
95
     * @param array $states
96
     */
97
    public static function addStates(array $states): void
98
    {
99
        self::$states = array_merge(self::$states, $states);
100
    }
101
102
    /**
103
     * Does this class, it's parent (or optionally one of it's children) have the passed extension attached?
104
     *
105
     * @param $class
106
     * @param $extension
107
     * @return bool
108
     * @throws \ReflectionException
109
     */
110
    public static function hasExtension($class, $extension): bool
111
    {
112
        /** @var DataObject $relatedclass */
113
        foreach (FieldResolver::gethierarchy($class) as $relatedclass) {
114
            if ($relatedclass::has_extension($extension)) {
115
                return true;
116
            }
117
        }
118
        return false;
119
    }
120
121
122
    /**
123
     * Returns an array of variants.
124
     *
125
     * With no arguments, returns all variants
126
     *
127
     * With a classname as the first argument, returns the variants that apply to that class
128
     * (optionally including subclasses)
129
     *
130
     * @static
131
     * @return array - An array of (string)$variantClassName => (Object)$variantInstance pairs
132
     * @throws \ReflectionException
133
     */
134 12
    public static function variants(): ?array
135
    {
136
        // Build up and cache a list of all search variants (subclasses of SearchVariant)
137 12
        if (!empty(self::$variants)) {
138
            $classes = ClassInfo::subclassesFor(static::class);
139
140
            foreach ($classes as $variantclass) {
141
                $ref = new ReflectionClass($variantclass);
142
                if ($ref->isInstantiable()) {
143
                    $variant = singleton($variantclass);
144
                    if ($variant->appliesToEnvironment()) {
145
                        self::$variants[$variantclass] = $variant;
146
                    }
147
                }
148
            }
149
        }
150
151 12
        return !empty(self::$variants) ? self::$variants : [];
152
    }
153
154
155
    /**
156
     * Get the current state of every variant
157
     *
158
     * @static
159
     * @return array
160
     * @throws \ReflectionException
161
     */
162 12
    public static function currentStates(): array
163
    {
164 12
        $state = [];
165 12
        foreach (self::variants() as $variant => $instance) {
166
            $state[$variant] = $instance->currentState();
167
        }
168 12
        return $state;
169
    }
170
171
    /**
172
     * Activate a site state for indexing
173
     *
174
     * @param $state
175
     * @throws \ReflectionException
176
     */
177
    public static function withState($state): void
178
    {
179
        /**
180
         * @var string $variant
181
         * @var static $instance
182
         */
183
        foreach (self::variants() as $variant => $instance) {
184
            $instance->activateState($state);
185
        }
186
    }
187
}
188