Passed
Push — hans/core-extraction ( a97b4e...a44af8 )
by Simon
06:16
created

CoreServiceTrait::getClassesInHierarchy()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 4
nc 2
nop 2
dl 0
loc 7
ccs 5
cts 5
cp 1
crap 2
rs 10
c 1
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
     * Ensure the getValidIndexes() method exists on all classes using this trait.
37
     *
38
     * @return mixed
39
     */
40
    abstract public function getValidIndexes();
41
    /**
42
     * Check if we are in debug mode
43
     *
44
     * @return bool
45
     */
46 6
    public function isInDebugMode(): bool
47
    {
48 6
        return $this->inDebugMode;
49
    }
50
51
    /**
52
     * Set the debug mode
53
     *
54
     * @param bool $inDebugMode
55
     * @return self
56
     */
57 3
    public function setInDebugMode(bool $inDebugMode): self
58
    {
59 3
        $this->inDebugMode = $inDebugMode;
60
61 3
        return $this;
62
    }
63
64
    /**
65
     * Is the given class a valid class to index
66
     * Does not discriminate against the indexes. All indexes are worth the same
67
     *
68
     * @param string $class
69
     * @return bool
70
     * @throws ReflectionException
71
     */
72 78
    public function isValidClass($class): bool
73
    {
74 78
        $classes = $this->getValidClasses();
75
76 78
        return in_array($class, $classes, true);
77
    }
78
79
    /**
80
     * Get all classes from all indexes and return them.
81
     * Used to get all classes that are to be indexed on change
82
     * Note, only base classes are in this object. A publish recursive is required
83
     * when any change from a relation is published.
84
     *
85
     * @return array
86
     * @throws ReflectionException
87
     */
88 78
    public function getValidClasses(): array
89
    {
90 78
        $indexes = $this->getValidIndexes();
91 78
        $classes = [];
92 78
        foreach ($indexes as $index) {
93 78
            $classes = $this->getClassesInHierarchy($index, $classes);
94
        }
95
96 78
        return array_unique($classes);
97
    }
98
99
    /**
100
     * Get the classes in hierarchy to see if it's valid
101
     *
102
     * @param $index
103
     * @param array $classes
104
     * @return array
105
     * @throws ReflectionException
106
     */
107 78
    protected function getClassesInHierarchy($index, array $classes): array
108
    {
109 78
        $indexClasses = singleton($index)->getClasses();
110 78
        foreach ($indexClasses as $class) {
111 78
            $classes = array_merge($classes, FieldResolver::getHierarchy($class, true));
112
        }
113 78
        return $classes;
114
    }
115
116
    /**
117
     * Get the client
118
     *
119
     * @return Client
120
     */
121 1
    public function getClient(): Client
122
    {
123 1
        return $this->client;
124
    }
125
126
    /**
127
     * Set the client
128
     *
129
     * @param Client $client
130
     * @return self
131
     */
132 1
    public function setClient($client): self
133
    {
134 1
        $this->client = $client;
135
136 1
        return $this;
137
    }
138
139
    /**
140
     * @return Query
141
     */
142 1
    public function getAdmin(): Query
143
    {
144 1
        return $this->admin;
145
    }
146
147
    /**
148
     * @param Query $admin
149
     */
150 1
    public function setAdmin(Query $admin): void
151
    {
152 1
        $this->admin = $admin;
153 1
    }
154
}
155