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 |
||
| 43 | class CrawlerApi |
||
| 44 | { |
||
| 45 | /** |
||
| 46 | * @var CrawlerController|Object |
||
| 47 | */ |
||
| 48 | private $crawlerController; |
||
| 49 | |||
| 50 | /** |
||
| 51 | * @var QueueRepository |
||
| 52 | */ |
||
| 53 | protected $queueRepository; |
||
| 54 | |||
| 55 | /** |
||
| 56 | * @var array |
||
| 57 | */ |
||
| 58 | protected $allowedConfigurations = []; |
||
| 59 | |||
| 60 | /** |
||
| 61 | * @var QueryBuilder |
||
| 62 | */ |
||
| 63 | protected $queryBuilder; |
||
| 64 | |||
| 65 | /** |
||
| 66 | * @var string |
||
| 67 | */ |
||
| 68 | protected $tableName = 'tx_crawler_queue'; |
||
| 69 | |||
| 70 | 10 | public function __construct() |
|
| 77 | |||
| 78 | /** |
||
| 79 | * Each crawler run has a setid, this facade method delegates |
||
| 80 | * the it to the crawler object |
||
| 81 | * |
||
| 82 | * @param int $id |
||
| 83 | * @throws \Exception |
||
| 84 | */ |
||
| 85 | 1 | public function overwriteSetId(int $id) |
|
| 89 | |||
| 90 | /** |
||
| 91 | * This method is used to limit the configuration selection to |
||
| 92 | * a set of configurations. |
||
| 93 | * |
||
| 94 | * @param array $allowedConfigurations |
||
| 95 | */ |
||
| 96 | 1 | public function setAllowedConfigurations(array $allowedConfigurations) |
|
| 100 | |||
| 101 | /** |
||
| 102 | * @return array |
||
| 103 | */ |
||
| 104 | 1 | public function getAllowedConfigurations() |
|
| 108 | |||
| 109 | /** |
||
| 110 | * Returns the setID of the crawler |
||
| 111 | * |
||
| 112 | * @return int |
||
| 113 | */ |
||
| 114 | 1 | public function getSetId() |
|
| 118 | |||
| 119 | /** |
||
| 120 | * Method to get an instance of the internal crawler singleton |
||
| 121 | * |
||
| 122 | * @return CrawlerController Instance of the crawler lib |
||
| 123 | * |
||
| 124 | * @throws \Exception |
||
| 125 | */ |
||
| 126 | 2 | protected function findCrawler() |
|
| 139 | |||
| 140 | /** |
||
| 141 | * Adds a page to the crawlerqueue by uid |
||
| 142 | * |
||
| 143 | * @param int $uid uid |
||
| 144 | */ |
||
| 145 | public function addPageToQueue($uid) |
||
| 151 | |||
| 152 | /** |
||
| 153 | * This method is used to limit the processing instructions to the processing instructions |
||
| 154 | * that are allowed. |
||
| 155 | * |
||
| 156 | * @return array |
||
| 157 | */ |
||
| 158 | 2 | protected function filterUnallowedConfigurations($configurations) |
|
| 171 | |||
| 172 | /** |
||
| 173 | * Adds a page to the crawlerqueue by uid and sets a |
||
| 174 | * timestamp when the page should be crawled. |
||
| 175 | * |
||
| 176 | * @param int $uid pageid |
||
| 177 | * @param int $time timestamp |
||
| 178 | * |
||
| 179 | * @throws \Exception |
||
| 180 | * @return void |
||
| 181 | */ |
||
| 182 | 2 | public function addPageToQueueTimed($uid, $time) |
|
| 215 | |||
| 216 | /** |
||
| 217 | * Counts all entries in the database which are scheduled for a given page id and a schedule timestamp. |
||
| 218 | * |
||
| 219 | * @param int $page_uid |
||
| 220 | * @param int $schedule_timestamp |
||
| 221 | * |
||
| 222 | * @return int |
||
| 223 | */ |
||
| 224 | 1 | protected function countEntriesInQueueForPageByScheduleTime($page_uid, $schedule_timestamp) |
|
| 251 | |||
| 252 | /** |
||
| 253 | * Method to return the latest Crawle Timestamp for a page. |
||
| 254 | * |
||
| 255 | * @param int $uid uid id of the page |
||
| 256 | * @param bool $future_crawldates_only |
||
| 257 | * @param bool $unprocessed_only |
||
| 258 | * |
||
| 259 | * @return int |
||
| 260 | */ |
||
| 261 | 1 | public function getLatestCrawlTimestampForPage($uid, $future_crawldates_only = false, $unprocessed_only = false) |
|
| 294 | |||
| 295 | /** |
||
| 296 | * Returns an array with timestamps when the page has been scheduled for crawling and |
||
| 297 | * at what time the scheduled crawl has been executed. The array also contains items that are |
||
| 298 | * scheduled but have note been crawled yet. |
||
| 299 | * |
||
| 300 | * @param int $uid uid of the page |
||
| 301 | * @param bool $limit |
||
| 302 | * |
||
| 303 | * @return array array with the crawl-history of a page => 0 : scheduled time , 1 : executed_time, 2 : set_id |
||
| 304 | */ |
||
| 305 | 1 | public function getCrawlHistoryForPage($uid, $limit = 0) |
|
| 323 | |||
| 324 | /** |
||
| 325 | * Reads the registered processingInstructions of the crawler |
||
| 326 | * |
||
| 327 | * @return array |
||
| 328 | */ |
||
| 329 | 2 | private function getCrawlerProcInstructions(): array |
|
| 340 | |||
| 341 | /** |
||
| 342 | * Get queue statistics |
||
| 343 | * |
||
| 344 | * @param void |
||
| 345 | * |
||
| 346 | * @return array array('assignedButUnprocessed' => <>, 'unprocessed' => <>); |
||
| 347 | */ |
||
| 348 | 1 | public function getQueueStatistics() |
|
| 355 | |||
| 356 | /** |
||
| 357 | * Get queue statistics by configuration |
||
| 358 | * |
||
| 359 | * @return array array of array('configuration' => <>, 'assignedButUnprocessed' => <>, 'unprocessed' => <>) |
||
| 360 | */ |
||
| 361 | public function getQueueStatisticsByConfiguration() |
||
| 374 | |||
| 375 | /** |
||
| 376 | * Get active processes count |
||
| 377 | * |
||
| 378 | * @param void |
||
| 379 | * |
||
| 380 | * @return int |
||
| 381 | */ |
||
| 382 | public function getActiveProcessesCount() |
||
| 388 | |||
| 389 | /** |
||
| 390 | * Get last processed entries |
||
| 391 | * |
||
| 392 | * @param int $limit |
||
| 393 | * |
||
| 394 | * @return array |
||
| 395 | */ |
||
| 396 | public function getLastProcessedQueueEntries($limit) |
||
| 400 | |||
| 401 | /** |
||
| 402 | * Get current crawling speed |
||
| 403 | * |
||
| 404 | * @param float|false page speed in pages per minute |
||
| 405 | * |
||
| 406 | * @return int |
||
| 407 | */ |
||
| 408 | public function getCurrentCrawlingSpeed() |
||
| 443 | |||
| 444 | /** |
||
| 445 | * Get some performance data |
||
| 446 | * |
||
| 447 | * @param integer $start |
||
| 448 | * @param integer $end |
||
| 449 | * @param integer $resolution |
||
| 450 | * |
||
| 451 | * @return array data |
||
| 452 | * |
||
| 453 | * @throws \Exception |
||
| 454 | */ |
||
| 455 | public function getPerformanceData($start, $end, $resolution) |
||
| 504 | } |
||
| 505 |