Total Complexity | 44 |
Total Lines | 325 |
Duplicated Lines | 0 % |
Changes | 1 | ||
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 | |||
22 | public function __construct(Client $client, $index = null) |
||
23 | { |
||
24 | $this->client = $client; |
||
25 | |||
26 | $this->index = $index; |
||
27 | } |
||
28 | |||
29 | public function search($input): Search |
||
30 | { |
||
31 | $search = new Search($this->client); |
||
32 | $search->setIndex($this->index); |
||
33 | return $search->search($input); |
||
34 | } |
||
35 | |||
36 | public function getDocumentById($id) |
||
37 | { |
||
38 | $params = [ |
||
39 | 'body' => [ |
||
40 | 'index' => $this->index, |
||
41 | 'query' => [ |
||
42 | 'equals' => ['id' => $id] |
||
43 | ] |
||
44 | ] |
||
45 | ]; |
||
46 | $result = new ResultSet($this->client->search($params, true)); |
||
47 | return $result->valid() ? $result->current() : null; |
||
48 | } |
||
49 | |||
50 | public function addDocument($data, $id = null) |
||
51 | { |
||
52 | $params = [ |
||
53 | 'body' => [ |
||
54 | 'index' => $this->index, |
||
55 | 'id' => $id, |
||
56 | 'doc' => $data |
||
57 | ] |
||
58 | ]; |
||
59 | return $this->client->insert($params); |
||
60 | } |
||
61 | |||
62 | public function addDocuments($documents) |
||
63 | { |
||
64 | $toinsert = []; |
||
65 | foreach ($documents as $document) { |
||
66 | $id = $document['id']; |
||
67 | unset($document['id']); |
||
68 | $toinsert[] = [ |
||
69 | 'insert' => [ |
||
70 | 'index' => $this->index, |
||
71 | 'id' => $id, |
||
72 | 'doc' => $document |
||
73 | ] |
||
74 | ]; |
||
75 | } |
||
76 | return $this->client->bulk(['body' => $toinsert]); |
||
77 | } |
||
78 | |||
79 | public function deleteDocument($id) |
||
80 | { |
||
81 | $params = [ |
||
82 | 'body' => [ |
||
83 | 'index' => $this->index, |
||
84 | 'id' => $id |
||
85 | ] |
||
86 | ]; |
||
87 | return $this->client->delete($params); |
||
88 | } |
||
89 | |||
90 | public function deleteDocuments($query) |
||
91 | { |
||
92 | if ($query instanceof Query) { |
||
93 | $query = $query->toArray(); |
||
94 | } |
||
95 | $params = [ |
||
96 | 'body' => [ |
||
97 | 'index' => $this->index, |
||
98 | 'query' => $query |
||
99 | ] |
||
100 | ]; |
||
101 | return $this->client->delete($params); |
||
102 | } |
||
103 | |||
104 | public function updateDocument($data, $id) |
||
105 | { |
||
106 | $params = [ |
||
107 | 'body' => [ |
||
108 | 'index' => $this->index, |
||
109 | 'id' => $id, |
||
110 | 'doc' => $data |
||
111 | ] |
||
112 | ]; |
||
113 | return $this->client->update($params); |
||
114 | } |
||
115 | |||
116 | public function updateDocuments($data, $query) |
||
117 | { |
||
118 | if ($query instanceof Query) { |
||
119 | $query = $query->toArray(); |
||
120 | } |
||
121 | $params = [ |
||
122 | 'body' => [ |
||
123 | 'index' => $this->index, |
||
124 | 'query' => $query, |
||
125 | 'doc' => $data |
||
126 | ] |
||
127 | ]; |
||
128 | return $this->client->update($params); |
||
129 | } |
||
130 | |||
131 | public function replaceDocument($data, $id) |
||
132 | { |
||
133 | $params = [ |
||
134 | 'body' => [ |
||
135 | 'index' => $this->index, |
||
136 | 'id' => $id, |
||
137 | 'doc' => $data |
||
138 | ] |
||
139 | ]; |
||
140 | return $this->client->replace($params); |
||
141 | } |
||
142 | |||
143 | public function replaceDocuments($documents) |
||
144 | { |
||
145 | $toreplace = []; |
||
146 | foreach ($documents as $document) { |
||
147 | $id = $document['id']; |
||
148 | unset($document['id']); |
||
149 | $toreplace[] = [ |
||
150 | 'replace' => [ |
||
151 | 'index' => $this->index, |
||
152 | 'id' => $id, |
||
153 | 'doc' => $document |
||
154 | ] |
||
155 | ]; |
||
156 | } |
||
157 | return $this->client->bulk(['body' => $toreplace]); |
||
158 | } |
||
159 | |||
160 | public function create($fields, $settings = [], $silent = false) |
||
161 | { |
||
162 | $params = [ |
||
163 | 'index' => $this->index, |
||
164 | 'body' => [ |
||
165 | 'columns' => $fields, |
||
166 | 'settings' => $settings |
||
167 | ] |
||
168 | ]; |
||
169 | if ($silent === true) { |
||
170 | $params['body']['silent'] = true; |
||
171 | } |
||
172 | return $this->client->indices()->create($params); |
||
173 | } |
||
174 | |||
175 | public function drop($silent = false) |
||
176 | { |
||
177 | $params = [ |
||
178 | 'index' => $this->index, |
||
179 | ]; |
||
180 | if ($silent === true) { |
||
181 | $params['body'] = ['silent' => true]; |
||
182 | } |
||
183 | return $this->client->indices()->drop($params); |
||
184 | } |
||
185 | |||
186 | public function describe() |
||
187 | { |
||
188 | $params = [ |
||
189 | 'index' => $this->index, |
||
190 | ]; |
||
191 | return $this->client->indices()->describe($params); |
||
192 | } |
||
193 | |||
194 | public function status() |
||
195 | { |
||
196 | $params = [ |
||
197 | 'index' => $this->index, |
||
198 | ]; |
||
199 | return $this->client->indices()->status($params); |
||
200 | } |
||
201 | |||
202 | public function truncate() |
||
203 | { |
||
204 | $params = [ |
||
205 | 'index' => $this->index, |
||
206 | ]; |
||
207 | return $this->client->indices()->truncate($params); |
||
208 | } |
||
209 | |||
210 | public function optimize($sync = false) |
||
211 | { |
||
212 | $params = [ |
||
213 | 'index' => $this->index, |
||
214 | ]; |
||
215 | if ($sync === true) { |
||
216 | $params['body'] = ['sync' => true]; |
||
217 | } |
||
218 | return $this->client->indices()->optimize($params); |
||
219 | } |
||
220 | |||
221 | public function flush() |
||
222 | { |
||
223 | $params = [ |
||
224 | 'index' => $this->index, |
||
225 | ]; |
||
226 | $this->client->indices()->flushrtindex($params); |
||
227 | } |
||
228 | |||
229 | public function flushramchunk() |
||
230 | { |
||
231 | $params = [ |
||
232 | 'index' => $this->index, |
||
233 | ]; |
||
234 | $this->client->indices()->flushramchunk($params); |
||
235 | } |
||
236 | |||
237 | public function alter($operation, $name, $type = null) |
||
238 | { |
||
239 | if ($operation === 'add') { |
||
240 | $params = [ |
||
241 | 'index' => $this->index, |
||
242 | 'body' => [ |
||
243 | 'operation' => 'add', |
||
244 | 'column' => ['name' => $name, 'type' => $type] |
||
245 | ] |
||
246 | ]; |
||
247 | } elseif ($operation === 'drop') { |
||
248 | $params = [ |
||
249 | 'index' => $this->index, |
||
250 | 'body' => [ |
||
251 | 'operation' => 'drop', |
||
252 | 'column' => ['name' => $name] |
||
253 | ] |
||
254 | ]; |
||
255 | } else { |
||
256 | throw new RuntimeException('Alter operation not recognized'); |
||
257 | } |
||
258 | return $this->client->indices()->alter($params); |
||
259 | } |
||
260 | |||
261 | public function keywords($query, $options) |
||
262 | { |
||
263 | $params = [ |
||
264 | 'index' => $this->index, |
||
265 | 'body' => [ |
||
266 | 'query' => $query, |
||
267 | 'options' => $options |
||
268 | ] |
||
269 | ]; |
||
270 | return $this->client->keywords($params); |
||
271 | } |
||
272 | |||
273 | public function suggest($query, $options) |
||
274 | { |
||
275 | $params = [ |
||
276 | 'index' => $this->index, |
||
277 | 'body' => [ |
||
278 | 'query' => $query, |
||
279 | 'options' => $options |
||
280 | ] |
||
281 | ]; |
||
282 | return $this->client->suggest($params); |
||
283 | } |
||
284 | |||
285 | public function explainQuery($query) |
||
294 | } |
||
295 | |||
296 | |||
297 | public function percolate($docs) |
||
298 | { |
||
310 | } |
||
311 | |||
312 | public function percolateToDocs($docs) |
||
313 | { |
||
314 | $params = ['index' => $this->index, 'body' => []]; |
||
315 | if ($docs instanceof Percolate) { |
||
316 | $params['body']['query'] = $docs->toArray(); |
||
317 | } else { |
||
318 | if (isset($docs[0]) && is_array($docs[0])) { |
||
319 | $params['body']['query'] = ['percolate' => ['documents' => $docs]]; |
||
320 | } else { |
||
321 | $params['body']['query'] = ['percolate' => ['document' => $docs]]; |
||
322 | } |
||
323 | } |
||
324 | return new Results\PercolateDocsResultSet($this->client->pq()->search($params, true), $docs); |
||
325 | } |
||
326 | |||
327 | |||
328 | public function getClient(): Client |
||
331 | } |
||
332 | |||
333 | public function getName(): string |
||
334 | { |
||
335 | return $this->index; |
||
336 | } |
||
337 | |||
338 | public function setName($index): self |
||
342 | } |
||
343 | } |
||
344 |