Passed
Push — sheepy/introspection ( 17b395...901708 )
by Simon
08:33 queued 05:46
created

BaseIndexTrait   A

Complexity

Total Complexity 29

Size/Duplication

Total Lines 281
Duplicated Lines 0 %

Test Coverage

Coverage 94.59%

Importance

Changes 4
Bugs 1 Features 0
Metric Value
wmc 29
eloc 65
c 4
b 1
f 0
dl 0
loc 281
ccs 70
cts 74
cp 0.9459
rs 10

20 Methods

Rating   Name   Duplication   Size   Complexity  
A getCopyFields() 0 3 1
A setSortFields() 0 5 1
A getFilterFields() 0 3 1
A setFilterFields() 0 5 1
A addFacetField() 0 9 2
A getDefaultField() 0 3 1
A addSortField() 0 12 3
A addFilterField() 0 8 2
A getFulltextFields() 0 5 1
A setFulltextFields() 0 5 1
A addFulltextField() 0 21 5
A setDefaultField() 0 5 1
A addCopyField() 0 9 2
A getSortFields() 0 3 1
A setCopyFields() 0 5 1
A getClient() 0 3 1
A setStoredFields() 0 5 1
A addStoredField() 0 6 1
A getStoredFields() 0 3 1
A setClient() 0 5 1
1
<?php
2
3
4
namespace Firesphere\SolrSearch\Traits;
5
6
use Firesphere\SolrSearch\Indexes\BaseIndex;
7
use SilverStripe\Dev\Deprecation;
8
use Solarium\Core\Client\Client;
9
10
/**
11
 * This is slightly cheating, but it works and also makes things more readable.
12
 *
13
 * Trait BaseIndexTrait
14
 * @package Firesphere\SolrSearch\Traits
15
 */
16
trait BaseIndexTrait
17
{
18
    /**
19
     * @var Client
20
     */
21
    protected $client;
22
    /**
23
     * @var array
24
     */
25
    protected $facetFields = [];
26
    /**
27
     * @var array
28
     */
29
    protected $fulltextFields = [];
30
    /**
31
     * @var array
32
     */
33
    protected $filterFields = [];
34
    /**
35
     * @var array
36
     */
37
    protected $sortFields = [];
38
    /**
39
     * @var string
40
     */
41
    protected $defaultField = '_text';
42
    /**
43
     * @var array
44
     */
45
    protected $storedFields = [];
46
    /**
47
     * @var array
48
     */
49
    protected $copyFields = [
50
        '_text' => [
51
            '*'
52
        ],
53
    ];
54
55
    /**
56
     * @return array
57
     */
58 25
    public function getCopyFields(): array
59
    {
60 25
        return $this->copyFields;
61
    }
62
63
    /**
64
     * @param array $copyField
65
     * @return $this
66
     */
67 58
    public function setCopyFields($copyField): self
68
    {
69 58
        $this->copyFields = $copyField;
70
71 58
        return $this;
72
    }
73
74
    /**
75
     * @return string
76
     */
77 26
    public function getDefaultField(): string
78
    {
79 26
        return $this->defaultField;
80
    }
81
82
    /**
83
     * @param string $defaultField
84
     * @return $this
85
     */
86 58
    public function setDefaultField($defaultField): self
87
    {
88 58
        $this->defaultField = $defaultField;
89
90 58
        return $this;
91
    }
92
93
    /**
94
     * @param $sortField
95
     * @return $this
96
     */
97 59
    public function addSortField($sortField): self
98
    {
99 59
        if (!in_array($sortField, $this->getFulltextFields(), true) &&
100 59
            !in_array($sortField, $this->getFilterFields(), true)
101
        ) {
102
            $this->addFulltextField($sortField);
103
            $this->sortFields[] = $sortField;
104
        }
105
106 59
        $this->setSortFields(array_unique($this->getSortFields()));
107
108 59
        return $this;
109
    }
110
111
    /**
112
     * @return array
113
     */
114 59
    public function getFulltextFields(): array
115
    {
116 59
        return array_values(
117 59
            array_unique(
118 59
                $this->fulltextFields
119
            )
120
        );
121
    }
122
123
    /**
124
     * @param array $fulltextFields
125
     * @return $this
126
     */
127 58
    public function setFulltextFields($fulltextFields): self
128
    {
129 58
        $this->fulltextFields = $fulltextFields;
130
131 58
        return $this;
132
    }
133
134
    /**
135
     * @return array
136
     */
137 59
    public function getFilterFields(): array
138
    {
139 59
        return $this->filterFields;
140
    }
141
142
    /**
143
     * @param array $filterFields
144
     * @return $this
145
     */
146 58
    public function setFilterFields($filterFields): self
147
    {
148 58
        $this->filterFields = $filterFields;
149
150 58
        return $this;
151
    }
152
153
    /**
154
     * @param string $fulltextField
155
     * @param null|string $forceType
156
     * @param array $options
157
     * @return $this
158
     */
159 59
    public function addFulltextField($fulltextField, $forceType = null, $options = []): self
160
    {
161 59
        if ($forceType) {
162
            Deprecation::notice('5.0', 'ForceType should be handled through casting');
163
        }
164
165 59
        $key = array_search($fulltextField, $this->getFilterFields(), true);
166
167 59
        if (!$key) {
168 59
            $this->fulltextFields[] = $fulltextField;
169
        }
170
171 59
        if (isset($options['boost'])) {
172
            $this->addBoostedField($fulltextField, [], $options['boost']);
0 ignored issues
show
Bug introduced by
It seems like addBoostedField() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

172
            $this->/** @scrutinizer ignore-call */ 
173
                   addBoostedField($fulltextField, [], $options['boost']);
Loading history...
173
        }
174
175 59
        if (isset($options['stored'])) {
176 1
            $this->storedFields[] = $fulltextField;
177
        }
178
179 59
        return $this;
180
    }
181
182
    /**
183
     * @return array
184
     */
185 59
    public function getSortFields(): array
186
    {
187 59
        return $this->sortFields;
188
    }
189
190
    /**
191
     * @param array $sortFields
192
     * @return $this
193
     */
194 59
    public function setSortFields($sortFields): self
195
    {
196 59
        $this->sortFields = $sortFields;
197
198 59
        return $this;
199
    }
200
201
    /**
202
     * @param $field
203
     * @param array $options
204
     * @return $this
205
     */
206 1
    public function addFacetField($field, $options): self
207
    {
208 1
        $this->facetFields[$field] = $options;
209
210 1
        if (!in_array($field, $this->getFilterFields(), true)) {
211 1
            $this->addFilterField($field);
212
        }
213
214 1
        return $this;
215
    }
216
217
    /**
218
     * @param $filterField
219
     * @return $this
220
     */
221 59
    public function addFilterField($filterField): self
222
    {
223 59
        $key = array_search($filterField, $this->getFulltextFields(), true);
224 59
        if ($key === false) {
225 59
            $this->filterFields[] = $filterField;
226
        }
227
228 59
        return $this;
229
    }
230
231
    /**
232
     * @param string $field Name of the copyfield
233
     * @param array $options Array of all fields that should be copied to this copyfield
234
     * @return $this
235
     */
236 1
    public function addCopyField($field, $options): self
237
    {
238 1
        $this->copyFields[$field] = $options;
239
240 1
        if (!in_array($field, $this->getFulltextFields(), true)) {
241 1
            $this->addFulltextField($field);
242
        }
243
244 1
        return $this;
245
    }
246
247
    /**
248
     * @param string $field
249
     * @param null|string $forceType
250
     * @param array $extraOptions
251
     * @return BaseIndex
252
     */
253 1
    public function addStoredField($field, $forceType = null, $extraOptions = []): self
254
    {
255 1
        $options = array_merge($extraOptions, ['stored' => 'true']);
256 1
        $this->addFulltextField($field, $forceType, $options);
257
258 1
        return $this;
259
    }
260
261
    /**
262
     * @return Client
263
     */
264 27
    public function getClient()
265
    {
266 27
        return $this->client;
267
    }
268
269
    /**
270
     * @param Client $client
271
     * @return $this
272
     */
273 1
    public function setClient($client): self
274
    {
275 1
        $this->client = $client;
276
277 1
        return $this;
278
    }
279
280
    /**
281
     * @return array
282
     */
283 25
    public function getStoredFields(): array
284
    {
285 25
        return $this->storedFields;
286
    }
287
288
    /**
289
     * @param array $storedFields
290
     * @return BaseIndex
291
     */
292 1
    public function setStoredFields(array $storedFields): self
293
    {
294 1
        $this->storedFields = $storedFields;
295
296 1
        return $this;
297
    }
298
}
299