| Total Complexity | 8 |
| Total Lines | 39 |
| Duplicated Lines | 0 % |
| Coverage | 95.24% |
| Changes | 1 | ||
| Bugs | 0 | Features | 0 |
| 1 | <?php |
||
| 11 | final readonly class ScrapingReviewerLinkExtractor |
||
|
|
|||
| 12 | { |
||
| 13 | 17 | public function __construct(private Crawler $crawler) |
|
| 14 | { |
||
| 15 | 17 | } |
|
| 16 | |||
| 17 | 18 | public static function fromUrlResponse(UrlResponse $response): self |
|
| 18 | { |
||
| 19 | 18 | if (empty($response->body())) { |
|
| 20 | 1 | throw new RuntimeException('Content is empty'); |
|
| 21 | } |
||
| 22 | 17 | $crawler = new Crawler($response->body(), $response->url()); |
|
| 23 | 17 | return new self($crawler); |
|
| 24 | } |
||
| 25 | |||
| 26 | 17 | public function search(string $search, int $position = 0): string |
|
| 27 | { |
||
| 28 | 17 | $link = $this->selectLink($search, $position); |
|
| 29 | 17 | $downloadUrl = $link->getUri(); |
|
| 30 | |||
| 31 | 17 | if (empty($downloadUrl)) { |
|
| 32 | throw new RuntimeException('The link was found but it does not contains the url to download'); |
||
| 33 | } |
||
| 34 | |||
| 35 | 17 | return $downloadUrl; |
|
| 36 | } |
||
| 37 | |||
| 38 | 17 | public function selectLink(string $search, int $position = 0): Link |
|
| 39 | { |
||
| 40 | 17 | $elements = $this->crawler->filterXPath('//a')->reduce( |
|
| 41 | 17 | fn (Crawler $linkElement): bool => |
|
| 42 | 17 | ('' !== $text = $linkElement->text('')) && fnmatch($search, $text, FNM_CASEFOLD) |
|
| 43 | 17 | ); |
|
| 44 | |||
| 45 | 17 | if ($elements->count() > $position) { |
|
| 46 | 17 | return $elements->eq($position)->link(); |
|
| 47 | } |
||
| 48 | |||
| 49 | 1 | throw new RuntimeException(sprintf('Link text "%s" [%d] was not found', $search, $position)); |
|
| 50 | } |
||
| 52 |