Conditions | 18 |
Paths | 129 |
Total Lines | 153 |
Code Lines | 84 |
Lines | 0 |
Ratio | 0 % |
Changes | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
1 | <?php |
||
221 | private function scrape(Target $target, ?Crawler $crawler): void |
||
222 | { |
||
223 | if ($crawler === null) { |
||
224 | return; |
||
225 | } |
||
226 | |||
227 | $markup = $target->getMarkup(); |
||
228 | $titleLinkSelector = $markup[TargetKey::MARKUP_TITLE]; |
||
229 | $markupInside = $markup[TargetKey::special(TargetKey::MARKUP_INSIDE)] ?? []; |
||
230 | $markupHasInside = !empty($markupInside); |
||
231 | $markupHasItemWrapper = false; |
||
232 | |||
233 | // choose link as title link selector if it is not empty and it is not set to a special key |
||
234 | if (!(empty($markup[TargetKey::MARKUP_LINK]) || ConfigProvider::isSpecialKey( |
||
235 | $markup[TargetKey::MARKUP_LINK] |
||
236 | ))) { |
||
237 | $titleLinkSelector = $markup[TargetKey::MARKUP_LINK]; |
||
238 | } |
||
239 | |||
240 | // determine what item wrapper is |
||
241 | $markup[TargetKey::special(TargetKey::ITEM_WRAPPER)] = Scanner::firstNonEmpty( |
||
242 | $markup, |
||
243 | [ |
||
244 | TargetKey::special(TargetKey::ITEM_WRAPPER), |
||
245 | TargetKey::special(TargetKey::RESULT), |
||
246 | TargetKey::special(TargetKey::ITEM), |
||
247 | TargetKey::special(TargetKey::WRAPPER), |
||
248 | ] |
||
249 | ); |
||
250 | if (!empty($markup[TargetKey::special(TargetKey::ITEM_WRAPPER)])) { |
||
251 | $markupHasItemWrapper = true; |
||
252 | } |
||
253 | |||
254 | // Page by page we go... |
||
255 | $page = self::INITIAL_PAGE; |
||
256 | |||
257 | do { |
||
258 | $this->tell( |
||
259 | FormattedMessage::get(FormattedMessage::PROCESSING_PAGE_N, $page), |
||
260 | self::COMM_DIRECTION_NONE |
||
261 | ); |
||
262 | |||
263 | // scrape each by carving the insides |
||
264 | $items = $crawler->filter($titleLinkSelector); |
||
265 | |||
266 | if ($markupHasItemWrapper) { |
||
267 | $items = $crawler->filter($markup[TargetKey::special(TargetKey::ITEM_WRAPPER)]); |
||
268 | } |
||
269 | |||
270 | if (!$items->count()) { |
||
271 | $this->tell( |
||
272 | FormattedMessage::get(FormattedMessage::NO_ITEMS_FOUND_ON_PAGE_N, $page) |
||
273 | ); |
||
274 | } |
||
275 | |||
276 | $items->each( |
||
277 | function (Crawler $itemCrawler) use ( |
||
278 | $markupHasInside, |
||
279 | $markupInside, |
||
280 | $target, |
||
281 | $titleLinkSelector |
||
282 | ) { |
||
283 | $cursor = $target->getCursor(); |
||
284 | |||
285 | try { |
||
286 | $titleLinkCrawler = $markupHasInside ? $itemCrawler : $itemCrawler->filter($titleLinkSelector); |
||
287 | $titleLinkText = Scanner::cleanText($titleLinkCrawler->text()); |
||
288 | $linkCrawler = $titleLinkCrawler->selectLink($titleLinkText); |
||
289 | |||
290 | // link crawler is empty in this case the link may be a parent element |
||
291 | // so we must find closest link: |
||
292 | if (!count($linkCrawler)) { |
||
293 | $linkCrawler = $this->nodeProximityAssistant->closest('a[href]', $titleLinkCrawler); |
||
294 | } |
||
295 | |||
296 | // simply get link from crawler |
||
297 | $link = $linkCrawler->link(); |
||
298 | |||
299 | $this->tell($titleLinkText, self::COMM_DIRECTION_FLAT); |
||
300 | } catch (InvalidArgumentException $e) { |
||
301 | $this->tell( |
||
302 | FormattedMessage::get( |
||
303 | ($markupHasInside |
||
304 | ? FormattedMessage::UNABLE_TO_RETRIEVE_SCRAP_FOR_X |
||
305 | : FormattedMessage::NO_TITLE_LINK_FOUND_FOR_X), |
||
306 | $cursor + 1 |
||
307 | ) |
||
308 | ); |
||
309 | $this->logger->error($e); |
||
310 | |||
311 | // escape 'each' block |
||
312 | return; |
||
313 | } |
||
314 | |||
315 | $target->incrementCursor(); |
||
316 | |||
317 | if ($markupHasInside) { |
||
318 | // grab handle on detail |
||
319 | $itemCrawler = $this->client->click($link); |
||
320 | $markupHasFocus = !empty($markupInside[TargetKey::special(TargetKey::MARKUP_FOCUS)]); |
||
321 | |||
322 | // focus detail crawler on section if specified |
||
323 | if ($markupHasFocus) { |
||
324 | $itemCrawler = $itemCrawler->filter( |
||
325 | $markupInside[TargetKey::special(TargetKey::MARKUP_FOCUS)] |
||
326 | ); |
||
327 | } |
||
328 | } |
||
329 | |||
330 | // collect the scrap... |
||
331 | $this->scrapper->collect( |
||
332 | new TitleLink($titleLinkText, $link->getUri()), |
||
333 | $target, |
||
334 | $itemCrawler |
||
335 | ); |
||
336 | } |
||
337 | ); |
||
338 | |||
339 | if ($page < $this->pageLimit && $target->hasPager()) { |
||
340 | // Look for next page. |
||
341 | // An InvalidArgumentException may be thrown if a 'next' link does not exist. |
||
342 | try { |
||
343 | // Select pager |
||
344 | $pager = $crawler->filter($target->getPager()[TargetKey::PAGER_SELECTOR]); |
||
345 | // Grab pager/next link |
||
346 | $nextLink = $pager->link(); |
||
347 | // Click it! |
||
348 | $crawler = $this->client->click($nextLink); |
||
349 | } catch (InvalidArgumentException $e) { |
||
350 | // Next link doesn't exist |
||
351 | $crawler = null; |
||
352 | |||
353 | if ($this->verbosity >= self::VERBOSITY_HIGH) { |
||
354 | $errorMessage = FormattedMessage::get( |
||
355 | FormattedMessage::TARGET_PAGER_NEXT_NOT_FOUND, |
||
356 | $target->getName(), |
||
357 | $target->getPager()[TargetKey::PAGER_SELECTOR], |
||
358 | $target->getPager()[TargetKey::PAGER_TEXT] |
||
359 | ); |
||
360 | |||
361 | $this->tell($errorMessage); |
||
362 | $this->logger->error($errorMessage); |
||
363 | } |
||
364 | } |
||
365 | |||
366 | ++$page; |
||
367 | } else { |
||
368 | $crawler = null; |
||
369 | } |
||
370 | |||
371 | // back-off |
||
372 | sleep($this->optionSet->getBackOff()); |
||
373 | } while ($crawler !== null); // unless Crawler died... |
||
374 | } |
||
388 |