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

ConstantOrigin   A

Complexity

Total Complexity 13

Size/Duplication

Total Lines 72
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 26
c 0
b 0
f 0
dl 0
loc 72
rs 10
wmc 13

8 Methods

Rating   Name   Duplication   Size   Complexity  
A destinationFilename() 0 3 1
A hasLastVersion() 0 3 1
A __construct() 0 19 5
A withLastModified() 0 5 1
A downloadUrl() 0 3 1
A url() 0 3 1
A name() 0 3 1
A lastVersion() 0 6 2
1
<?php
2
3
declare(strict_types=1);
4
5
namespace PhpCfdi\SatCatalogosPopulate\Origins;
6
7
use DateTimeImmutable;
8
use InvalidArgumentException;
9
use LogicException;
10
11
class ConstantOrigin implements OriginInterface
12
{
13
    /** @var string */
14
    private $name;
15
16
    /** @var string */
17
    private $url;
18
19
    /** @var DateTimeImmutable|null */
20
    private $lastVersion;
21
22
    private $destinationFilename;
23
24
    public function __construct(
25
        string $name,
26
        string $url,
27
        ?DateTimeImmutable $lastVersion = null,
28
        string $destinationFilename = ''
29
    ) {
30
        $this->name = $name;
31
        $this->url = $url;
32
        $this->lastVersion = $lastVersion;
33
        if ('' === $destinationFilename) {
34
            $destinationFilename = basename($destinationFilename);
35
        }
36
        if ('' === $destinationFilename) {
37
            $destinationFilename = ltrim(parse_url($url, PHP_URL_PATH) ?: '', '/');
38
        }
39
        if ('' === $destinationFilename) {
40
            throw new InvalidArgumentException('The is no destination filename and url does not have a valid basename');
41
        }
42
        $this->destinationFilename = $destinationFilename;
43
    }
44
45
    public function withLastModified(?DateTimeImmutable $lastModified): self
46
    {
47
        $clone = clone $this;
48
        $clone->lastVersion = $lastModified;
49
        return $clone;
50
    }
51
52
    public function name(): string
53
    {
54
        return $this->name;
55
    }
56
57
    public function url(): string
58
    {
59
        return $this->url;
60
    }
61
62
    public function lastVersion(): DateTimeImmutable
63
    {
64
        if (! $this->lastVersion instanceof DateTimeImmutable) {
65
            throw new LogicException('There is no last version in the origin');
66
        }
67
        return $this->lastVersion;
68
    }
69
70
    public function hasLastVersion(): bool
71
    {
72
        return $this->lastVersion instanceof DateTimeImmutable;
73
    }
74
75
    public function destinationFilename(): string
76
    {
77
        return $this->destinationFilename;
78
    }
79
80
    public function downloadUrl(): string
81
    {
82
        return $this->url;
83
    }
84
}
85