Passed
Push — hans/core-extraction ( ed2510...fc7dca )
by Simon
10:29 queued 08:21
created

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