Total Complexity | 44 |
Total Lines | 356 |
Duplicated Lines | 0 % |
Changes | 0 |
Complex classes like Seeker 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 Seeker, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
27 | final class Seeker extends Communicator implements SeekerInterface |
||
28 | { |
||
29 | use Timed; |
||
30 | |||
31 | private const INITIAL_PAGE = 1; |
||
32 | |||
33 | protected OptionSet $optionSet; |
||
34 | protected GoutteClient $client; |
||
35 | |||
36 | private array $targetDefinitions; |
||
37 | private LoggerInterface $logger; |
||
38 | private NodeProximityAssistant $nodeProximityAssistant; |
||
39 | private int $pageLimit; |
||
40 | private Scrapper $scrapper; |
||
41 | |||
42 | /** |
||
43 | * Create a new seeker. |
||
44 | * |
||
45 | * @throws Exception |
||
46 | */ |
||
47 | public function __construct( |
||
66 | ); |
||
67 | } |
||
68 | |||
69 | /** |
||
70 | * {@inheritdoc} |
||
71 | */ |
||
72 | public function seek( |
||
142 | } |
||
143 | |||
144 | private function crawlTarget(Target $target): void |
||
159 | } |
||
160 | |||
161 | private function searchAndScrape(Target $target, Crawler $crawler): void |
||
162 | { |
||
163 | if ($this->verbosity >= self::VERBOSITY_MEDIUM) { |
||
164 | $this->logger->info('Landing Document', [$crawler->html()]); |
||
165 | } |
||
166 | |||
167 | $searchFormConfig = $target->getSearch()[TargetKey::SEARCH_FORM]; |
||
168 | $searchFormSubmitButtonConfig = $searchFormConfig[TargetKey::SEARCH_FORM_SUBMIT_BUTTON] ?? []; |
||
169 | $formCrawler = $crawler->filter($searchFormConfig[TargetKey::SEARCH_FORM_SELECTOR]); |
||
170 | $submitButtonIdentifier = null; |
||
171 | $form = null; |
||
|
|||
172 | |||
173 | // check if submit button was defined |
||
174 | if (!empty($searchFormSubmitButtonConfig[TargetKey::SEARCH_FORM_SUBMIT_BUTTON_ID])) { |
||
175 | $submitButtonIdentifier = $searchFormSubmitButtonConfig[TargetKey::SEARCH_FORM_SUBMIT_BUTTON_ID]; |
||
176 | } elseif (!empty($searchFormSubmitButtonConfig[TargetKey::SEARCH_FORM_SUBMIT_BUTTON_TEXT])) { |
||
177 | $submitButtonIdentifier = $searchFormSubmitButtonConfig[TargetKey::SEARCH_FORM_SUBMIT_BUTTON_TEXT]; |
||
178 | } |
||
179 | |||
180 | try { |
||
181 | $form = empty($submitButtonIdentifier) |
||
182 | ? $formCrawler->form() |
||
183 | : $formCrawler->selectButton((string)$submitButtonIdentifier)->form(); |
||
184 | } catch (InvalidArgumentException $e) { |
||
185 | $this->logger->error($e); |
||
186 | } |
||
187 | |||
188 | if ($form instanceof Form) { |
||
189 | if ($this->verbosity >= self::VERBOSITY_MEDIUM) { |
||
190 | $this->logger->info('FORM', [$form]); |
||
191 | } |
||
192 | |||
193 | // Search each phrase/keyword one at a time. |
||
194 | foreach ($target->getSearch()[TargetKey::SEARCH_KEYWORDS] as $keyword) { |
||
195 | // set keyword |
||
196 | $form[$searchFormConfig[TargetKey::SEARCH_FORM_INPUT]] = $keyword; |
||
197 | |||
198 | $this->tell(FormattedMessage::get(FormattedMessage::SEARCH_KEYWORD, $keyword), self::COMM_DIRECTION_IN); |
||
199 | // submit search form |
||
200 | $resultCrawler = $this->client->submit($form); |
||
201 | |||
202 | if ($this->verbosity >= self::VERBOSITY_MEDIUM) { |
||
203 | $this->logger->info( |
||
204 | FormattedMessage::get( |
||
205 | FormattedMessage::FIRST_RESULT_PAGE_FOR_KEYWORD_ON_TARGET, |
||
206 | $keyword, |
||
207 | $target->getName() |
||
208 | ), |
||
209 | [$resultCrawler === null ? '[NULL]' : $resultCrawler->html()] |
||
210 | ); |
||
211 | } |
||
212 | |||
213 | // crawl |
||
214 | $this->scrape($target, $resultCrawler); |
||
215 | } |
||
216 | } else { |
||
217 | $this->tell(FormattedMessage::get(FormattedMessage::TARGET_INVALID_SEARCH_FORM, $target->getName())); |
||
218 | } |
||
219 | } |
||
220 | |||
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 | } |
||
375 | |||
376 | private function getTargetBuilder(): TargetBuilder |
||
383 | ) |
||
384 | ) |
||
385 | ); |
||
388 |