BaseService   A
last analyzed

Complexity

Total Complexity 12

Size/Duplication

Total Lines 89
Duplicated Lines 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 24
dl 0
loc 89
rs 10
c 2
b 0
f 0
wmc 12

5 Methods

Rating   Name   Duplication   Size   Complexity  
A filterIndexes() 0 13 5
A __construct() 0 4 1
A setValidIndexes() 0 3 1
A getValidIndexes() 0 12 4
A checkReflection() 0 5 1
1
<?php
2
3
namespace Firesphere\SearchBackend\Services;
4
5
use LogicException;
6
use ReflectionClass;
7
use ReflectionException;
8
use SilverStripe\Core\ClassInfo;
9
use SilverStripe\Core\Config\Configurable;
10
11
class BaseService
12
{
13
    use Configurable;
14
15
    /**
16
     * Unique ID in Search engine
17
     */
18
    public const ID_FIELD = 'id';
19
    /**
20
     * SilverStripe ID of the object
21
     */
22
    public const CLASS_ID_FIELD = 'ObjectID';
23
    /**
24
     * Name of the field that can be used for queries
25
     */
26
    public const CLASSNAME = 'ClassName';
27
28
    /**
29
     * @var array Base indexes that exist
30
     */
31
    protected $baseIndexes = [];
32
    /**
33
     * @var array Valid indexes out of the base indexes
34
     */
35
    protected $validIndexes = [];
36
37
    /**
38
     * @throws ReflectionException
39
     */
40
    public function __construct($baseIndexClass)
41
    {
42
        $this->baseIndexes = ClassInfo::subclassesFor($baseIndexClass);
43
        $this->filterIndexes();
44
    }
45
46
    /**
47
     * Filter enabled indexes down to valid indexes that can be instantiated
48
     * or are allowed from config
49
     *
50
     * @throws ReflectionException
51
     */
52
    protected function filterIndexes(): void
53
    {
54
        $enabledIndexes = static::config()->get('indexes');
55
        $enabledIndexes = is_array($enabledIndexes) ? $enabledIndexes : $this->baseIndexes;
56
        foreach ($this->baseIndexes as $subindex) {
57
            // If the config of indexes is set, and the requested index isn't in it, skip addition
58
            // Or, the index simply doesn't exist, also a valid option
59
            if (!in_array($subindex, $enabledIndexes, true) ||
60
                !$this->checkReflection($subindex)
61
            ) {
62
                continue;
63
            }
64
            $this->validIndexes[] = $subindex;
65
        }
66
    }
67
68
69
    /**
70
     * Check if the class is instantiable
71
     *
72
     * @param $subindex
73
     * @return bool
74
     * @throws ReflectionException
75
     */
76
    protected function checkReflection($subindex): bool
77
    {
78
        $reflectionClass = new ReflectionClass($subindex);
79
80
        return $reflectionClass->isInstantiable();
81
    }
82
83
    public function getValidIndexes($index = null): array
84
    {
85
        if ($index && !in_array($index, $this->validIndexes, true)) {
86
            throw new LogicException('Incorrect index ' . $index);
87
        }
88
89
        if ($index) {
90
            return [$index];
91
        }
92
93
        // return the array values, to reset the keys
94
        return array_values($this->validIndexes);
95
    }
96
97
    public function setValidIndexes(array $validIndexes): void
98
    {
99
        $this->validIndexes = $validIndexes;
100
    }
101
}
102