Passed
Push — hans/its-the-same ( 0a6987...73cfcc )
by Simon
04:13
created

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