ScrapingReviewer::review()   B
last analyzed

Complexity

Conditions 7
Paths 8

Size

Total Lines 28
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 13
CRAP Score 7

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 7
eloc 13
c 1
b 0
f 0
nc 8
nop 1
dl 0
loc 28
ccs 13
cts 13
cp 1
crap 7
rs 8.8333
1
<?php
2
3
declare(strict_types=1);
4
5
namespace PhpCfdi\SatCatalogosPopulate\Origins;
6
7
use LogicException;
8
use RuntimeException;
9
10
class ScrapingReviewer implements ReviewerInterface
11
{
12 22
    public function __construct(private readonly ResourcesGatewayInterface $gateway)
13
    {
14 22
    }
15
16
    public function gateway(): ResourcesGatewayInterface
17
    {
18
        return $this->gateway;
19
    }
20
21 3
    public function accepts(OriginInterface $origin): bool
22
    {
23 3
        return ($origin instanceof ScrapingOrigin);
24
    }
25
26 6
    public function review(OriginInterface $origin): Review
27
    {
28 6
        if (! $origin instanceof ScrapingOrigin) {
29 1
            throw new LogicException('This reviewer can only handle ScrapingOrigin objects');
30
        }
31
32 5
        if (! $origin->hasDownloadUrl()) {
33
            try {
34 5
                $origin = $this->resolveOrigin($origin);
35 1
            } catch (RuntimeException) {
36 1
                return new Review($origin, ReviewStatus::notFound());
37
            }
38
        }
39
40 4
        $response = $this->gateway->headers($origin->downloadUrl());
41
42
        // si no se pudo obtener el recurso
43 4
        if (! $response->isSuccess()) {
44 1
            return new Review($origin, ReviewStatus::notFound());
45
        }
46
47
        // si el recurso no coincide con la última versión
48 3
        if (! $origin->hasLastVersion() || ! $response->dateMatch($origin->lastVersion())) {
49 2
            return new Review($origin, ReviewStatus::notUpdated());
50
        }
51
52
        // entonces el recurso coincide
53 1
        return new Review($origin, ReviewStatus::uptodate());
54
    }
55
56 5
    public function resolveOrigin(ScrapingOrigin $origin): ScrapingOrigin
57
    {
58 5
        $baseResource = $this->gateway->get($origin->url(), '');
59 5
        $downloadUrl = $this->resolveHtmlToLink($baseResource, $origin->linkText(), $origin->linkPosition());
60 4
        return $origin->withDownloadUrl($downloadUrl);
61
    }
62
63 18
    public function resolveHtmlToLink(UrlResponse $response, string $linkText, int $position = 0): string
64
    {
65 18
        return ScrapingReviewerLinkExtractor::fromUrlResponse($response)->search($linkText, $position);
0 ignored issues
show
Bug introduced by
The type PhpCfdi\SatCatalogosPopu...ngReviewerLinkExtractor was not found. Maybe you did not declare it correctly or list all dependencies?

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:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
66
    }
67
}
68