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 | 4 | protected function filterUnallowedConfigurations($configurations) |
|
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 | 4 | public function addPageToQueueTimed($uid, $time) |
|
192 | |||
193 | /** |
||
194 | * Counts all entries 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 | * @deprecated since crawler v6.2.0, will be removed in crawler v7.0.0. |
||
202 | */ |
||
203 | 1 | protected function countEntriesInQueueForPageByScheduleTime($page_uid, $schedule_timestamp) |
|
227 | |||
228 | /** |
||
229 | * Determines if a page is queued |
||
230 | * |
||
231 | * @param $uid |
||
232 | * @param bool $unprocessed_only |
||
233 | * @param bool $timed_only |
||
234 | * @param bool $timestamp |
||
235 | * |
||
236 | * @return bool |
||
237 | */ |
||
238 | 6 | public function isPageInQueue($uid, $unprocessed_only = true, $timed_only = false, $timestamp = false) |
|
272 | |||
273 | /** |
||
274 | * Method to return the latest Crawle Timestamp for a page. |
||
275 | * |
||
276 | * @param int $uid uid id of the page |
||
277 | * @param bool $future_crawldates_only |
||
278 | * @param bool $unprocessed_only |
||
279 | * |
||
280 | * @return int |
||
281 | */ |
||
282 | 1 | public function getLatestCrawlTimestampForPage($uid, $future_crawldates_only = false, $unprocessed_only = false) |
|
305 | |||
306 | /** |
||
307 | * Returns an array with timestamps when the page has been scheduled for crawling and |
||
308 | * at what time the scheduled crawl has been executed. The array also contains items that are |
||
309 | * scheduled but have note been crawled yet. |
||
310 | * |
||
311 | * @param int $uid uid of the page |
||
312 | * @param bool $limit |
||
313 | * |
||
314 | * @return array array with the crawl-history of a page => 0 : scheduled time , 1 : executed_time, 2 : set_id |
||
315 | */ |
||
316 | 1 | public function getCrawlHistoryForPage($uid, $limit = 0) |
|
329 | |||
330 | /** |
||
331 | * Method to determine unprocessed Items in the crawler queue. |
||
332 | * |
||
333 | * @return array |
||
334 | */ |
||
335 | 1 | public function getUnprocessedItems() |
|
343 | |||
344 | /** |
||
345 | * Method to get the number of unprocessed items in the crawler |
||
346 | * |
||
347 | * @param int number of unprocessed items in the queue |
||
348 | */ |
||
349 | 4 | public function countUnprocessedItems() |
|
358 | |||
359 | /** |
||
360 | * Method to check if a page is in the queue which is timed for a |
||
361 | * date when it should be crawled |
||
362 | * |
||
363 | * @param int $uid uid of the page |
||
364 | * @param boolean $show_unprocessed only respect unprocessed pages |
||
365 | * |
||
366 | * @return boolean |
||
367 | */ |
||
368 | 1 | public function isPageInQueueTimed($uid, $show_unprocessed = true) |
|
374 | |||
375 | /** |
||
376 | * Reads the registered processingInstructions of the crawler |
||
377 | * |
||
378 | * @return array |
||
379 | */ |
||
380 | 4 | private function getCrawlerProcInstructions() |
|
388 | |||
389 | /** |
||
390 | * Removes an queue entry with a given queue id |
||
391 | * |
||
392 | * @param int $qid |
||
393 | */ |
||
394 | public function removeQueueEntrie($qid) |
||
401 | |||
402 | /** |
||
403 | * Get queue statistics |
||
404 | * |
||
405 | * @param void |
||
406 | * |
||
407 | * @return array array('assignedButUnprocessed' => <>, 'unprocessed' => <>); |
||
408 | */ |
||
409 | 1 | public function getQueueStatistics() |
|
416 | |||
417 | /** |
||
418 | * Get queue repository |
||
419 | * |
||
420 | * @return QueueRepository |
||
421 | */ |
||
422 | 2 | protected function getQueueRepository() |
|
430 | |||
431 | /** |
||
432 | * Get queue statistics by configuration |
||
433 | * |
||
434 | * @return array array of array('configuration' => <>, 'assignedButUnprocessed' => <>, 'unprocessed' => <>) |
||
435 | */ |
||
436 | public function getQueueStatisticsByConfiguration() |
||
451 | |||
452 | /** |
||
453 | * Get active processes count |
||
454 | * |
||
455 | * @param void |
||
456 | * |
||
457 | * @return int |
||
458 | */ |
||
459 | public function getActiveProcessesCount() |
||
465 | |||
466 | /** |
||
467 | * Get last processed entries |
||
468 | * |
||
469 | * @param int $limit |
||
470 | * |
||
471 | * @return array |
||
472 | */ |
||
473 | public function getLastProcessedQueueEntries($limit) |
||
477 | |||
478 | /** |
||
479 | * Get current crawling speed |
||
480 | * |
||
481 | * @param float|false page speed in pages per minute |
||
482 | * |
||
483 | * @return int |
||
484 | */ |
||
485 | public function getCurrentCrawlingSpeed() |
||
520 | |||
521 | /** |
||
522 | * Get some performance data |
||
523 | * |
||
524 | * @param integer $start |
||
525 | * @param integer $end |
||
526 | * @param integer $resolution |
||
527 | * |
||
528 | * @return array data |
||
529 | * |
||
530 | * @throws \Exception |
||
531 | */ |
||
532 | public function getPerformanceData($start, $end, $resolution) |
||
581 | } |
||
582 |