Passed
Push — master ( 10c327...3c8cd4 )
by Carlos C
12:47 queued 14s
created

ScrapingReviewerLinkExtractor::fromUrlResponse()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 4
c 1
b 0
f 0
nc 2
nop 1
dl 0
loc 7
ccs 5
cts 5
cp 1
crap 2
rs 10
1
<?php
2
3
declare(strict_types=1);
4
5
namespace PhpCfdi\SatCatalogosPopulate\Origins;
6
7
use RuntimeException;
8
use Symfony\Component\DomCrawler\Crawler;
9
use Symfony\Component\DomCrawler\Link;
10
11
final readonly class ScrapingReviewerLinkExtractor
0 ignored issues
show
Bug introduced by
A parse error occurred: Syntax error, unexpected T_READONLY, expecting T_CLASS on line 11 at column 6
Loading history...
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
    }
51
}
52