Passed
Push — sheepy/elevation-configuration ( f8fadb...77a4b0 )
by Marco
07:42
created

CoreServiceTrait::setInDebugMode()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 2
nc 1
nop 1
dl 0
loc 5
ccs 3
cts 3
cp 1
crap 1
rs 10
c 1
b 0
f 0
1
<?php
2
/**
3
 * Trait CoreServiceTrait|Firesphere\SolrSearch\Traits\CoreServiceTraits to have simple methods that don't really
4
 * need to be in core.
5
 *
6
 * @package Firesphere\SolrSearch\Traits
7
 * @author Simon `Firesphere` Erkelens; Marco `Sheepy` Hermo
8
 * @copyright Copyright (c) 2018 - now() Firesphere & Sheepy
9
 */
10
11
namespace Firesphere\SolrSearch\Traits;
12
13
use Firesphere\SolrSearch\Helpers\FieldResolver;
14
use Psr\SimpleCache\CacheInterface;
15
use Psr\SimpleCache\InvalidArgumentException;
16
use ReflectionException;
17
use SilverStripe\Core\Injector\Injector;
18
use Solarium\Client;
19
use Solarium\QueryType\Server\CoreAdmin\Query\Query;
20
21
/**
22
 * Trait CoreServiceTrait to have simple methods that don't really need to be in core.
23
 *
24
 * Trait to support basic settings for the Solr Core Service
25
 *
26
 * @package Firesphere\SolrSearch\Traits
27
 */
28
trait CoreServiceTrait
29
{
30
    /**
31
     * Add debugging information
32
     *
33
     * @var bool
34
     */
35
    protected $debug = false;
36
    /**
37
     * @var Client The current client
38
     */
39
    protected $client;
40
    /**
41
     * @var Query A core admin object
42
     */
43
    protected $admin;
44
45
    /**
46
     * Check if we are in debug mode
47
     *
48
     * @return bool
49
     */
50 7
    public function isDebug(): bool
51
    {
52 7
        return $this->debug;
53
    }
54
55
    /**
56
     * Set the debug mode
57
     *
58
     * @param bool $debug
59
     * @return self
60
     */
61 4
    public function setDebug(bool $debug): self
62
    {
63 4
        $this->debug = $debug;
64
65 4
        return $this;
66
    }
67
68
    /**
69
     * Is the given class a valid class to index
70
     * Does not discriminate against the indexes. All indexes are worth the same
71
     *
72
     * @param string $class
73
     * @return bool
74
     * @throws ReflectionException
75
     * @throws InvalidArgumentException
76
     */
77 85
    public function isValidClass($class): bool
78
    {
79 85
        $classes = $this->getValidClasses();
80
81 85
        return in_array($class, $classes, true);
82
    }
83
84
    /**
85
     * Get all classes from all indexes and return them.
86
     * Used to get all classes that are to be indexed on change
87
     * Note, only base classes are in this object. A publish recursive is required
88
     * when any change from a relation is published.
89
     *
90
     * @return array
91
     * @throws ReflectionException
92
     * @throws InvalidArgumentException
93
     */
94 85
    public function getValidClasses(): array
95
    {
96
        /** @var CacheInterface $cache */
97 85
        $cache = Injector::inst()->get(CacheInterface::class . '.SolrCache');
98
99 85
        if ($cache->has('ValidClasses')) {
100 85
            return $cache->get('ValidClasses');
101
        }
102
103 35
        $indexes = $this->getValidIndexes();
104 35
        $classes = [];
105 35
        foreach ($indexes as $index) {
106 35
            $classes = $this->getClassesInHierarchy($index, $classes);
107
        }
108
109 35
        $cache->set('ValidClasses', array_unique($classes));
110
111 35
        return array_unique($classes);
112
    }
113
114
    /**
115
     * Ensure the getValidIndexes() method exists on all classes using this trait.
116
     *
117
     * @return mixed
118
     */
119
    abstract public function getValidIndexes();
120
121
    /**
122
     * Get the classes in hierarchy to see if it's valid
123
     *
124
     * @param string $index Index to check classes for
125
     * @param array $classes Classes to get hierarchy for
126
     * @return array
127
     * @throws ReflectionException
128
     */
129 35
    protected function getClassesInHierarchy($index, array $classes): array
130
    {
131 35
        $indexClasses = singleton($index)->getClasses();
132 35
        foreach ($indexClasses as $class) {
133 35
            $classes = array_merge($classes, FieldResolver::getHierarchy($class));
134
        }
135
136 35
        return $classes;
137
    }
138
139
    /**
140
     * Get the client
141
     *
142
     * @return Client
143
     */
144 1
    public function getClient(): Client
145
    {
146 1
        return $this->client;
147
    }
148
149
    /**
150
     * Set the client
151
     *
152
     * @param Client $client
153
     * @return self
154
     */
155 1
    public function setClient($client): self
156
    {
157 1
        $this->client = $client;
158
159 1
        return $this;
160
    }
161
162
    /**
163
     * Get the admin query
164
     *
165
     * @return Query
166
     */
167 1
    public function getAdmin(): Query
168
    {
169 1
        return $this->admin;
170
    }
171
172
    /**
173
     * Set a (custom) admin query object
174
     *
175
     * @param Query $admin
176
     */
177 1
    public function setAdmin(Query $admin): void
178
    {
179 1
        $this->admin = $admin;
180 1
    }
181
}
182