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 |
||
| 45 | class CrawlerApi |
||
| 46 | { |
||
| 47 | /** |
||
| 48 | * @var QueueRepository |
||
| 49 | */ |
||
| 50 | protected $queueRepository; |
||
| 51 | |||
| 52 | /** |
||
| 53 | * @var array |
||
| 54 | */ |
||
| 55 | protected $allowedConfigurations = []; |
||
| 56 | |||
| 57 | /** |
||
| 58 | * @var QueryBuilder |
||
| 59 | */ |
||
| 60 | protected $queryBuilder; |
||
| 61 | |||
| 62 | /** |
||
| 63 | * @var string |
||
| 64 | */ |
||
| 65 | protected $tableName = 'tx_crawler_queue'; |
||
| 66 | |||
| 67 | public function __construct() |
||
| 73 | |||
| 74 | /** |
||
| 75 | * Each crawler run has a setid, this facade method delegates |
||
| 76 | * the it to the crawler object |
||
| 77 | * |
||
| 78 | * @param int $id |
||
| 79 | * @throws \Exception |
||
| 80 | */ |
||
| 81 | public function overwriteSetId(int $id): void |
||
| 85 | |||
| 86 | /** |
||
| 87 | * This method is used to limit the configuration selection to |
||
| 88 | * a set of configurations. |
||
| 89 | * |
||
| 90 | * @param array $allowedConfigurations |
||
| 91 | */ |
||
| 92 | public function setAllowedConfigurations(array $allowedConfigurations): void |
||
| 96 | |||
| 97 | /** |
||
| 98 | * @return array |
||
| 99 | */ |
||
| 100 | public function getAllowedConfigurations() |
||
| 104 | |||
| 105 | /** |
||
| 106 | * Returns the setID of the crawler |
||
| 107 | * |
||
| 108 | * @return int |
||
| 109 | */ |
||
| 110 | public function getSetId() |
||
| 114 | |||
| 115 | /** |
||
| 116 | * Method to get an instance of the internal crawler singleton |
||
| 117 | * |
||
| 118 | * @return CrawlerController Instance of the crawler lib |
||
| 119 | * |
||
| 120 | * @throws \Exception |
||
| 121 | */ |
||
| 122 | protected function findCrawler() |
||
| 135 | |||
| 136 | /** |
||
| 137 | * Adds a page to the crawlerqueue by uid |
||
| 138 | * |
||
| 139 | * @param int $uid uid |
||
| 140 | */ |
||
| 141 | public function addPageToQueue($uid): void |
||
| 147 | |||
| 148 | /** |
||
| 149 | * This method is used to limit the processing instructions to the processing instructions |
||
| 150 | * that are allowed. |
||
| 151 | * |
||
| 152 | * @return array |
||
| 153 | */ |
||
| 154 | protected function filterUnallowedConfigurations($configurations) |
||
| 167 | |||
| 168 | /** |
||
| 169 | * Adds a page to the crawlerqueue by uid and sets a |
||
| 170 | * timestamp when the page should be crawled. |
||
| 171 | * |
||
| 172 | * @param int $uid pageid |
||
| 173 | * @param int $time timestamp |
||
| 174 | * |
||
| 175 | * @throws \Exception |
||
| 176 | * @return void |
||
| 177 | */ |
||
| 178 | public function addPageToQueueTimed($uid, $time): void |
||
| 211 | |||
| 212 | /** |
||
| 213 | * Counts all entries in the database which are scheduled for a given page id and a schedule timestamp. |
||
| 214 | * |
||
| 215 | * @param int $page_uid |
||
| 216 | * @param int $schedule_timestamp |
||
| 217 | * |
||
| 218 | * @return int |
||
| 219 | */ |
||
| 220 | protected function countEntriesInQueueForPageByScheduleTime($page_uid, $schedule_timestamp) |
||
| 247 | |||
| 248 | /** |
||
| 249 | * Method to return the latest Crawle Timestamp for a page. |
||
| 250 | * |
||
| 251 | * @param int $uid uid id of the page |
||
| 252 | * @param bool $future_crawldates_only |
||
| 253 | * @param bool $unprocessed_only |
||
| 254 | * |
||
| 255 | * @return int |
||
| 256 | */ |
||
| 257 | public function getLatestCrawlTimestampForPage($uid, $future_crawldates_only = false, $unprocessed_only = false) |
||
| 290 | |||
| 291 | /** |
||
| 292 | * Returns an array with timestamps when the page has been scheduled for crawling and |
||
| 293 | * at what time the scheduled crawl has been executed. The array also contains items that are |
||
| 294 | * scheduled but have note been crawled yet. |
||
| 295 | * |
||
| 296 | * @param int $uid uid of the page |
||
| 297 | * @param bool $limit |
||
| 298 | * |
||
| 299 | * @return array array with the crawl-history of a page => 0 : scheduled time , 1 : executed_time, 2 : set_id |
||
| 300 | */ |
||
| 301 | public function getCrawlHistoryForPage($uid, $limit = 0) |
||
| 319 | |||
| 320 | /** |
||
| 321 | * Reads the registered processingInstructions of the crawler |
||
| 322 | * |
||
| 323 | * @return array |
||
| 324 | */ |
||
| 325 | private function getCrawlerProcInstructions(): array |
||
| 336 | |||
| 337 | /** |
||
| 338 | * Get queue statistics |
||
| 339 | * |
||
| 340 | * @param void |
||
| 341 | * |
||
| 342 | * @return array array('assignedButUnprocessed' => <>, 'unprocessed' => <>); |
||
| 343 | */ |
||
| 344 | public function getQueueStatistics() |
||
| 351 | |||
| 352 | /** |
||
| 353 | * Get queue statistics by configuration |
||
| 354 | * |
||
| 355 | * @return array array of array('configuration' => <>, 'assignedButUnprocessed' => <>, 'unprocessed' => <>) |
||
| 356 | */ |
||
| 357 | public function getQueueStatisticsByConfiguration() |
||
| 370 | |||
| 371 | /** |
||
| 372 | * Get active processes count |
||
| 373 | * |
||
| 374 | * @param void |
||
| 375 | * |
||
| 376 | * @return int |
||
| 377 | */ |
||
| 378 | public function getActiveProcessesCount() |
||
| 384 | |||
| 385 | /** |
||
| 386 | * Get last processed entries |
||
| 387 | * |
||
| 388 | * @param int $limit |
||
| 389 | * |
||
| 390 | * @return array |
||
| 391 | */ |
||
| 392 | public function getLastProcessedQueueEntries($limit) |
||
| 396 | |||
| 397 | /** |
||
| 398 | * Get current crawling speed |
||
| 399 | * |
||
| 400 | * @param float|false page speed in pages per minute |
||
| 401 | * |
||
| 402 | * @return int |
||
| 403 | */ |
||
| 404 | public function getCurrentCrawlingSpeed() |
||
| 438 | |||
| 439 | /** |
||
| 440 | * Get some performance data |
||
| 441 | * |
||
| 442 | * @param integer $start |
||
| 443 | * @param integer $end |
||
| 444 | * @param integer $resolution |
||
| 445 | * |
||
| 446 | * @return array data |
||
| 447 | * |
||
| 448 | * @throws \Exception |
||
| 449 | */ |
||
| 450 | public function getPerformanceData($start, $end, $resolution) |
||
| 499 | } |
||
| 500 |
In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:
Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion: