Complex classes like CrawlerApi 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 CrawlerApi, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 41 | class CrawlerApi |
||
| 42 | { |
||
| 43 | /** |
||
| 44 | * @var CrawlerController |
||
| 45 | */ |
||
| 46 | private $crawlerController; |
||
| 47 | |||
| 48 | /** |
||
| 49 | * @var QueueRepository |
||
| 50 | */ |
||
| 51 | protected $queueRepository; |
||
| 52 | |||
| 53 | /** |
||
| 54 | * @var $allowedConfigurations array |
||
| 55 | */ |
||
| 56 | protected $allowedConfigurations = []; |
||
| 57 | |||
| 58 | /** |
||
| 59 | * Each crawler run has a setid, this facade method delegates |
||
| 60 | * the it to the crawler object |
||
| 61 | * |
||
| 62 | * @param int |
||
| 63 | */ |
||
| 64 | 1 | public function overwriteSetId($id) |
|
| 68 | |||
| 69 | /** |
||
| 70 | * This method is used to limit the configuration selection to |
||
| 71 | * a set of configurations. |
||
| 72 | * |
||
| 73 | * @param array $allowedConfigurations |
||
| 74 | */ |
||
| 75 | 1 | public function setAllowedConfigurations(array $allowedConfigurations) |
|
| 79 | |||
| 80 | /** |
||
| 81 | * @return array |
||
| 82 | */ |
||
| 83 | 1 | public function getAllowedConfigurations() |
|
| 87 | |||
| 88 | /** |
||
| 89 | * Returns the setID of the crawler |
||
| 90 | * |
||
| 91 | * @return int |
||
| 92 | */ |
||
| 93 | 1 | public function getSetId() |
|
| 97 | |||
| 98 | 19 | public function __construct() |
|
| 103 | |||
| 104 | /** |
||
| 105 | * Method to get an instance of the internal crawler singleton |
||
| 106 | * |
||
| 107 | * @return CrawlerController Instance of the crawler lib |
||
| 108 | * |
||
| 109 | * @throws \Exception |
||
| 110 | */ |
||
| 111 | 2 | protected function findCrawler() |
|
| 124 | |||
| 125 | /** |
||
| 126 | * Adds a page to the crawlerqueue by uid |
||
| 127 | * |
||
| 128 | * @param int $uid uid |
||
| 129 | */ |
||
| 130 | public function addPageToQueue($uid) |
||
| 136 | |||
| 137 | /** |
||
| 138 | * This method is used to limit the processing instructions to the processing instructions |
||
| 139 | * that are allowed. |
||
| 140 | * |
||
| 141 | * @return array |
||
| 142 | */ |
||
| 143 | 4 | protected function filterUnallowedConfigurations($configurations) |
|
| 156 | |||
| 157 | /** |
||
| 158 | * Adds a page to the crawlerqueue by uid and sets a |
||
| 159 | * timestamp when the page should be crawled. |
||
| 160 | * |
||
| 161 | * @param int $uid pageid |
||
| 162 | * @param int $time timestamp |
||
| 163 | * |
||
| 164 | * @throws \Exception |
||
| 165 | */ |
||
| 166 | 4 | public function addPageToQueueTimed($uid, $time) |
|
| 201 | |||
| 202 | /** |
||
| 203 | * Counts all entries in the database which are scheduled for a given page id and a schedule timestamp. |
||
| 204 | * |
||
| 205 | * @param int $page_uid |
||
| 206 | * @param int $schedule_timestamp |
||
| 207 | * |
||
| 208 | * @return int |
||
| 209 | * |
||
| 210 | * @deprecated since crawler v6.2.0, will be removed in crawler v7.0.0. |
||
| 211 | */ |
||
| 212 | 1 | protected function countEntriesInQueueForPageByScheduleTime($page_uid, $schedule_timestamp) |
|
| 236 | |||
| 237 | /** |
||
| 238 | * Determines if a page is queued |
||
| 239 | * |
||
| 240 | * @param $uid |
||
| 241 | * @param bool $unprocessed_only |
||
| 242 | * @param bool $timed_only |
||
| 243 | * @param bool $timestamp |
||
| 244 | * |
||
| 245 | * @deprecated since crawler v6.5.1, will be removed in crawler v9.0.0. |
||
| 246 | * |
||
| 247 | * @return bool |
||
| 248 | */ |
||
| 249 | 5 | public function isPageInQueue($uid, $unprocessed_only = true, $timed_only = false, $timestamp = false) |
|
| 283 | |||
| 284 | /** |
||
| 285 | * Method to return the latest Crawler Timestamp for a page. |
||
| 286 | * |
||
| 287 | * @param int $uid uid id of the page |
||
| 288 | * @param bool $future_crawldates_only |
||
| 289 | * @param bool $unprocessed_only |
||
| 290 | * |
||
| 291 | * @return int |
||
| 292 | */ |
||
| 293 | 1 | public function getLatestCrawlTimestampForPage($uid, $future_crawldates_only = false, $unprocessed_only = false) |
|
| 316 | |||
| 317 | /** |
||
| 318 | * Returns an array with timestamps when the page has been scheduled for crawling and |
||
| 319 | * at what time the scheduled crawl has been executed. The array also contains items that are |
||
| 320 | * scheduled but have note been crawled yet. |
||
| 321 | * |
||
| 322 | * @param int $uid uid of the page |
||
| 323 | * @param bool $limit |
||
| 324 | * |
||
| 325 | * @return array array with the crawl-history of a page => 0 : scheduled time , 1 : executed_time, 2 : set_id |
||
| 326 | */ |
||
| 327 | 1 | public function getCrawlHistoryForPage($uid, $limit = 0) |
|
| 340 | |||
| 341 | /** |
||
| 342 | * Method to determine unprocessed Items in the crawler queue. |
||
| 343 | * |
||
| 344 | * @deprecated since crawler v6.5.1, will be removed in crawler v9.0.0. |
||
| 345 | * |
||
| 346 | * @return array |
||
| 347 | */ |
||
| 348 | 1 | public function getUnprocessedItems() |
|
| 360 | |||
| 361 | /** |
||
| 362 | * Method to get the number of unprocessed items in the crawler |
||
| 363 | * |
||
| 364 | * @param int number of unprocessed items in the queue |
||
| 365 | * @deprecated since crawler v6.5.1, will be removed in crawler v9.0.0. |
||
| 366 | * |
||
| 367 | */ |
||
| 368 | 3 | public function countUnprocessedItems() |
|
| 377 | |||
| 378 | /** |
||
| 379 | * Method to check if a page is in the queue which is timed for a |
||
| 380 | * date when it should be crawled |
||
| 381 | * |
||
| 382 | * @param int $uid uid of the page |
||
| 383 | * @param boolean $show_unprocessed only respect unprocessed pages |
||
| 384 | * |
||
| 385 | * @deprecated since crawler v6.5.1, will be removed in crawler v9.0.0. |
||
| 386 | * |
||
| 387 | * @return boolean |
||
| 388 | */ |
||
| 389 | 1 | public function isPageInQueueTimed($uid, $show_unprocessed = true) |
|
| 394 | |||
| 395 | /** |
||
| 396 | * Reads the registered processingInstructions of the crawler |
||
| 397 | * |
||
| 398 | * @return array |
||
| 399 | */ |
||
| 400 | 4 | private function getCrawlerProcInstructions() |
|
| 408 | |||
| 409 | /** |
||
| 410 | * Removes an queue entry with a given queue id |
||
| 411 | * |
||
| 412 | * @param int $qid |
||
| 413 | * @deprecated since crawler v6.5.1, will be removed in crawler v9.0.0. Please use the QueueRepository instead |
||
| 414 | */ |
||
| 415 | public function removeQueueEntrie($qid) |
||
| 422 | |||
| 423 | /** |
||
| 424 | * Get queue statistics |
||
| 425 | * |
||
| 426 | * @param void |
||
| 427 | * |
||
| 428 | * @return array array('assignedButUnprocessed' => <>, 'unprocessed' => <>); |
||
| 429 | */ |
||
| 430 | 1 | public function getQueueStatistics() |
|
| 437 | |||
| 438 | /** |
||
| 439 | * Get queue repository |
||
| 440 | * |
||
| 441 | * @return QueueRepository |
||
| 442 | */ |
||
| 443 | 2 | protected function getQueueRepository() |
|
| 451 | |||
| 452 | /** |
||
| 453 | * Get queue statistics by configuration |
||
| 454 | * |
||
| 455 | * @return array array of array('configuration' => <>, 'assignedButUnprocessed' => <>, 'unprocessed' => <>) |
||
| 456 | */ |
||
| 457 | public function getQueueStatisticsByConfiguration() |
||
| 472 | |||
| 473 | /** |
||
| 474 | * Get active processes count |
||
| 475 | * |
||
| 476 | * @param void |
||
| 477 | * |
||
| 478 | * @return int |
||
| 479 | */ |
||
| 480 | public function getActiveProcessesCount() |
||
| 486 | |||
| 487 | /** |
||
| 488 | * Get last processed entries |
||
| 489 | * |
||
| 490 | * @param int $limit |
||
| 491 | * |
||
| 492 | * @return array |
||
| 493 | */ |
||
| 494 | public function getLastProcessedQueueEntries($limit) |
||
| 498 | |||
| 499 | /** |
||
| 500 | * Get current crawling speed |
||
| 501 | * |
||
| 502 | * @param float|false page speed in pages per minute |
||
| 503 | * |
||
| 504 | * @return int |
||
| 505 | */ |
||
| 506 | public function getCurrentCrawlingSpeed() |
||
| 541 | |||
| 542 | /** |
||
| 543 | * Get some performance data |
||
| 544 | * |
||
| 545 | * @param integer $start |
||
| 546 | * @param integer $end |
||
| 547 | * @param integer $resolution |
||
| 548 | * |
||
| 549 | * @return array data |
||
| 550 | * |
||
| 551 | * @throws \Exception |
||
| 552 | */ |
||
| 553 | public function getPerformanceData($start, $end, $resolution) |
||
| 602 | } |
||
| 603 |