Total Complexity | 69 |
Total Lines | 428 |
Duplicated Lines | 0 % |
Changes | 3 | ||
Bugs | 0 | Features | 0 |
Complex classes like Index often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use Index, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
17 | class Index |
||
18 | { |
||
19 | protected $client; |
||
20 | protected $index; |
||
21 | protected $cluster; |
||
22 | |||
23 | public function __construct(Client $client, $index = null) |
||
30 | } |
||
31 | |||
32 | public function search($input): Search |
||
37 | } |
||
38 | |||
39 | public function getDocumentById($id) |
||
40 | { |
||
41 | static::checkDocumentId($id); |
||
42 | $params = [ |
||
43 | 'body' => [ |
||
44 | 'index' => $this->index, |
||
45 | 'query' => [ |
||
46 | 'equals' => ['id' => $id] |
||
47 | ] |
||
48 | ] |
||
49 | ]; |
||
50 | $result = new ResultSet($this->client->search($params, true)); |
||
51 | return $result->valid() ? $result->current() : null; |
||
52 | } |
||
53 | |||
54 | public function getDocumentByIds($ids) |
||
55 | { |
||
56 | if (!is_array($ids)) { |
||
57 | $ids = [$ids]; |
||
58 | } |
||
59 | array_walk($ids, [static::class, 'checkDocumentId']); |
||
60 | $params = [ |
||
61 | 'body' => [ |
||
62 | 'index' => $this->index, |
||
63 | 'query' => [ |
||
64 | 'in' => ['id' => $ids] |
||
65 | ] |
||
66 | ] |
||
67 | ]; |
||
68 | return new ResultSet($this->client->search($params, true)); |
||
69 | } |
||
70 | |||
71 | public function addDocument($data, $id = 0) |
||
72 | { |
||
73 | static::checkDocumentId($id); |
||
74 | if (is_object($data)) { |
||
75 | $data = (array) $data; |
||
76 | } elseif (is_string($data)) { |
||
77 | $data = json_decode($data, true); |
||
78 | } |
||
79 | $params = [ |
||
80 | 'body' => [ |
||
81 | 'index' => $this->index, |
||
82 | 'id' => $id, |
||
83 | 'doc' => $data |
||
84 | ] |
||
85 | ]; |
||
86 | |||
87 | if ($this->cluster !== null) { |
||
88 | $params['body']['cluster'] = $this->cluster; |
||
89 | } |
||
90 | return $this->client->insert($params); |
||
91 | } |
||
92 | |||
93 | public function addDocuments($documents) |
||
94 | { |
||
95 | $toinsert = []; |
||
96 | foreach ($documents as $document) { |
||
97 | if (is_object($document)) { |
||
98 | $document = (array) $document; |
||
99 | } elseif (is_string($document)) { |
||
100 | $document = json_decode($document, true); |
||
101 | } |
||
102 | if (isset($document['id'])) { |
||
103 | $id = $document['id']; |
||
104 | static::checkDocumentId($id); |
||
105 | unset($document['id']); |
||
106 | } else { |
||
107 | $id = 0; |
||
108 | } |
||
109 | $insert = [ |
||
110 | 'index' => $this->index, |
||
111 | 'id' => $id, |
||
112 | 'doc' => $document |
||
113 | ]; |
||
114 | if ($this->cluster !== null) { |
||
115 | $insert['cluster'] = $this->cluster; |
||
116 | } |
||
117 | $toinsert[] = ['insert' => $insert]; |
||
118 | } |
||
119 | return $this->client->bulk(['body' => $toinsert]); |
||
120 | } |
||
121 | |||
122 | public function deleteDocument($id) |
||
135 | } |
||
136 | |||
137 | public function deleteDocumentsByIds(array $ids) |
||
138 | { |
||
139 | array_walk($ids, 'self::checkDocumentId'); |
||
140 | $params = [ |
||
141 | 'body' => [ |
||
142 | 'index' => $this->index, |
||
143 | 'id' => $ids |
||
144 | ] |
||
145 | ]; |
||
146 | if ($this->cluster !== null) { |
||
147 | $params['body']['cluster'] = $this->cluster; |
||
148 | } |
||
149 | return $this->client->delete($params); |
||
150 | } |
||
151 | |||
152 | public function deleteDocuments($query) |
||
153 | { |
||
154 | if ($query instanceof Query) { |
||
155 | $query = $query->toArray(); |
||
156 | } |
||
157 | $params = [ |
||
158 | 'body' => [ |
||
159 | 'index' => $this->index, |
||
160 | 'query' => $query |
||
161 | ] |
||
162 | ]; |
||
163 | if ($this->cluster !== null) { |
||
164 | $params['body']['cluster'] = $this->cluster; |
||
165 | } |
||
166 | return $this->client->delete($params); |
||
167 | } |
||
168 | |||
169 | public function updateDocument($data, $id) |
||
170 | { |
||
171 | static::checkDocumentId($id); |
||
172 | $params = [ |
||
173 | 'body' => [ |
||
174 | 'index' => $this->index, |
||
175 | 'id' => $id, |
||
176 | 'doc' => $data |
||
177 | ] |
||
178 | ]; |
||
179 | if ($this->cluster !== null) { |
||
180 | $params['body']['cluster'] = $this->cluster; |
||
181 | } |
||
182 | return $this->client->update($params); |
||
183 | } |
||
184 | |||
185 | public function updateDocuments($data, $query) |
||
186 | { |
||
187 | if ($query instanceof Query) { |
||
188 | $query = $query->toArray(); |
||
189 | } |
||
190 | $params = [ |
||
191 | 'body' => [ |
||
192 | 'index' => $this->index, |
||
193 | 'query' => $query, |
||
194 | 'doc' => $data |
||
195 | ] |
||
196 | ]; |
||
197 | if ($this->cluster !== null) { |
||
198 | $params['body']['cluster'] = $this->cluster; |
||
199 | } |
||
200 | return $this->client->update($params); |
||
201 | } |
||
202 | |||
203 | public function replaceDocument($data, $id) |
||
204 | { |
||
205 | static::checkDocumentId($id); |
||
206 | if (is_object($data)) { |
||
207 | $data = (array) $data; |
||
208 | } elseif (is_string($data)) { |
||
209 | $data = json_decode($data, true); |
||
210 | } |
||
211 | $params = [ |
||
212 | 'body' => [ |
||
213 | 'index' => $this->index, |
||
214 | 'id' => $id, |
||
215 | 'doc' => $data |
||
216 | ] |
||
217 | ]; |
||
218 | if ($this->cluster !== null) { |
||
219 | $params['body']['cluster'] = $this->cluster; |
||
220 | } |
||
221 | return $this->client->replace($params); |
||
222 | } |
||
223 | |||
224 | public function replaceDocuments($documents) |
||
225 | { |
||
226 | $toreplace = []; |
||
227 | foreach ($documents as $document) { |
||
228 | if (is_object($document)) { |
||
229 | $document = (array) $document; |
||
230 | } elseif (is_string($document)) { |
||
231 | $document = json_decode($document, true); |
||
232 | } |
||
233 | $id = $document['id']; |
||
234 | static::checkDocumentId($id); |
||
235 | unset($document['id']); |
||
236 | $replace = [ |
||
237 | 'index' => $this->index, |
||
238 | 'id' => $id, |
||
239 | 'doc' => $document |
||
240 | ]; |
||
241 | if ($this->cluster !== null) { |
||
242 | $replace['cluster'] = $this->cluster; |
||
243 | } |
||
244 | $toreplace[] = ['replace' => $replace]; |
||
245 | } |
||
246 | return $this->client->bulk(['body' => $toreplace]); |
||
247 | } |
||
248 | |||
249 | public function create($fields, $settings = [], $silent = false) |
||
250 | { |
||
251 | $params = [ |
||
252 | 'index' => $this->index, |
||
253 | 'body' => [ |
||
254 | 'columns' => $fields, |
||
255 | 'settings' => $settings |
||
256 | ] |
||
257 | ]; |
||
258 | if ($silent === true) { |
||
259 | $params['body']['silent'] = true; |
||
260 | } |
||
261 | return $this->client->indices()->create($params); |
||
262 | } |
||
263 | |||
264 | public function drop($silent = false) |
||
265 | { |
||
266 | $params = [ |
||
267 | 'index' => $this->index, |
||
268 | ]; |
||
269 | if ($silent === true) { |
||
270 | $params['body'] = ['silent' => true]; |
||
271 | } |
||
272 | return $this->client->indices()->drop($params); |
||
273 | } |
||
274 | |||
275 | public function describe() |
||
276 | { |
||
277 | $params = [ |
||
278 | 'index' => $this->index, |
||
279 | ]; |
||
280 | return $this->client->indices()->describe($params); |
||
281 | } |
||
282 | |||
283 | public function status() |
||
284 | { |
||
285 | $params = [ |
||
286 | 'index' => $this->index, |
||
287 | ]; |
||
288 | return $this->client->indices()->status($params); |
||
289 | } |
||
290 | |||
291 | public function truncate() |
||
297 | } |
||
298 | |||
299 | public function optimize($sync = false) |
||
308 | } |
||
309 | |||
310 | public function flush() |
||
311 | { |
||
312 | $params = [ |
||
313 | 'index' => $this->index, |
||
314 | ]; |
||
315 | $this->client->indices()->flushrtindex($params); |
||
316 | } |
||
317 | |||
318 | public function flushramchunk() |
||
319 | { |
||
320 | $params = [ |
||
321 | 'index' => $this->index, |
||
322 | ]; |
||
323 | $this->client->indices()->flushramchunk($params); |
||
324 | } |
||
325 | |||
326 | public function alter($operation, $name, $type = null) |
||
327 | { |
||
328 | if ($operation === 'add') { |
||
329 | $params = [ |
||
330 | 'index' => $this->index, |
||
331 | 'body' => [ |
||
332 | 'operation' => 'add', |
||
333 | 'column' => ['name' => $name, 'type' => $type] |
||
334 | ] |
||
335 | ]; |
||
336 | } elseif ($operation === 'drop') { |
||
337 | $params = [ |
||
338 | 'index' => $this->index, |
||
339 | 'body' => [ |
||
340 | 'operation' => 'drop', |
||
341 | 'column' => ['name' => $name] |
||
342 | ] |
||
343 | ]; |
||
344 | } else { |
||
345 | throw new RuntimeException('Alter operation not recognized'); |
||
346 | } |
||
347 | return $this->client->indices()->alter($params); |
||
348 | } |
||
349 | |||
350 | public function keywords($query, $options) |
||
351 | { |
||
352 | $params = [ |
||
353 | 'index' => $this->index, |
||
354 | 'body' => [ |
||
355 | 'query' => $query, |
||
356 | 'options' => $options |
||
357 | ] |
||
358 | ]; |
||
359 | return $this->client->keywords($params); |
||
360 | } |
||
361 | |||
362 | public function suggest($query, $options) |
||
363 | { |
||
364 | $params = [ |
||
365 | 'index' => $this->index, |
||
366 | 'body' => [ |
||
367 | 'query' => $query, |
||
368 | 'options' => $options |
||
369 | ] |
||
370 | ]; |
||
371 | return $this->client->suggest($params); |
||
372 | } |
||
373 | |||
374 | public function explainQuery($query) |
||
375 | { |
||
376 | $params = [ |
||
377 | 'index' => $this->index, |
||
378 | 'body' => [ |
||
379 | 'query' => $query, |
||
380 | ] |
||
381 | ]; |
||
382 | return $this->client->explainQuery($params); |
||
383 | } |
||
384 | |||
385 | |||
386 | public function percolate($docs) |
||
399 | } |
||
400 | |||
401 | public function percolateToDocs($docs) |
||
402 | { |
||
403 | $params = ['index' => $this->index, 'body' => []]; |
||
404 | if ($docs instanceof Percolate) { |
||
405 | $params['body']['query'] = $docs->toArray(); |
||
406 | } else { |
||
407 | if (isset($docs[0]) && is_array($docs[0])) { |
||
408 | $params['body']['query'] = ['percolate' => ['documents' => $docs]]; |
||
409 | } else { |
||
410 | $params['body']['query'] = ['percolate' => ['document' => $docs]]; |
||
411 | } |
||
412 | } |
||
413 | return new Results\PercolateDocsResultSet($this->client->pq()->search($params, true), $docs); |
||
414 | } |
||
415 | |||
416 | |||
417 | public function getClient(): Client |
||
418 | { |
||
419 | return $this->client; |
||
420 | } |
||
421 | |||
422 | public function getName(): string |
||
423 | { |
||
424 | return $this->index; |
||
425 | } |
||
426 | |||
427 | public function setName($index): self |
||
428 | { |
||
429 | $this->index = $index; |
||
430 | return $this; |
||
431 | } |
||
432 | |||
433 | public function setCluster($cluster): self |
||
434 | { |
||
435 | $this->cluster = $cluster; |
||
436 | return $this; |
||
437 | } |
||
438 | |||
439 | protected static function checkDocumentId(&$id) |
||
445 | } |
||
446 | } |
||
447 |