DocumentCoreFactory   A
last analyzed

Complexity

Total Complexity 19

Size/Duplication

Total Lines 202
Duplicated Lines 0 %

Importance

Changes 3
Bugs 0 Features 0
Metric Value
eloc 41
dl 0
loc 202
rs 10
c 3
b 0
f 0
wmc 19

14 Methods

Rating   Name   Duplication   Size   Complexity  
A getFieldResolver() 0 3 1
A setFieldResolver() 0 3 1
A __construct() 0 5 1
A setDebug() 0 5 1
A isDebug() 0 3 1
A getValuesForField() 0 11 2
A classEquals() 0 3 2
A setItems() 0 3 1
A setClass() 0 3 1
A indexGroupMessage() 0 10 1
A getShortFieldName() 0 5 1
A classIs() 0 11 4
A getItems() 0 3 1
A getClass() 0 3 1
1
<?php
2
3
namespace Firesphere\SearchBackend\Factories;
4
5
use Exception;
6
use Firesphere\SearchBackend\Helpers\DataResolver;
7
use Firesphere\SearchBackend\Helpers\FieldResolver;
8
use Firesphere\SearchBackend\Indexes\CoreIndex;
9
use Firesphere\SearchBackend\Traits\LoggerTrait;
10
use Firesphere\SolrSearch\Indexes\BaseIndex as SolrBaseIndex;
0 ignored issues
show
Bug introduced by
The type Firesphere\SolrSearch\Indexes\BaseIndex was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
11
use Psr\Container\NotFoundExceptionInterface;
12
use SilverStripe\Core\Config\Configurable;
13
use SilverStripe\Core\Extensible;
14
use SilverStripe\Core\Injector\Injector;
15
use SilverStripe\ORM\DataObject;
16
use SilverStripe\ORM\SS_List;
17
use SilverStripe\ORM\UniqueKey\UniqueKeyInterface;
18
19
/**
20
 * Class DocumentFactory
21
 * Factory to create documents to be pushed to Solr
22
 *
23
 * @package Firesphere\Search\Backend
24
 */
25
abstract class DocumentCoreFactory
26
{
27
    use Extensible;
28
    use Configurable;
29
    use LoggerTrait;
30
31
    /**
32
     * @var string Name of the class being indexed
33
     */
34
    protected $class;
35
36
    /**
37
     * @var SS_List Items that need to be manufactured into documents
38
     */
39
    protected $items;
40
41
    /**
42
     * @var FieldResolver
43
     */
44
    protected $fieldResolver;
45
46
    /**
47
     * Set debug mode on or off
48
     * @var bool
49
     */
50
    protected $debug;
51
    /**
52
     * @var UniqueKeyInterface
53
     */
54
    protected $keyService;
55
56
    /**
57
     * DocumentFactory constructor, sets up the field resolver
58
     * @throws NotFoundExceptionInterface
59
     */
60
    public function __construct()
61
    {
62
        $this->fieldResolver = Injector::inst()->get(FieldResolver::class);
63
        $this->keyService = Injector::inst()->get(UniqueKeyInterface::class);
64
        $this->logger = $this->getLogger();
65
    }
66
67
    /**
68
     * Note, it can only take one type of class at a time!
69
     * So make sure you properly loop and set $class
70
     *
71
     * @param array $fields Fields to index
72
     * @param SolrBaseIndex $index Index to push the documents to
73
     * @return array Documents to be pushed
74
     * @throws Exception
75
     */
76
    abstract public function buildItems($fields, $index, $update = null): array;
77
78
    /**
79
     * Are we debugging?
80
     *
81
     * @return bool
82
     */
83
    public function isDebug(): bool
84
    {
85
        return (bool)$this->debug;
86
    }
87
88
    /**
89
     * Set to true if debugging should be enabled
90
     *
91
     * @param bool $debug
92
     * @return DocumentCoreFactory
93
     */
94
    public function setDebug(bool $debug): DocumentCoreFactory
95
    {
96
        $this->debug = $debug;
97
98
        return $this;
99
    }
100
101
    /**
102
     * @return mixed|object|Injector
103
     */
104
    public function getFieldResolver(): mixed
105
    {
106
        return $this->fieldResolver;
107
    }
108
109
    /**
110
     * @param mixed|object|Injector $fieldResolver
111
     */
112
    public function setFieldResolver(mixed $fieldResolver): void
113
    {
114
        $this->fieldResolver = $fieldResolver;
115
    }
116
117
    abstract protected function addDefaultFields($doc, $item);
118
119
    /**
120
     * Show the message about what is being indexed
121
     *
122
     * @param CoreIndex $index
123
     * @throws NotFoundExceptionInterface
124
     */
125
    protected function indexGroupMessage($index): void
126
    {
127
        $debugString = sprintf(
128
            'Indexing %s on %s (%s items)%s',
129
            $this->getClass(),
130
            $index->getIndexName(),
131
            $this->getItems()->count(),
132
            PHP_EOL
133
        );
134
        $this->getLogger()->info($debugString);
135
    }
136
137
    /**
138
     * @return string
139
     */
140
    public function getClass()
141
    {
142
        return $this->class;
143
    }
144
145
    /**
146
     * @param mixed $class
147
     */
148
    public function setClass($class): void
149
    {
150
        $this->class = $class;
151
    }
152
153
    /**
154
     * @return mixed
155
     */
156
    public function getItems()
157
    {
158
        return $this->items;
159
    }
160
161
    /**
162
     * @param mixed $items
163
     */
164
    public function setItems($items): void
165
    {
166
        $this->items = $items;
167
    }
168
169
    /**
170
     * Determine if the given object is one of the given type
171
     *
172
     * @param string|DataObject $class Class to compare
173
     * @param array|string $base Class or list of base classes
174
     * @return bool
175
     */
176
    protected function classIs($class, $base): bool
177
    {
178
        $base = is_array($base) ? $base : [$base];
179
180
        foreach ($base as $nextBase) {
181
            if ($this->classEquals($class, $nextBase)) {
182
                return true;
183
            }
184
        }
185
186
        return false;
187
    }
188
189
    /**
190
     * Check if a base class is an instance of the expected base group
191
     *
192
     * @param string|DataObject $class Class to compare
193
     * @param string $base Base class
194
     * @return bool
195
     */
196
    protected function classEquals($class, $base): bool
197
    {
198
        return $class === $base || ($class instanceof $base);
199
    }
200
201
    /**
202
     * Use the DataResolver to find the value(s) for a field.
203
     * Returns an array of values, and if it's multiple, it becomes a long array
204
     *
205
     * @param DataObject $object Object to resolve
206
     * @param array $options Customised options
207
     * @return array
208
     */
209
    protected function getValuesForField($object, $options): array
210
    {
211
        try {
212
            $valuesForField = [DataResolver::identify($object, $options['fullfield'])];
213
        } catch (Exception $error) {
214
            // @codeCoverageIgnoreStart
215
            $valuesForField = [];
216
            // @codeCoverageIgnoreEnd
217
        }
218
219
        return $valuesForField;
220
    }
221
222
    protected function getShortFieldName($name)
223
    {
224
        $name = explode('\\', $name);
225
226
        return end($name);
227
    }
228
}
229