Complex classes like Manager 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. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
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 Manager, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 27 | class Manager |
||
| 28 | { |
||
| 29 | /** |
||
| 30 | * @var string Manager name |
||
| 31 | */ |
||
| 32 | private $name; |
||
| 33 | |||
| 34 | /** |
||
| 35 | * @var array Manager configuration |
||
| 36 | */ |
||
| 37 | private $config = []; |
||
| 38 | |||
| 39 | /** |
||
| 40 | * @var Client |
||
| 41 | */ |
||
| 42 | private $client; |
||
| 43 | |||
| 44 | /** |
||
| 45 | * @var Converter |
||
| 46 | */ |
||
| 47 | private $converter; |
||
| 48 | |||
| 49 | /** |
||
| 50 | * @var array Container for bulk queries |
||
| 51 | */ |
||
| 52 | private $bulkQueries = []; |
||
| 53 | |||
| 54 | /** |
||
| 55 | * @var array Container for msearch queries |
||
| 56 | */ |
||
| 57 | private $msearchQueries = []; |
||
| 58 | |||
| 59 | /** |
||
| 60 | * @var array Holder for consistency, refresh and replication parameters |
||
| 61 | */ |
||
| 62 | private $bulkParams = []; |
||
| 63 | |||
| 64 | /** |
||
| 65 | * @var array Holder for consistency, refresh and replication parameters |
||
| 66 | */ |
||
| 67 | private $msearchParams = []; |
||
| 68 | |||
| 69 | /** |
||
| 70 | * @var array |
||
| 71 | */ |
||
| 72 | private $indexSettings; |
||
| 73 | |||
| 74 | /** |
||
| 75 | * @var MetadataCollector |
||
| 76 | */ |
||
| 77 | private $metadataCollector; |
||
| 78 | |||
| 79 | /** |
||
| 80 | * After commit to make data available the refresh or flush operation is needed |
||
| 81 | * so one of those methods has to be defined, the default is refresh. |
||
| 82 | * |
||
| 83 | * @var string |
||
| 84 | */ |
||
| 85 | private $commitMode = 'refresh'; |
||
| 86 | |||
| 87 | /** |
||
| 88 | * The size that defines after how much document inserts call commit function. |
||
| 89 | * |
||
| 90 | * @var int |
||
| 91 | */ |
||
| 92 | private $bulkCommitSize = 100; |
||
| 93 | |||
| 94 | /** |
||
| 95 | * The size that defines how many searches can be executed with msearch at once. |
||
| 96 | * |
||
| 97 | * @var int |
||
| 98 | */ |
||
| 99 | private $msearchSize = 100; |
||
| 100 | |||
| 101 | /** |
||
| 102 | * Container to count how many documents was passed to the bulk query. |
||
| 103 | * |
||
| 104 | * @var int |
||
| 105 | */ |
||
| 106 | private $bulkCount = 0; |
||
| 107 | |||
| 108 | /** |
||
| 109 | * Container to count how many queries were passed to the msearch. |
||
| 110 | * |
||
| 111 | * @var int |
||
| 112 | */ |
||
| 113 | private $msearchCount = 0; |
||
| 114 | |||
| 115 | /** |
||
| 116 | * @var Repository[] Repository local cache |
||
| 117 | */ |
||
| 118 | private $repositories; |
||
| 119 | |||
| 120 | /** |
||
| 121 | * @param string $name Manager name |
||
| 122 | * @param array $config Manager configuration |
||
| 123 | * @param Client $client |
||
| 124 | * @param array $indexSettings |
||
| 125 | * @param MetadataCollector $metadataCollector |
||
| 126 | * @param Converter $converter |
||
| 127 | */ |
||
| 128 | public function __construct( |
||
| 143 | |||
| 144 | /** |
||
| 145 | * Returns Elasticsearch connection. |
||
| 146 | * |
||
| 147 | * @return Client |
||
| 148 | */ |
||
| 149 | public function getClient() |
||
| 153 | |||
| 154 | /** |
||
| 155 | * @return string |
||
| 156 | */ |
||
| 157 | public function getName() |
||
| 161 | |||
| 162 | /** |
||
| 163 | * @return array |
||
| 164 | */ |
||
| 165 | public function getMsearchParams() |
||
| 169 | |||
| 170 | /** |
||
| 171 | * @param array $msearchParams |
||
| 172 | */ |
||
| 173 | public function setMsearchParams($msearchParams) |
||
| 177 | |||
| 178 | /** |
||
| 179 | * @return int |
||
| 180 | */ |
||
| 181 | public function getMsearchSize() |
||
| 185 | |||
| 186 | /** |
||
| 187 | * @param int $msearchSize |
||
| 188 | */ |
||
| 189 | public function setMsearchSize($msearchSize) |
||
| 193 | |||
| 194 | /** |
||
| 195 | * @return array |
||
| 196 | */ |
||
| 197 | public function getConfig() |
||
| 201 | |||
| 202 | /** |
||
| 203 | * Returns repository by document class. |
||
| 204 | * |
||
| 205 | * @param string $className FQCN or string in Bundle:Document format |
||
| 206 | * |
||
| 207 | * @return Repository |
||
| 208 | */ |
||
| 209 | public function getRepository($className) |
||
| 226 | |||
| 227 | /** |
||
| 228 | * @return MetadataCollector |
||
| 229 | */ |
||
| 230 | public function getMetadataCollector() |
||
| 234 | |||
| 235 | /** |
||
| 236 | * @return Converter |
||
| 237 | */ |
||
| 238 | public function getConverter() |
||
| 242 | |||
| 243 | /** |
||
| 244 | * @return string |
||
| 245 | */ |
||
| 246 | public function getCommitMode() |
||
| 250 | |||
| 251 | /** |
||
| 252 | * @param string $commitMode |
||
| 253 | */ |
||
| 254 | public function setCommitMode($commitMode) |
||
| 262 | |||
| 263 | /** |
||
| 264 | * @return int |
||
| 265 | */ |
||
| 266 | public function getBulkCommitSize() |
||
| 270 | |||
| 271 | /** |
||
| 272 | * @param int $bulkCommitSize |
||
| 273 | */ |
||
| 274 | public function setBulkCommitSize($bulkCommitSize) |
||
| 278 | |||
| 279 | /** |
||
| 280 | * Creates a repository. |
||
| 281 | * |
||
| 282 | * @param string $className |
||
| 283 | * |
||
| 284 | * @return Repository |
||
| 285 | */ |
||
| 286 | private function createRepository($className) |
||
| 290 | |||
| 291 | /** |
||
| 292 | * Executes search query in the index. |
||
| 293 | * |
||
| 294 | * @param array $types List of types to search in. |
||
| 295 | * @param array $query Query to execute. |
||
| 296 | * @param array $queryStringParams Query parameters. |
||
| 297 | * |
||
| 298 | * @return array |
||
| 299 | */ |
||
| 300 | public function search(array $types, array $query, array $queryStringParams = []) |
||
| 313 | |||
| 314 | /** |
||
| 315 | * Adds document to next flush. |
||
| 316 | * |
||
| 317 | * @param object $document |
||
| 318 | */ |
||
| 319 | public function persist($document) |
||
| 326 | |||
| 327 | /** |
||
| 328 | * Adds document for removal. |
||
| 329 | * |
||
| 330 | * @param object $document |
||
| 331 | */ |
||
| 332 | public function remove($document) |
||
| 346 | |||
| 347 | /** |
||
| 348 | * Flushes elasticsearch index. |
||
| 349 | * |
||
| 350 | * @param array $params |
||
| 351 | * |
||
| 352 | * @return array |
||
| 353 | */ |
||
| 354 | public function flush(array $params = []) |
||
| 358 | |||
| 359 | /** |
||
| 360 | * Refreshes elasticsearch index. |
||
| 361 | * |
||
| 362 | * @param array $params |
||
| 363 | * |
||
| 364 | * @return array |
||
| 365 | */ |
||
| 366 | public function refresh(array $params = []) |
||
| 370 | |||
| 371 | /** |
||
| 372 | * Inserts the current query container to the index, used for bulk queries execution. |
||
| 373 | * |
||
| 374 | * @param array $params Parameters that will be passed to the flush or refresh queries. |
||
| 375 | * |
||
| 376 | * @return null|array |
||
| 377 | */ |
||
| 378 | public function commit(array $params = []) |
||
| 401 | |||
| 402 | /** |
||
| 403 | * Executes multi search queries |
||
| 404 | * |
||
| 405 | * @param string $resultsType |
||
| 406 | * |
||
| 407 | * @return null|array |
||
| 408 | */ |
||
| 409 | public function msearch($resultsType = Result::RESULTS_OBJECT) |
||
| 431 | |||
| 432 | /** |
||
| 433 | * Adds query to bulk queries container. |
||
| 434 | * |
||
| 435 | * @param string $operation One of: index, update, delete, create. |
||
| 436 | * @param string|array $type Elasticsearch type name. |
||
| 437 | * @param array $query DSL to execute. |
||
| 438 | * |
||
| 439 | * @throws \InvalidArgumentException |
||
| 440 | * |
||
| 441 | * @return null|array |
||
| 442 | */ |
||
| 443 | public function bulk($operation, $type, array $query) |
||
| 485 | |||
| 486 | /** |
||
| 487 | * Adds query to bulk queries container. |
||
| 488 | * |
||
| 489 | * @param Search $search DSL to execute. |
||
| 490 | * @param array $header Search header. |
||
| 491 | * |
||
| 492 | * @return null|array |
||
| 493 | */ |
||
| 494 | public function addSearch(Search $search, $header = []) |
||
| 511 | |||
| 512 | /** |
||
| 513 | * Optional setter to change bulk query params. |
||
| 514 | * |
||
| 515 | * @param array $params Possible keys: |
||
| 516 | * ['consistency'] = (enum) Explicit write consistency setting for the operation. |
||
| 517 | * ['refresh'] = (boolean) Refresh the index after performing the operation. |
||
| 518 | * ['replication'] = (enum) Explicitly set the replication type. |
||
| 519 | */ |
||
| 520 | public function setBulkParams(array $params) |
||
| 524 | |||
| 525 | /** |
||
| 526 | * Creates fresh elasticsearch index. |
||
| 527 | * |
||
| 528 | * @param bool $noMapping Determines if mapping should be included. |
||
| 529 | * |
||
| 530 | * @return array |
||
| 531 | */ |
||
| 532 | public function createIndex($noMapping = false) |
||
| 540 | |||
| 541 | /** |
||
| 542 | * Drops elasticsearch index. |
||
| 543 | */ |
||
| 544 | public function dropIndex() |
||
| 548 | |||
| 549 | /** |
||
| 550 | * Tries to drop and create fresh elasticsearch index. |
||
| 551 | * |
||
| 552 | * @param bool $noMapping Determines if mapping should be included. |
||
| 553 | * |
||
| 554 | * @return array |
||
| 555 | */ |
||
| 556 | public function dropAndCreateIndex($noMapping = false) |
||
| 566 | |||
| 567 | /** |
||
| 568 | * Checks if connection index is already created. |
||
| 569 | * |
||
| 570 | * @return bool |
||
| 571 | */ |
||
| 572 | public function indexExists() |
||
| 576 | |||
| 577 | /** |
||
| 578 | * Returns index name this connection is attached to. |
||
| 579 | * |
||
| 580 | * @return string |
||
| 581 | */ |
||
| 582 | public function getIndexName() |
||
| 586 | |||
| 587 | /** |
||
| 588 | * Sets index name for this connection. |
||
| 589 | * |
||
| 590 | * @param string $name |
||
| 591 | */ |
||
| 592 | public function setIndexName($name) |
||
| 596 | |||
| 597 | /** |
||
| 598 | * Returns Elasticsearch version number. |
||
| 599 | * |
||
| 600 | * @return string |
||
| 601 | */ |
||
| 602 | public function getVersionNumber() |
||
| 606 | |||
| 607 | /** |
||
| 608 | * Clears elasticsearch client cache. |
||
| 609 | */ |
||
| 610 | public function clearCache() |
||
| 614 | |||
| 615 | /** |
||
| 616 | * Returns a single document by ID. Returns NULL if document was not found. |
||
| 617 | * |
||
| 618 | * @param string $className Document class name or Elasticsearch type name |
||
| 619 | * @param string $id Document ID to find |
||
| 620 | * |
||
| 621 | * @return object |
||
| 622 | */ |
||
| 623 | public function find($className, $id) |
||
| 641 | |||
| 642 | /** |
||
| 643 | * Executes given search. |
||
| 644 | * |
||
| 645 | * @param array $types |
||
| 646 | * @param Search $search |
||
| 647 | * @param string $resultsType |
||
| 648 | * |
||
| 649 | * @return DocumentIterator|RawIterator|array |
||
| 650 | */ |
||
| 651 | public function execute($types, Search $search, $resultsType = Result::RESULTS_OBJECT) |
||
| 661 | |||
| 662 | /** |
||
| 663 | * Parses raw result. |
||
| 664 | * |
||
| 665 | * @param array $raw |
||
| 666 | * @param string $resultsType |
||
| 667 | * @param string $scrollDuration |
||
| 668 | * |
||
| 669 | * @return DocumentIterator|RawIterator|array |
||
| 670 | * |
||
| 671 | * @throws \Exception |
||
| 672 | */ |
||
| 673 | private function parseResult($raw, $resultsType, $scrollDuration = null) |
||
| 694 | |||
| 695 | /** |
||
| 696 | * Normalizes response array. |
||
| 697 | * |
||
| 698 | * @param array $data |
||
| 699 | * |
||
| 700 | * @return array |
||
| 701 | */ |
||
| 702 | private function convertToNormalizedArray($data) |
||
| 722 | |||
| 723 | /** |
||
| 724 | * Fetches next set of results. |
||
| 725 | * |
||
| 726 | * @param string $scrollId |
||
| 727 | * @param string $scrollDuration |
||
| 728 | * @param string $resultsType |
||
| 729 | * |
||
| 730 | * @return AbstractResultsIterator |
||
| 731 | * |
||
| 732 | * @throws \Exception |
||
| 733 | */ |
||
| 734 | public function scroll( |
||
| 743 | |||
| 744 | /** |
||
| 745 | * Clears scroll. |
||
| 746 | * |
||
| 747 | * @param string $scrollId |
||
| 748 | */ |
||
| 749 | public function clearScroll($scrollId) |
||
| 753 | |||
| 754 | /** |
||
| 755 | * Resolves type name by class name. |
||
| 756 | * |
||
| 757 | * @param string $className |
||
| 758 | * |
||
| 759 | * @return string |
||
| 760 | */ |
||
| 761 | private function resolveTypeName($className) |
||
| 769 | } |
||
| 770 |