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 | * @throws \Exception |
||
158 | */ |
||
159 | 4 | public function addPageToQueueTimed($uid, $time) |
|
194 | |||
195 | /** |
||
196 | * Counts all entries in the database which are scheduled for a given page id and a schedule timestamp. |
||
197 | * |
||
198 | * @param int $page_uid |
||
199 | * @param int $schedule_timestamp |
||
200 | * |
||
201 | * @return int |
||
202 | * |
||
203 | * @deprecated since crawler v6.2.0, will be removed in crawler v7.0.0. |
||
204 | */ |
||
205 | 1 | protected function countEntriesInQueueForPageByScheduleTime($page_uid, $schedule_timestamp) |
|
229 | |||
230 | /** |
||
231 | * Determines if a page is queued |
||
232 | * |
||
233 | * @param $uid |
||
234 | * @param bool $unprocessed_only |
||
235 | * @param bool $timed_only |
||
236 | * @param bool $timestamp |
||
237 | * |
||
238 | * @return bool |
||
239 | */ |
||
240 | 6 | public function isPageInQueue($uid, $unprocessed_only = true, $timed_only = false, $timestamp = false) |
|
274 | |||
275 | /** |
||
276 | * Method to return the latest Crawle Timestamp for a page. |
||
277 | * |
||
278 | * @param int $uid uid id of the page |
||
279 | * @param bool $future_crawldates_only |
||
280 | * @param bool $unprocessed_only |
||
281 | * |
||
282 | * @return int |
||
283 | */ |
||
284 | 1 | public function getLatestCrawlTimestampForPage($uid, $future_crawldates_only = false, $unprocessed_only = false) |
|
307 | |||
308 | /** |
||
309 | * Returns an array with timestamps when the page has been scheduled for crawling and |
||
310 | * at what time the scheduled crawl has been executed. The array also contains items that are |
||
311 | * scheduled but have note been crawled yet. |
||
312 | * |
||
313 | * @param int $uid uid of the page |
||
314 | * @param bool $limit |
||
315 | * |
||
316 | * @return array array with the crawl-history of a page => 0 : scheduled time , 1 : executed_time, 2 : set_id |
||
317 | */ |
||
318 | 1 | public function getCrawlHistoryForPage($uid, $limit = 0) |
|
331 | |||
332 | /** |
||
333 | * Method to determine unprocessed Items in the crawler queue. |
||
334 | * |
||
335 | * @return array |
||
336 | */ |
||
337 | 1 | public function getUnprocessedItems() |
|
349 | |||
350 | /** |
||
351 | * Method to get the number of unprocessed items in the crawler |
||
352 | * |
||
353 | * @param int number of unprocessed items in the queue |
||
354 | */ |
||
355 | 4 | public function countUnprocessedItems() |
|
364 | |||
365 | /** |
||
366 | * Method to check if a page is in the queue which is timed for a |
||
367 | * date when it should be crawled |
||
368 | * |
||
369 | * @param int $uid uid of the page |
||
370 | * @param boolean $show_unprocessed only respect unprocessed pages |
||
371 | * |
||
372 | * @return boolean |
||
373 | */ |
||
374 | 1 | public function isPageInQueueTimed($uid, $show_unprocessed = true) |
|
380 | |||
381 | /** |
||
382 | * Reads the registered processingInstructions of the crawler |
||
383 | * |
||
384 | * @return array |
||
385 | */ |
||
386 | 4 | private function getCrawlerProcInstructions() |
|
394 | |||
395 | /** |
||
396 | * Removes an queue entry with a given queue id |
||
397 | * |
||
398 | * @param int $qid |
||
399 | */ |
||
400 | public function removeQueueEntrie($qid) |
||
407 | |||
408 | /** |
||
409 | * Get queue statistics |
||
410 | * |
||
411 | * @param void |
||
412 | * |
||
413 | * @return array array('assignedButUnprocessed' => <>, 'unprocessed' => <>); |
||
414 | */ |
||
415 | 1 | public function getQueueStatistics() |
|
422 | |||
423 | /** |
||
424 | * Get queue repository |
||
425 | * |
||
426 | * @return QueueRepository |
||
427 | */ |
||
428 | 2 | protected function getQueueRepository() |
|
436 | |||
437 | /** |
||
438 | * Get queue statistics by configuration |
||
439 | * |
||
440 | * @return array array of array('configuration' => <>, 'assignedButUnprocessed' => <>, 'unprocessed' => <>) |
||
441 | */ |
||
442 | public function getQueueStatisticsByConfiguration() |
||
457 | |||
458 | /** |
||
459 | * Get active processes count |
||
460 | * |
||
461 | * @param void |
||
462 | * |
||
463 | * @return int |
||
464 | */ |
||
465 | public function getActiveProcessesCount() |
||
471 | |||
472 | /** |
||
473 | * Get last processed entries |
||
474 | * |
||
475 | * @param int $limit |
||
476 | * |
||
477 | * @return array |
||
478 | */ |
||
479 | public function getLastProcessedQueueEntries($limit) |
||
483 | |||
484 | /** |
||
485 | * Get current crawling speed |
||
486 | * |
||
487 | * @param float|false page speed in pages per minute |
||
488 | * |
||
489 | * @return int |
||
490 | */ |
||
491 | public function getCurrentCrawlingSpeed() |
||
526 | |||
527 | /** |
||
528 | * Get some performance data |
||
529 | * |
||
530 | * @param integer $start |
||
531 | * @param integer $end |
||
532 | * @param integer $resolution |
||
533 | * |
||
534 | * @return array data |
||
535 | * |
||
536 | * @throws \Exception |
||
537 | */ |
||
538 | public function getPerformanceData($start, $end, $resolution) |
||
587 | } |
||
588 |