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

ConstantReviewer   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 36
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 12
c 0
b 0
f 0
dl 0
loc 36
rs 10
wmc 7

3 Methods

Rating   Name   Duplication   Size   Complexity  
A accepts() 0 3 1
A review() 0 21 5
A __construct() 0 3 1
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