Total Complexity | 55 |
Total Lines | 532 |
Duplicated Lines | 0 % |
Changes | 0 |
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.
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 |
||
37 | class CrawlerApi |
||
38 | { |
||
39 | /** |
||
40 | * @var \tx_crawler_lib |
||
41 | */ |
||
42 | private $crawlerObj; |
||
43 | |||
44 | /** |
||
45 | * @var \tx_crawler_domain_queue_repository queue repository |
||
46 | */ |
||
47 | protected $queueRepository; |
||
48 | |||
49 | /** |
||
50 | * @var $allowedConfigrations array |
||
51 | */ |
||
52 | protected $allowedConfigrations = []; |
||
53 | |||
54 | /** |
||
55 | * Each crawler run has a setid, this facade method delegates |
||
56 | * the it to the crawler object |
||
57 | * |
||
58 | * @param int |
||
59 | */ |
||
60 | public function overwriteSetId($id) |
||
61 | { |
||
62 | $this->findCrawler()->setID = intval($id); |
||
63 | } |
||
64 | |||
65 | /** |
||
66 | * This method is used to limit the configuration selection to |
||
67 | * a set of configurations. |
||
68 | * |
||
69 | * @param array $allowedConfigurations |
||
70 | */ |
||
71 | public function setAllowedConfigurations(array $allowedConfigurations) |
||
72 | { |
||
73 | $this->allowedConfigrations = $allowedConfigurations; |
||
74 | } |
||
75 | |||
76 | /** |
||
77 | * Returns the setID of the crawler |
||
78 | * |
||
79 | * @return int |
||
80 | */ |
||
81 | public function getSetId() |
||
82 | { |
||
83 | return $this->findCrawler()->setID; |
||
84 | } |
||
85 | |||
86 | /** |
||
87 | * Method to get an instance of the internal crawler singleton |
||
88 | * |
||
89 | * @return \tx_crawler_lib Instance of the crawler lib |
||
90 | * |
||
91 | * @throws \Exception |
||
92 | */ |
||
93 | protected function findCrawler() |
||
94 | { |
||
95 | if (!is_object($this->crawlerObj)) { |
||
96 | $this->crawlerObj = GeneralUtility::makeInstance(\tx_crawler_lib::class); |
||
97 | $this->crawlerObj->setID = GeneralUtility::md5int(microtime()); |
||
98 | } |
||
99 | |||
100 | if (is_object($this->crawlerObj)) { |
||
101 | return $this->crawlerObj; |
||
102 | } else { |
||
103 | throw new \Exception('no crawler object', 1512659759); |
||
104 | } |
||
105 | } |
||
106 | |||
107 | /** |
||
108 | * Adds a page to the crawlerqueue by uid |
||
109 | * |
||
110 | * @param int $uid uid |
||
111 | */ |
||
112 | public function addPageToQueue($uid) |
||
117 | } |
||
118 | |||
119 | /** |
||
120 | * This method is used to limit the processing instructions to the processing instructions |
||
121 | * that are allowed. |
||
122 | * |
||
123 | * @return array |
||
124 | */ |
||
125 | protected function filterUnallowedConfigurations($configurations) |
||
137 | } |
||
138 | |||
139 | /** |
||
140 | * Adds a page to the crawlerqueue by uid and sets a |
||
141 | * timestamp when the page should be crawled. |
||
142 | * |
||
143 | * @param int $uid pageid |
||
144 | * @param int $time timestamp |
||
145 | */ |
||
146 | public function addPageToQueueTimed($uid, $time) |
||
178 | //no configuration found |
||
179 | } |
||
180 | } |
||
181 | |||
182 | /** |
||
183 | * Counts all entrys in the database which are scheduled for a given page id and a schedule timestamp. |
||
184 | * |
||
185 | * @param int $page_uid |
||
186 | * @param int $schedule_timestamp |
||
187 | * |
||
188 | * @return int |
||
189 | */ |
||
190 | protected function countEntriesInQueueForPageByScheduletime($page_uid, $schedule_timestamp) |
||
191 | { |
||
192 | //if the same page is scheduled for the same time and has not be executed? |
||
193 | if ($schedule_timestamp == 0) { |
||
194 | //untimed elements need an exec_time with 0 because they can occure multiple times |
||
195 | $where = 'page_id=' . $page_uid . ' AND exec_time = 0 AND scheduled=' . $schedule_timestamp; |
||
196 | } else { |
||
197 | //timed elementes have got a fixed schedule time, if a record with this time |
||
198 | //exists it is maybe queued for the future, or is has been queue for the past and therefore |
||
199 | //also been processed. |
||
200 | $where = 'page_id=' . $page_uid . ' AND scheduled=' . $schedule_timestamp; |
||
201 | } |
||
202 | |||
203 | $row = $GLOBALS['TYPO3_DB']->sql_fetch_assoc($GLOBALS['TYPO3_DB']->exec_SELECTquery( |
||
204 | 'count(*) as cnt', |
||
205 | 'tx_crawler_queue', |
||
206 | $where |
||
207 | )); |
||
208 | |||
209 | return intval($row['cnt']); |
||
210 | } |
||
211 | |||
212 | /** |
||
213 | * Determines if a page is queued |
||
214 | * |
||
215 | * @param $uid |
||
216 | * @param bool $unprocessed_only |
||
217 | * @param bool $timed_only |
||
218 | * @param bool $timestamp |
||
219 | * |
||
220 | * @return bool |
||
221 | */ |
||
222 | public function isPageInQueue($uid, $unprocessed_only = true, $timed_only = false, $timestamp = false) |
||
223 | { |
||
224 | if (MathUtility::canBeInterpretedAsInteger($uid)) { |
||
225 | throw new \InvalidArgumentException('Invalid parameter type', 1468931945); |
||
226 | } |
||
227 | |||
228 | $isPageInQueue = false; |
||
229 | |||
230 | $whereClause = 'page_id = ' . (integer)$uid; |
||
231 | |||
232 | if (false !== $unprocessed_only) { |
||
233 | $whereClause .= ' AND exec_time = 0'; |
||
234 | } |
||
235 | |||
236 | if (false !== $timed_only) { |
||
237 | $whereClause .= ' AND scheduled != 0'; |
||
238 | } |
||
239 | |||
240 | if (false !== $timestamp) { |
||
241 | $whereClause .= ' AND scheduled = ' . (integer)$timestamp; |
||
242 | } |
||
243 | |||
244 | $count = $GLOBALS['TYPO3_DB']->exec_SELECTcountRows( |
||
245 | '*', |
||
246 | 'tx_crawler_queue', |
||
247 | $whereClause |
||
248 | ); |
||
249 | |||
250 | if (false !== $count && $count > 0) { |
||
251 | $isPageInQueue = true; |
||
252 | } |
||
253 | |||
254 | return $isPageInQueue; |
||
255 | } |
||
256 | |||
257 | /** |
||
258 | * Method to return the latest Crawle Timestamp for a page. |
||
259 | * |
||
260 | * @param int $uid uid id of the page |
||
261 | * @param bool $future_crawldates_only |
||
262 | * @param bool $unprocessed_only |
||
263 | * |
||
264 | * @return int |
||
265 | */ |
||
266 | public function getLatestCrawlTimestampForPage($uid, $future_crawldates_only = false, $unprocessed_only = false) |
||
267 | { |
||
268 | $uid = intval($uid); |
||
269 | $query = 'max(scheduled) as latest'; |
||
270 | $where = ' page_id = ' . $uid; |
||
271 | |||
272 | if ($future_crawldates_only) { |
||
273 | $where .= ' AND scheduled > ' . time(); |
||
274 | } |
||
275 | |||
276 | if ($unprocessed_only) { |
||
277 | $where .= ' AND exec_time = 0'; |
||
278 | } |
||
279 | |||
280 | $rs = $GLOBALS['TYPO3_DB']->exec_SELECTquery($query, 'tx_crawler_queue', $where); |
||
281 | if ($row = $GLOBALS['TYPO3_DB']->sql_fetch_assoc($rs)) { |
||
282 | $res = $row['latest']; |
||
283 | } else { |
||
284 | $res = 0; |
||
285 | } |
||
286 | |||
287 | return $res; |
||
288 | } |
||
289 | |||
290 | /** |
||
291 | * Returns an array with timestamps when the page has been scheduled for crawling and |
||
292 | * at what time the scheduled crawl has been executed. The array also contains items that are |
||
293 | * scheduled but have note been crawled yet. |
||
294 | * |
||
295 | * @param int $uid uid of the page |
||
296 | * @param bool $limit |
||
297 | * |
||
298 | * @return array array with the crawlhistory of a page => 0 : scheduled time , 1 : execuded_time, 2 : set_id |
||
299 | */ |
||
300 | public function getCrawlHistoryForPage($uid, $limit = false) |
||
301 | { |
||
302 | $uid = intval($uid); |
||
303 | $limit = $GLOBALS['TYPO3_DB']->fullQuoteStr($limit, 'tx_crawler_queue'); |
||
304 | |||
305 | $query = 'scheduled, exec_time, set_id'; |
||
306 | $where = ' page_id = ' . $uid; |
||
307 | |||
308 | $limit_query = ($limit) ? $limit : null; |
||
309 | |||
310 | $rows = $GLOBALS['TYPO3_DB']->exec_SELECTgetRows($query, 'tx_crawler_queue', $where, null, null, $limit_query); |
||
311 | |||
312 | return $rows; |
||
313 | } |
||
314 | |||
315 | /** |
||
316 | * Method to determine unprocessed Items in the crawler queue. |
||
317 | * |
||
318 | * @return array |
||
319 | */ |
||
320 | public function getUnprocessedItems() |
||
321 | { |
||
322 | $query = '*'; |
||
323 | $where = 'exec_time = 0'; |
||
324 | $rows = $GLOBALS['TYPO3_DB']->exec_SELECTgetRows($query, 'tx_crawler_queue', $where, '', 'page_id, scheduled'); |
||
325 | |||
326 | return $rows; |
||
327 | } |
||
328 | |||
329 | /** |
||
330 | * Method to get the number of unprocessed items in the crawler |
||
331 | * |
||
332 | * @param int number of unprocessed items in the queue |
||
333 | */ |
||
334 | public function countUnprocessedItems() |
||
335 | { |
||
336 | $query = 'count(page_id) as anz'; |
||
337 | $where = 'exec_time = 0'; |
||
338 | $rs = $GLOBALS['TYPO3_DB']->exec_SELECTquery($query, 'tx_crawler_queue', $where); |
||
339 | $row = $GLOBALS['TYPO3_DB']->sql_fetch_assoc($rs); |
||
340 | |||
341 | return $row['anz']; |
||
342 | } |
||
343 | |||
344 | /** |
||
345 | * Method to check if a page is in the queue which is timed for a |
||
346 | * date when it should be crawled |
||
347 | * |
||
348 | * @param int $uid uid of the page |
||
349 | * @param boolean $show_unprocessed only respect unprocessed pages |
||
350 | * |
||
351 | * @return boolean |
||
352 | */ |
||
353 | public function isPageInQueueTimed($uid, $show_unprocessed = true) |
||
354 | { |
||
355 | $uid = intval($uid); |
||
356 | |||
357 | return $this->isPageInQueue($uid, $show_unprocessed); |
||
358 | } |
||
359 | |||
360 | /** |
||
361 | * Reads the registered processingInstructions of the crawler |
||
362 | * |
||
363 | * @return array |
||
364 | */ |
||
365 | private function getCrawlerProcInstructions() |
||
366 | { |
||
367 | if (isset($GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['crawler']['procInstructions'])) { |
||
368 | return $GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['crawler']['procInstructions']; |
||
369 | } |
||
370 | |||
371 | return []; |
||
372 | } |
||
373 | |||
374 | /** |
||
375 | * Removes an queue entry with a given queue id |
||
376 | * |
||
377 | * @param int $qid |
||
378 | */ |
||
379 | public function removeQueueEntrie($qid) |
||
380 | { |
||
381 | $qid = intval($qid); |
||
382 | $table = 'tx_crawler_queue'; |
||
383 | $where = ' qid=' . $qid; |
||
384 | $GLOBALS['TYPO3_DB']->exec_DELETEquery($table, $where); |
||
385 | } |
||
386 | |||
387 | /** |
||
388 | * Get queue statistics |
||
389 | * |
||
390 | * @param void |
||
391 | * |
||
392 | * @return array array('assignedButUnprocessed' => <>, 'unprocessed' => <>); |
||
393 | */ |
||
394 | public function getQueueStatistics() |
||
395 | { |
||
396 | return [ |
||
397 | 'assignedButUnprocessed' => $this->getQueueRepository()->countAllAssignedPendingItems(), |
||
398 | 'unprocessed' => $this->getQueueRepository()->countAllPendingItems() |
||
399 | ]; |
||
400 | } |
||
401 | |||
402 | /** |
||
403 | * Get queue repository |
||
404 | * |
||
405 | * @param void |
||
406 | * |
||
407 | * @return \tx_crawler_domain_queue_repository queue repository |
||
408 | */ |
||
409 | protected function getQueueRepository() |
||
410 | { |
||
411 | if (!$this->queueRepository instanceof \tx_crawler_domain_queue_repository) { |
||
412 | $this->queueRepository = new \tx_crawler_domain_queue_repository(); |
||
413 | } |
||
414 | |||
415 | return $this->queueRepository; |
||
416 | } |
||
417 | |||
418 | /** |
||
419 | * Get queue statistics by configuration |
||
420 | * |
||
421 | * @param void |
||
422 | * |
||
423 | * @return array array of array('configuration' => <>, 'assignedButUnprocessed' => <>, 'unprocessed' => <>) |
||
424 | */ |
||
425 | public function getQueueStatisticsByConfiguration() |
||
426 | { |
||
427 | $statistics = $this->getQueueRepository()->countPendingItemsGroupedByConfigurationKey(); |
||
428 | |||
429 | $setIds = $this->getQueueRepository()->getSetIdWithUnprocessedEntries(); |
||
430 | |||
431 | $totals = $this->getQueueRepository()->getTotalQueueEntriesByConfiguration($setIds); |
||
432 | |||
433 | // "merge" arrays |
||
434 | foreach ($statistics as $key => &$value) { |
||
435 | $value['total'] = $totals[$value['configuration']]; |
||
436 | } |
||
437 | |||
438 | return $statistics; |
||
439 | } |
||
440 | |||
441 | /** |
||
442 | * Get active processes count |
||
443 | * |
||
444 | * @param void |
||
445 | * |
||
446 | * @return int |
||
447 | */ |
||
448 | public function getActiveProcessesCount() |
||
449 | { |
||
450 | $processRepository = new \tx_crawler_domain_process_repository(); |
||
451 | |||
452 | return $processRepository->countActive(); |
||
453 | } |
||
454 | |||
455 | /** |
||
456 | * Get last processed entries |
||
457 | * |
||
458 | * @param int limit |
||
459 | * |
||
460 | * @return array |
||
461 | */ |
||
462 | public function getLastProcessedQueueEntries($limit) |
||
463 | { |
||
464 | return $this->getQueueRepository()->getLastProcessedEntries('*', $limit); |
||
465 | } |
||
466 | |||
467 | /** |
||
468 | * Get current crawling speed |
||
469 | * |
||
470 | * @param float|false page speed in pages per minute |
||
471 | * |
||
472 | * @return int |
||
473 | */ |
||
474 | public function getCurrentCrawlingSpeed() |
||
508 | } |
||
509 | |||
510 | /** |
||
511 | * Get some performance data |
||
512 | * |
||
513 | * @param integer $start |
||
514 | * @param integer $end |
||
515 | * @param integer $resolution |
||
516 | * |
||
517 | * @return array data |
||
518 | * |
||
519 | * @throws \Exception |
||
520 | */ |
||
521 | public function getPerformanceData($start, $end, $resolution) |
||
569 | } |
||
570 | } |
||
571 |
The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g.
excluded_paths: ["lib/*"]
, you can move it to the dependency path list as follows:For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths