| Total Complexity | 62 |
| Total Lines | 382 |
| 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 | protected $cluster; |
||
| 22 | |||
| 23 | public function __construct(Client $client, $index = null) |
||
| 30 | } |
||
| 31 | |||
| 32 | public function search($input): Search |
||
| 33 | { |
||
| 34 | $search = new Search($this->client); |
||
| 35 | $search->setIndex($this->index); |
||
| 36 | return $search->search($input); |
||
| 37 | } |
||
| 38 | |||
| 39 | public function getDocumentById($id) |
||
| 51 | } |
||
| 52 | |||
| 53 | public function addDocument($data, $id = 0) |
||
| 54 | { |
||
| 55 | if (is_object($data)) { |
||
| 56 | $data = (array) $data; |
||
| 57 | } elseif (is_string($data)) { |
||
| 58 | $data = json_decode($data, true); |
||
| 59 | } |
||
| 60 | $params = [ |
||
| 61 | 'body' => [ |
||
| 62 | 'index' => $this->index, |
||
| 63 | 'id' => $id, |
||
| 64 | 'doc' => $data |
||
| 65 | ] |
||
| 66 | ]; |
||
| 67 | |||
| 68 | if ($this->cluster !== null) { |
||
| 69 | $params['body']['cluster'] = $this->cluster; |
||
| 70 | } |
||
| 71 | return $this->client->insert($params); |
||
| 72 | } |
||
| 73 | |||
| 74 | public function addDocuments($documents) |
||
| 75 | { |
||
| 76 | $toinsert = []; |
||
| 77 | foreach ($documents as $document) { |
||
| 78 | if (is_object($document)) { |
||
| 79 | $document = (array) $document; |
||
| 80 | } elseif (is_string($document)) { |
||
| 81 | $document = json_decode($document, true); |
||
| 82 | } |
||
| 83 | if (isset($document['id'])) { |
||
| 84 | $id = $document['id']; |
||
| 85 | unset($document['id']); |
||
| 86 | } else { |
||
| 87 | $id = 0; |
||
| 88 | } |
||
| 89 | $insert = [ |
||
| 90 | 'index' => $this->index, |
||
| 91 | 'id' => $id, |
||
| 92 | 'doc' => $document |
||
| 93 | ]; |
||
| 94 | if ($this->cluster !== null) { |
||
| 95 | $insert['cluster'] = $this->cluster; |
||
| 96 | } |
||
| 97 | $toinsert[] = ['insert' => $insert]; |
||
| 98 | } |
||
| 99 | return $this->client->bulk(['body' => $toinsert]); |
||
| 100 | } |
||
| 101 | |||
| 102 | public function deleteDocument($id) |
||
| 103 | { |
||
| 104 | $params = [ |
||
| 105 | 'body' => [ |
||
| 106 | 'index' => $this->index, |
||
| 107 | 'id' => $id |
||
| 108 | ] |
||
| 109 | ]; |
||
| 110 | if ($this->cluster !== null) { |
||
| 111 | $params['body']['cluster'] = $this->cluster; |
||
| 112 | } |
||
| 113 | return $this->client->delete($params); |
||
| 114 | } |
||
| 115 | |||
| 116 | public function deleteDocuments($query) |
||
| 117 | { |
||
| 118 | if ($query instanceof Query) { |
||
| 119 | $query = $query->toArray(); |
||
| 120 | } |
||
| 121 | $params = [ |
||
| 122 | 'body' => [ |
||
| 123 | 'index' => $this->index, |
||
| 124 | 'query' => $query |
||
| 125 | ] |
||
| 126 | ]; |
||
| 127 | if ($this->cluster !== null) { |
||
| 128 | $params['body']['cluster'] = $this->cluster; |
||
| 129 | } |
||
| 130 | return $this->client->delete($params); |
||
| 131 | } |
||
| 132 | |||
| 133 | public function updateDocument($data, $id) |
||
| 134 | { |
||
| 135 | $params = [ |
||
| 136 | 'body' => [ |
||
| 137 | 'index' => $this->index, |
||
| 138 | 'id' => $id, |
||
| 139 | 'doc' => $data |
||
| 140 | ] |
||
| 141 | ]; |
||
| 142 | if ($this->cluster !== null) { |
||
| 143 | $params['body']['cluster'] = $this->cluster; |
||
| 144 | } |
||
| 145 | return $this->client->update($params); |
||
| 146 | } |
||
| 147 | |||
| 148 | public function updateDocuments($data, $query) |
||
| 149 | { |
||
| 150 | if ($query instanceof Query) { |
||
| 151 | $query = $query->toArray(); |
||
| 152 | } |
||
| 153 | $params = [ |
||
| 154 | 'body' => [ |
||
| 155 | 'index' => $this->index, |
||
| 156 | 'query' => $query, |
||
| 157 | 'doc' => $data |
||
| 158 | ] |
||
| 159 | ]; |
||
| 160 | if ($this->cluster !== null) { |
||
| 161 | $params['body']['cluster'] = $this->cluster; |
||
| 162 | } |
||
| 163 | return $this->client->update($params); |
||
| 164 | } |
||
| 165 | |||
| 166 | public function replaceDocument($data, $id) |
||
| 167 | { |
||
| 168 | if (is_object($data)) { |
||
| 169 | $data = (array) $data; |
||
| 170 | } elseif (is_string($data)) { |
||
| 171 | $data = json_decode($data, true); |
||
| 172 | } |
||
| 173 | $params = [ |
||
| 174 | 'body' => [ |
||
| 175 | 'index' => $this->index, |
||
| 176 | 'id' => $id, |
||
| 177 | 'doc' => $data |
||
| 178 | ] |
||
| 179 | ]; |
||
| 180 | if ($this->cluster !== null) { |
||
| 181 | $params['body']['cluster'] = $this->cluster; |
||
| 182 | } |
||
| 183 | return $this->client->replace($params); |
||
| 184 | } |
||
| 185 | |||
| 186 | public function replaceDocuments($documents) |
||
| 187 | { |
||
| 188 | $toreplace = []; |
||
| 189 | foreach ($documents as $document) { |
||
| 190 | if (is_object($document)) { |
||
| 191 | $document = (array) $document; |
||
| 192 | } elseif (is_string($document)) { |
||
| 193 | $document = json_decode($document, true); |
||
| 194 | } |
||
| 195 | $id = $document['id']; |
||
| 196 | unset($document['id']); |
||
| 197 | $replace = [ |
||
| 198 | 'index' => $this->index, |
||
| 199 | 'id' => $id, |
||
| 200 | 'doc' => $document |
||
| 201 | |||
| 202 | ]; |
||
| 203 | if ($this->cluster !== null) { |
||
| 204 | $replace['cluster'] = $this->cluster; |
||
| 205 | } |
||
| 206 | $toreplace[] = ['replace' => $replace]; |
||
| 207 | } |
||
| 208 | return $this->client->bulk(['body' => $toreplace]); |
||
| 209 | } |
||
| 210 | |||
| 211 | public function create($fields, $settings = [], $silent = false) |
||
| 212 | { |
||
| 213 | $params = [ |
||
| 214 | 'index' => $this->index, |
||
| 215 | 'body' => [ |
||
| 216 | 'columns' => $fields, |
||
| 217 | 'settings' => $settings |
||
| 218 | ] |
||
| 219 | ]; |
||
| 220 | if ($silent === true) { |
||
| 221 | $params['body']['silent'] = true; |
||
| 222 | } |
||
| 223 | return $this->client->indices()->create($params); |
||
| 224 | } |
||
| 225 | |||
| 226 | public function drop($silent = false) |
||
| 227 | { |
||
| 228 | $params = [ |
||
| 229 | 'index' => $this->index, |
||
| 230 | ]; |
||
| 231 | if ($silent === true) { |
||
| 232 | $params['body'] = ['silent' => true]; |
||
| 233 | } |
||
| 234 | return $this->client->indices()->drop($params); |
||
| 235 | } |
||
| 236 | |||
| 237 | public function describe() |
||
| 238 | { |
||
| 239 | $params = [ |
||
| 240 | 'index' => $this->index, |
||
| 241 | ]; |
||
| 242 | return $this->client->indices()->describe($params); |
||
| 243 | } |
||
| 244 | |||
| 245 | public function status() |
||
| 246 | { |
||
| 247 | $params = [ |
||
| 248 | 'index' => $this->index, |
||
| 249 | ]; |
||
| 250 | return $this->client->indices()->status($params); |
||
| 251 | } |
||
| 252 | |||
| 253 | public function truncate() |
||
| 259 | } |
||
| 260 | |||
| 261 | public function optimize($sync = false) |
||
| 270 | } |
||
| 271 | |||
| 272 | public function flush() |
||
| 273 | { |
||
| 274 | $params = [ |
||
| 275 | 'index' => $this->index, |
||
| 276 | ]; |
||
| 277 | $this->client->indices()->flushrtindex($params); |
||
| 278 | } |
||
| 279 | |||
| 280 | public function flushramchunk() |
||
| 281 | { |
||
| 282 | $params = [ |
||
| 283 | 'index' => $this->index, |
||
| 284 | ]; |
||
| 285 | $this->client->indices()->flushramchunk($params); |
||
| 286 | } |
||
| 287 | |||
| 288 | public function alter($operation, $name, $type = null) |
||
| 289 | { |
||
| 290 | if ($operation === 'add') { |
||
| 291 | $params = [ |
||
| 292 | 'index' => $this->index, |
||
| 293 | 'body' => [ |
||
| 294 | 'operation' => 'add', |
||
| 295 | 'column' => ['name' => $name, 'type' => $type] |
||
| 296 | ] |
||
| 297 | ]; |
||
| 298 | } elseif ($operation === 'drop') { |
||
| 299 | $params = [ |
||
| 300 | 'index' => $this->index, |
||
| 301 | 'body' => [ |
||
| 302 | 'operation' => 'drop', |
||
| 303 | 'column' => ['name' => $name] |
||
| 304 | ] |
||
| 305 | ]; |
||
| 306 | } else { |
||
| 307 | throw new RuntimeException('Alter operation not recognized'); |
||
| 308 | } |
||
| 309 | return $this->client->indices()->alter($params); |
||
| 310 | } |
||
| 311 | |||
| 312 | public function keywords($query, $options) |
||
| 313 | { |
||
| 314 | $params = [ |
||
| 315 | 'index' => $this->index, |
||
| 316 | 'body' => [ |
||
| 317 | 'query' => $query, |
||
| 318 | 'options' => $options |
||
| 319 | ] |
||
| 320 | ]; |
||
| 321 | return $this->client->keywords($params); |
||
| 322 | } |
||
| 323 | |||
| 324 | public function suggest($query, $options) |
||
| 325 | { |
||
| 326 | $params = [ |
||
| 327 | 'index' => $this->index, |
||
| 328 | 'body' => [ |
||
| 329 | 'query' => $query, |
||
| 330 | 'options' => $options |
||
| 331 | ] |
||
| 332 | ]; |
||
| 333 | return $this->client->suggest($params); |
||
| 334 | } |
||
| 335 | |||
| 336 | public function explainQuery($query) |
||
| 337 | { |
||
| 338 | $params = [ |
||
| 339 | 'index' => $this->index, |
||
| 340 | 'body' => [ |
||
| 341 | 'query' => $query, |
||
| 342 | ] |
||
| 343 | ]; |
||
| 344 | return $this->client->explainQuery($params); |
||
| 345 | } |
||
| 346 | |||
| 347 | |||
| 348 | public function percolate($docs) |
||
| 361 | } |
||
| 362 | |||
| 363 | public function percolateToDocs($docs) |
||
| 364 | { |
||
| 365 | $params = ['index' => $this->index, 'body' => []]; |
||
| 366 | if ($docs instanceof Percolate) { |
||
| 367 | $params['body']['query'] = $docs->toArray(); |
||
| 368 | } else { |
||
| 369 | if (isset($docs[0]) && is_array($docs[0])) { |
||
| 370 | $params['body']['query'] = ['percolate' => ['documents' => $docs]]; |
||
| 371 | } else { |
||
| 372 | $params['body']['query'] = ['percolate' => ['document' => $docs]]; |
||
| 373 | } |
||
| 374 | } |
||
| 375 | return new Results\PercolateDocsResultSet($this->client->pq()->search($params, true), $docs); |
||
| 376 | } |
||
| 377 | |||
| 378 | |||
| 379 | public function getClient(): Client |
||
| 382 | } |
||
| 383 | |||
| 384 | public function getName(): string |
||
| 385 | { |
||
| 386 | return $this->index; |
||
| 387 | } |
||
| 388 | |||
| 389 | public function setName($index): self |
||
| 393 | } |
||
| 394 | |||
| 395 | public function setCluster($cluster): self |
||
| 396 | { |
||
| 397 | $this->cluster = $cluster; |
||
| 398 | return $this; |
||
| 399 | } |
||
| 400 | } |
||
| 401 |