Passed
Push — master ( fe617e...bc2c58 )
by Carlos C
14:43 queued 11s
created

ConstantReviewer::accepts()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
c 0
b 0
f 0
nc 1
nop 1
dl 0
loc 3
rs 10
1
<?php
2
3
declare(strict_types=1);
4
5
namespace PhpCfdi\SatCatalogosPopulate\Origins;
6
7
use LogicException;
8
9
class ConstantReviewer implements ReviewerInterface
10
{
11
    /** @var ResourcesGatewayInterface */
12
    private $gateway;
13
14
    public function __construct(ResourcesGatewayInterface $gateway)
15
    {
16
        $this->gateway = $gateway;
17
    }
18
19
    public function accepts(OriginInterface $origin): bool
20
    {
21
        return ($origin instanceof ConstantOrigin);
22
    }
23
24
    public function review(OriginInterface $origin): Review
25
    {
26
        if (! $origin instanceof ConstantOrigin) {
27
            throw new LogicException('This reviewer can only handle ConstantOrigin objects');
28
        }
29
30
        // obtener la información de la url del origen
31
        $response = $this->gateway->headers($origin->url());
32
33
        // si no se pudo obtener el recurso
34
        if (! $response->isSuccess()) {
35
            return new Review($origin, ReviewStatus::notFound());
36
        }
37
38
        // si el recurso no coincide con la última versión
39
        if (! $origin->hasLastVersion() || ! $response->dateMatch($origin->lastVersion())) {
40
            return new Review($origin, ReviewStatus::notUpdated());
41
        }
42
43
        // entonces el recurso coincide
44
        return new Review($origin, ReviewStatus::uptodate());
45
    }
46
}
47