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

ConstantOrigin::__construct()   A

Complexity

Conditions 5
Paths 8

Size

Total Lines 19
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 5
eloc 10
c 0
b 0
f 0
nc 8
nop 4
dl 0
loc 19
rs 9.6111
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