Passed
Push — hans/state-improvements ( 5f18c2...259e4b )
by Simon
06:57
created

DocumentFactory::getValuesForField()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 5
c 1
b 0
f 0
nc 2
nop 2
dl 0
loc 9
rs 10
1
<?php
2
3
4
namespace Firesphere\SolrSearch\Factories;
5
6
use Exception;
7
use Firesphere\SolrSearch\Extensions\DataObjectExtension;
8
use Firesphere\SolrSearch\Helpers\DataResolver;
9
use Firesphere\SolrSearch\Helpers\FieldResolver;
10
use Firesphere\SolrSearch\Helpers\Statics;
11
use Firesphere\SolrSearch\Indexes\BaseIndex;
12
use Firesphere\SolrSearch\Services\SolrCoreService;
13
use Firesphere\SolrSearch\Traits\DocumentFactoryTrait;
14
use Firesphere\SolrSearch\Traits\LoggerTrait;
15
use SilverStripe\Core\ClassInfo;
16
use SilverStripe\Core\Config\Configurable;
17
use SilverStripe\Core\Extensible;
18
use SilverStripe\Core\Injector\Injector;
19
use SilverStripe\ORM\DataObject;
20
use SilverStripe\ORM\FieldType\DBDate;
21
use SilverStripe\ORM\FieldType\DBField;
22
use Solarium\QueryType\Update\Query\Document;
23
use Solarium\QueryType\Update\Query\Query;
24
25
/**
26
 * Class DocumentFactory
27
 * Factory to create documents to be pushed to Solr
28
 *
29
 * @package Firesphere\SolrSearch\Factories
30
 */
31
class DocumentFactory
32
{
33
    use Configurable;
34
    use Extensible;
35
    use DocumentFactoryTrait;
36
    use LoggerTrait;
37
38
    /**
39
     * Numeral types in Solr
40
     *
41
     * @var array
42
     */
43
    protected static $numerals = [
44
        'tint',
45
        'tfloat',
46
        'tdouble',
47
    ];
48
    /**
49
     * Debug this build
50
     *
51
     * @var bool
52
     */
53
    protected $debug = false;
54
55
    /**
56
     * DocumentFactory constructor, sets up the field resolver
57
     */
58
    public function __construct()
59
    {
60
        $this->fieldResolver = Injector::inst()->get(FieldResolver::class);
61
    }
62
63
    /**
64
     * Note, it can only take one type of class at a time!
65
     * So make sure you properly loop and set $class
66
     *
67
     * @param $fields
68
     * @param BaseIndex $index
69
     * @param Query $update
70
     * @return array
71
     * @throws Exception
72
     */
73
    public function buildItems($fields, $index, $update): array
74
    {
75
        $class = $this->getClass();
76
        $this->getFieldResolver()->setIndex($index);
77
        $boostFields = $index->getBoostedFields();
78
        $docs = [];
79
        $debugString = sprintf('Adding %s to %s%s', $class, $index->getIndexName(), PHP_EOL);
80
        $debugString .= '[';
81
        foreach ($this->getItems() as $item) {
82
            if ($item->ShowInSearch === 0) {
83
                continue;
84
            }
85
            $debugString .= "$item->ID, ";
86
            /** @var Document $doc */
87
            $doc = $update->createDocument();
88
            $this->addDefaultFields($doc, $item);
89
90
            $this->buildFields($fields, $doc, $item, $boostFields);
91
            $item->destroy();
92
93
            $docs[] = $doc;
94
        }
95
96
        if ($this->debug) {
97
            $this->getLogger()->info(rtrim($debugString, ', ') . ']' . PHP_EOL);
98
        }
99
100
        return $docs;
101
    }
102
103
    /**
104
     * Add fields that should always be included
105
     *
106
     * @param Document $doc
107
     * @param DataObject|DataObjectExtension $item
108
     */
109
    protected function addDefaultFields(Document $doc, DataObject $item)
110
    {
111
        $doc->setKey(SolrCoreService::ID_FIELD, $item->ClassName . '-' . $item->ID);
112
        $doc->addField(SolrCoreService::CLASS_ID_FIELD, $item->ID);
113
        $doc->addField('ClassName', $item->ClassName);
114
        $doc->addField('ClassHierarchy', ClassInfo::ancestry($item));
115
        $doc->addField('ViewStatus', $item->getViewStatus());
116
    }
117
118
    /**
119
     * Create the required record for a field
120
     *
121
     * @param $fields
122
     * @param Document $doc
123
     * @param DataObject $item
124
     * @param array $boostFields
125
     * @throws Exception
126
     */
127
    protected function buildFields($fields, Document $doc, DataObject $item, array $boostFields): void
128
    {
129
        foreach ($fields as $field) {
130
            $fieldData = $this->getFieldResolver()->resolveField($field);
131
            foreach ($fieldData as $dataField => $options) {
132
                $options['boost'] = $boostFields[$field] ?? null;
133
                $this->addField($doc, $item, $options);
134
            }
135
        }
136
    }
137
138
    /**
139
     * Add a single field to the Solr index
140
     *
141
     * @param Document $doc
142
     * @param DataObject $object
143
     * @param array $options
144
     */
145
    protected function addField($doc, $object, $options): void
146
    {
147
        if (!$this->classIs($object, $options['origin'])) {
148
            return;
149
        }
150
151
        $this->extend('onBeforeAddField', $options);
152
153
        $valuesForField = $this->getValuesForField($object, $options);
154
155
        $typeMap = Statics::getTypeMap();
156
        $type = $typeMap[$options['type']] ?? $typeMap['*'];
157
158
        foreach ($valuesForField as $value) {
159
            if ($value === null) {
160
                continue;
161
            }
162
            $this->extend('onBeforeAddDoc', $options, $value);
163
            $this->addToDoc($doc, $options, $type, $value);
164
        }
165
    }
166
167
    /**
168
     * Determine if the given object is one of the given type
169
     *
170
     * @param string|DataObject $class
171
     * @param array|string $base Class or list of base classes
172
     * @return bool
173
     * @todo remove in favour of the inheritance check from PHP
174
     */
175
    protected function classIs($class, $base): bool
176
    {
177
        $base = is_array($base) ? $base : [$base];
178
179
        foreach ($base as $nextBase) {
180
            if ($this->classEquals($class, $nextBase)) {
181
                return true;
182
            }
183
        }
184
185
        return false;
186
    }
187
188
    /**
189
     * Check if a base class is an instance of the expected base group
190
     *
191
     * @param string|DataObject $class
192
     * @param string $base
193
     * @return bool
194
     */
195
    protected function classEquals($class, $base): bool
196
    {
197
        return $class === $base || ($class instanceof $base);
198
    }
199
200
    /**
201
     * Push field to a document
202
     *
203
     * @param Document $doc
204
     * @param array $options
205
     * @param string $type
206
     * @param DBField|string $value
207
     */
208
    protected function addToDoc($doc, $options, $type, $value): void
209
    {
210
        /* Solr requires dates in the form 1995-12-31T23:59:59Z, so we need to normalize to GMT */
211
        if ($type === 'tdate' || $value instanceof DBDate) {
212
            $value = gmdate('Y-m-d\TH:i:s\Z', strtotime($value));
213
        }
214
215
        $name = getShortFieldName($options['name']);
216
217
        $doc->addField($name, $value, $options['boost'], Document::MODIFIER_SET);
218
    }
219
220
    /**
221
     * Are we debugging?
222
     *
223
     * @return bool
224
     */
225
    public function isDebug(): bool
226
    {
227
        return $this->debug;
228
    }
229
230
    /**
231
     * Set to true if debugging should be enabled
232
     *
233
     * @param bool $debug
234
     * @return DocumentFactory
235
     */
236
    public function setDebug(bool $debug): DocumentFactory
237
    {
238
        $this->debug = $debug;
239
240
        return $this;
241
    }
242
243
    /**
244
     * Use the DataResolver to find the value(s) for a field.
245
     * Returns an array of values, and if it's multiple, it becomes a long array
246
     *
247
     * @param $object
248
     * @param $options
249
     * @return array
250
     */
251
    protected function getValuesForField($object, $options): array
252
    {
253
        try {
254
            $valuesForField = [DataResolver::identify($object, $options['field'])];
255
        } catch (Exception $e) {
256
            $valuesForField = [];
257
        }
258
259
        return $valuesForField;
260
    }
261
}
262