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 |
||
| 40 | class CrawlerApi |
||
| 41 | { |
||
| 42 | /** |
||
| 43 | * @var CrawlerController |
||
| 44 | */ |
||
| 45 | private $crawlerController; |
||
| 46 | |||
| 47 | /** |
||
| 48 | * @var QueueRepository |
||
| 49 | */ |
||
| 50 | protected $queueRepository; |
||
| 51 | |||
| 52 | /** |
||
| 53 | * @var $allowedConfigurations array |
||
| 54 | */ |
||
| 55 | protected $allowedConfigurations = []; |
||
| 56 | |||
| 57 | /** |
||
| 58 | * Each crawler run has a setid, this facade method delegates |
||
| 59 | * the it to the crawler object |
||
| 60 | * |
||
| 61 | * @param int |
||
| 62 | */ |
||
| 63 | 1 | public function overwriteSetId($id) |
|
| 67 | |||
| 68 | /** |
||
| 69 | * This method is used to limit the configuration selection to |
||
| 70 | * a set of configurations. |
||
| 71 | * |
||
| 72 | * @param array $allowedConfigurations |
||
| 73 | */ |
||
| 74 | 1 | public function setAllowedConfigurations(array $allowedConfigurations) |
|
| 78 | |||
| 79 | /** |
||
| 80 | * @return array |
||
| 81 | */ |
||
| 82 | 1 | public function getAllowedConfigurations() |
|
| 86 | |||
| 87 | /** |
||
| 88 | * Returns the setID of the crawler |
||
| 89 | * |
||
| 90 | * @return int |
||
| 91 | */ |
||
| 92 | 1 | public function getSetId() |
|
| 96 | |||
| 97 | /** |
||
| 98 | * Method to get an instance of the internal crawler singleton |
||
| 99 | * |
||
| 100 | * @return CrawlerController Instance of the crawler lib |
||
| 101 | * |
||
| 102 | * @throws \Exception |
||
| 103 | */ |
||
| 104 | 2 | protected function findCrawler() |
|
| 117 | |||
| 118 | /** |
||
| 119 | * Adds a page to the crawlerqueue by uid |
||
| 120 | * |
||
| 121 | * @param int $uid uid |
||
| 122 | */ |
||
| 123 | public function addPageToQueue($uid) |
||
| 129 | |||
| 130 | /** |
||
| 131 | * This method is used to limit the processing instructions to the processing instructions |
||
| 132 | * that are allowed. |
||
| 133 | * |
||
| 134 | * @return array |
||
| 135 | */ |
||
| 136 | protected function filterUnallowedConfigurations($configurations) |
||
| 137 | { |
||
| 138 | if (count($this->allowedConfigurations) > 0) { |
||
| 139 | // remove configuration that does not match the current selection |
||
| 140 | foreach ($configurations as $confKey => $confArray) { |
||
| 141 | if (!in_array($confKey, $this->allowedConfigurations)) { |
||
| 142 | unset($configurations[$confKey]); |
||
| 143 | } |
||
| 144 | } |
||
| 145 | } |
||
| 146 | |||
| 147 | return $configurations; |
||
| 148 | } |
||
| 149 | |||
| 150 | /** |
||
| 151 | * Adds a page to the crawlerqueue by uid and sets a |
||
| 152 | * timestamp when the page should be crawled. |
||
| 153 | * |
||
| 154 | * @param int $uid pageid |
||
| 155 | * @param int $time timestamp |
||
| 156 | */ |
||
| 157 | public function addPageToQueueTimed($uid, $time) |
||
| 158 | { |
||
| 159 | $uid = intval($uid); |
||
| 160 | $time = intval($time); |
||
| 161 | |||
| 162 | $crawler = $this->findCrawler(); |
||
| 163 | $pageData = GeneralUtility::makeInstance(PageRepository::class)->getPage($uid); |
||
| 164 | $configurations = $crawler->getUrlsForPageRow($pageData); |
||
| 165 | $configurations = $this->filterUnallowedConfigurations($configurations); |
||
| 166 | $downloadUrls = []; |
||
| 167 | $duplicateTrack = []; |
||
| 168 | |||
| 169 | if (is_array($configurations)) { |
||
| 170 | foreach ($configurations as $cv) { |
||
| 171 | //enable inserting of entries |
||
| 172 | $crawler->registerQueueEntriesInternallyOnly = false; |
||
| 173 | $crawler->urlListFromUrlArray( |
||
| 174 | $cv, |
||
| 175 | $pageData, |
||
| 176 | $time, |
||
| 177 | 300, |
||
| 178 | true, |
||
| 179 | false, |
||
| 180 | $duplicateTrack, |
||
| 181 | $downloadUrls, |
||
| 182 | array_keys($this->getCrawlerProcInstructions()) |
||
| 183 | ); |
||
| 184 | |||
| 185 | //reset the queue because the entries have been written to the db |
||
| 186 | unset($crawler->queueEntries); |
||
| 187 | } |
||
| 188 | } else { |
||
| 189 | //no configuration found |
||
| 190 | } |
||
| 191 | } |
||
| 192 | |||
| 193 | /** |
||
| 194 | * Counts all entrys in the database which are scheduled for a given page id and a schedule timestamp. |
||
| 195 | * |
||
| 196 | * @param int $page_uid |
||
| 197 | * @param int $schedule_timestamp |
||
| 198 | * |
||
| 199 | * @return int |
||
| 200 | */ |
||
| 201 | 1 | protected function countEntriesInQueueForPageByScheduleTime($page_uid, $schedule_timestamp) |
|
| 222 | |||
| 223 | /** |
||
| 224 | * Determines if a page is queued |
||
| 225 | * |
||
| 226 | * @param $uid |
||
| 227 | * @param bool $unprocessed_only |
||
| 228 | * @param bool $timed_only |
||
| 229 | * @param bool $timestamp |
||
| 230 | * |
||
| 231 | * @return bool |
||
| 232 | */ |
||
| 233 | 6 | public function isPageInQueue($uid, $unprocessed_only = true, $timed_only = false, $timestamp = false) |
|
| 267 | |||
| 268 | /** |
||
| 269 | * Method to return the latest Crawle Timestamp for a page. |
||
| 270 | * |
||
| 271 | * @param int $uid uid id of the page |
||
| 272 | * @param bool $future_crawldates_only |
||
| 273 | * @param bool $unprocessed_only |
||
| 274 | * |
||
| 275 | * @return int |
||
| 276 | */ |
||
| 277 | 1 | public function getLatestCrawlTimestampForPage($uid, $future_crawldates_only = false, $unprocessed_only = false) |
|
| 300 | |||
| 301 | /** |
||
| 302 | * Returns an array with timestamps when the page has been scheduled for crawling and |
||
| 303 | * at what time the scheduled crawl has been executed. The array also contains items that are |
||
| 304 | * scheduled but have note been crawled yet. |
||
| 305 | * |
||
| 306 | * @param int $uid uid of the page |
||
| 307 | * @param bool $limit |
||
| 308 | * |
||
| 309 | * @return array array with the crawl-history of a page => 0 : scheduled time , 1 : executed_time, 2 : set_id |
||
| 310 | */ |
||
| 311 | 1 | public function getCrawlHistoryForPage($uid, $limit = 0) |
|
| 324 | |||
| 325 | /** |
||
| 326 | * Method to determine unprocessed Items in the crawler queue. |
||
| 327 | * |
||
| 328 | * @return array |
||
| 329 | */ |
||
| 330 | public function getUnprocessedItems() |
||
| 331 | { |
||
| 332 | $query = '*'; |
||
| 333 | $where = 'exec_time = 0'; |
||
| 334 | $rows = $GLOBALS['TYPO3_DB']->exec_SELECTgetRows($query, 'tx_crawler_queue', $where, '', 'page_id, scheduled'); |
||
| 335 | |||
| 336 | return $rows; |
||
| 337 | } |
||
| 338 | |||
| 339 | /** |
||
| 340 | * Method to get the number of unprocessed items in the crawler |
||
| 341 | * |
||
| 342 | * @param int number of unprocessed items in the queue |
||
| 343 | */ |
||
| 344 | public function countUnprocessedItems() |
||
| 345 | { |
||
| 346 | $query = 'count(page_id) as anz'; |
||
| 347 | $where = 'exec_time = 0'; |
||
| 348 | $rs = $GLOBALS['TYPO3_DB']->exec_SELECTquery($query, 'tx_crawler_queue', $where); |
||
| 349 | $row = $GLOBALS['TYPO3_DB']->sql_fetch_assoc($rs); |
||
| 350 | |||
| 351 | return $row['anz']; |
||
| 352 | } |
||
| 353 | |||
| 354 | /** |
||
| 355 | * Method to check if a page is in the queue which is timed for a |
||
| 356 | * date when it should be crawled |
||
| 357 | * |
||
| 358 | * @param int $uid uid of the page |
||
| 359 | * @param boolean $show_unprocessed only respect unprocessed pages |
||
| 360 | * |
||
| 361 | * @return boolean |
||
| 362 | */ |
||
| 363 | 1 | public function isPageInQueueTimed($uid, $show_unprocessed = true) |
|
| 369 | |||
| 370 | /** |
||
| 371 | * Reads the registered processingInstructions of the crawler |
||
| 372 | * |
||
| 373 | * @return array |
||
| 374 | */ |
||
| 375 | private function getCrawlerProcInstructions() |
||
| 376 | { |
||
| 377 | if (isset($GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['crawler']['procInstructions'])) { |
||
| 378 | return $GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['crawler']['procInstructions']; |
||
| 379 | } |
||
| 380 | |||
| 381 | return []; |
||
| 382 | } |
||
| 383 | |||
| 384 | /** |
||
| 385 | * Removes an queue entry with a given queue id |
||
| 386 | * |
||
| 387 | * @param int $qid |
||
| 388 | */ |
||
| 389 | public function removeQueueEntrie($qid) |
||
| 396 | |||
| 397 | /** |
||
| 398 | * Get queue statistics |
||
| 399 | * |
||
| 400 | * @param void |
||
| 401 | * |
||
| 402 | * @return array array('assignedButUnprocessed' => <>, 'unprocessed' => <>); |
||
| 403 | */ |
||
| 404 | 1 | public function getQueueStatistics() |
|
| 411 | |||
| 412 | /** |
||
| 413 | * Get queue repository |
||
| 414 | * |
||
| 415 | * @return QueueRepository |
||
| 416 | */ |
||
| 417 | 2 | protected function getQueueRepository() |
|
| 425 | |||
| 426 | /** |
||
| 427 | * Get queue statistics by configuration |
||
| 428 | * |
||
| 429 | * @return array array of array('configuration' => <>, 'assignedButUnprocessed' => <>, 'unprocessed' => <>) |
||
| 430 | */ |
||
| 431 | public function getQueueStatisticsByConfiguration() |
||
| 446 | |||
| 447 | /** |
||
| 448 | * Get active processes count |
||
| 449 | * |
||
| 450 | * @param void |
||
| 451 | * |
||
| 452 | * @return int |
||
| 453 | */ |
||
| 454 | public function getActiveProcessesCount() |
||
| 460 | |||
| 461 | /** |
||
| 462 | * Get last processed entries |
||
| 463 | * |
||
| 464 | * @param int $limit |
||
| 465 | * |
||
| 466 | * @return array |
||
| 467 | */ |
||
| 468 | public function getLastProcessedQueueEntries($limit) |
||
| 472 | |||
| 473 | /** |
||
| 474 | * Get current crawling speed |
||
| 475 | * |
||
| 476 | * @param float|false page speed in pages per minute |
||
| 477 | * |
||
| 478 | * @return int |
||
| 479 | */ |
||
| 480 | public function getCurrentCrawlingSpeed() |
||
| 515 | |||
| 516 | /** |
||
| 517 | * Get some performance data |
||
| 518 | * |
||
| 519 | * @param integer $start |
||
| 520 | * @param integer $end |
||
| 521 | * @param integer $resolution |
||
| 522 | * |
||
| 523 | * @return array data |
||
| 524 | * |
||
| 525 | * @throws \Exception |
||
| 526 | */ |
||
| 527 | public function getPerformanceData($start, $end, $resolution) |
||
| 576 | } |
||
| 577 |
This checks looks for assignemnts to variables using the
list(...)function, where not all assigned variables are subsequently used.Consider the following code example.
Only the variables
$aand$care used. There was no need to assign$b.Instead, the list call could have been.