SolrReindexTest_Variant::get_current()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 2
eloc 2
c 2
b 0
f 0
nc 2
nop 0
dl 0
loc 5
rs 10
1
<?php
2
3
namespace SilverStripe\FullTextSearch\Tests\SolrReindexTest;
4
5
use SilverStripe\Dev\TestOnly;
6
use SilverStripe\FullTextSearch\Search\Indexes\SearchIndex;
7
use SilverStripe\FullTextSearch\Search\Queries\SearchQuery;
8
use SilverStripe\FullTextSearch\Search\Variants\SearchVariant;
9
use SilverStripe\ORM\DataObject;
10
11
/**
12
 * Dummy variant that selects items with field Variant matching the current value
13
 *
14
 * Variant states are 0 and 1, or null if disabled
15
 */
16
class SolrReindexTest_Variant extends SearchVariant implements TestOnly
17
{
18
    /**
19
     * Value of this variant (either null, 0, or 1)
20
     *
21
     * @var int|null
22
     */
23
    protected static $current = null;
24
25
    /**
26
     * Activate this variant
27
     */
28
    public static function enable()
29
    {
30
        self::disable();
31
32
        self::$current = 0;
33
        self::$variants = [
34
            self::class => singleton(self::class)
35
        ];
36
    }
37
38
    /**
39
     * Disable this variant and reset
40
     */
41
    public static function disable()
42
    {
43
        self::$current = null;
44
        self::$variants = null;
45
        self::$class_variants = [];
46
        self::$call_instances = [];
47
    }
48
49
    public function activateState($state)
50
    {
51
        self::set_current($state);
52
    }
53
54
    /**
55
     * Set the current variant to the given state
56
     *
57
     * @param int $current 0, 1, 2, or null (disabled)
58
     */
59
    public static function set_current($current)
60
    {
61
        self::$current = $current;
62
    }
63
64
    /**
65
     * Get the current state
66
     *
67
     * @return string|null
68
     */
69
    public static function get_current()
70
    {
71
        // Always use string values for states for consistent json_encode value
72
        if (isset(self::$current)) {
73
            return (string) self::$current;
74
        }
75
    }
76
77
    /**
78
     * @param string $class
79
     * @param SearchIndex $index
80
     */
81
    public function alterDefinition($class, $index)
82
    {
83
        $self = get_class($this);
84
85
        $this->addFilterField($index, '_testvariant', [
86
            'name' => '_testvariant',
87
            'field' => '_testvariant',
88
            'fullfield' => '_testvariant',
89
            'base' => DataObject::getSchema()->baseDataClass($class),
90
            'origin' => $class,
91
            'type' => 'Int',
92
            'lookup_chain' => [
93
                [
94
                    'call' => 'variant',
95
                    'variant' => $self,
96
                    'method' => 'currentState'
97
                ]
98
            ]
99
        ]);
100
    }
101
102
    /**
103
     * @param SearchQuery $query
104
     * @param SearchIndex $index
105
     */
106
    public function alterQuery($query, $index)
107
    {
108
        // I guess just calling it _testvariant is ok?
109
        $query->addFilter('_testvariant', $this->currentState());
110
    }
111
112
    public function appliesTo($class, $includeSubclasses)
113
    {
114
        return $class === SolrReindexTest_Item::class ||
115
            ($includeSubclasses && is_subclass_of($class, SolrReindexTest_Item::class, true));
116
    }
117
118
    public function appliesToEnvironment()
119
    {
120
        // Set to null to disable
121
        return self::$current !== null;
122
    }
123
124
    public function currentState()
125
    {
126
        return self::get_current();
127
    }
128
129
    public function reindexStates()
130
    {
131
        // Always use string values for states for consistent json_encode value
132
        return array('0', '1', '2');
133
    }
134
}
135