Passed
Push — hans/Index-all-fluent-options ( 53088a )
by Simon
05:29
created

DocumentFactory::buildFields()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 9
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

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