ConstantOrigin::name()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 1
c 0
b 0
f 0
nc 1
nop 0
dl 0
loc 3
ccs 2
cts 2
cp 1
crap 1
rs 10
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
    private string $destinationFilename;
14
15 10
    public function __construct(
16
        private readonly string $name,
17
        private readonly string $url,
18
        private ?DateTimeImmutable $lastVersion = null,
19
        string $destinationFilename = ''
20
    ) {
21 10
        if ('' === $destinationFilename) {
22 9
            $destinationFilename = ltrim(parse_url($url, PHP_URL_PATH) ?: '', '/');
23
        }
24 10
        if ('' !== $destinationFilename) {
25 10
            $destinationFilename = basename($destinationFilename);
26
        }
27 10
        if ('' === $destinationFilename) {
28
            throw new InvalidArgumentException('The is no destination filename and url does not have a valid basename');
29
        }
30 10
        $this->destinationFilename = $destinationFilename;
31
    }
32
33 2
    public function withLastModified(?DateTimeImmutable $lastModified): static
34
    {
35 2
        $clone = clone $this;
36 2
        $clone->lastVersion = $lastModified;
37 2
        return $clone;
38
    }
39
40 3
    public function name(): string
41
    {
42 3
        return $this->name;
43
    }
44
45 7
    public function url(): string
46
    {
47 7
        return $this->url;
48
    }
49
50 5
    public function lastVersion(): DateTimeImmutable
51
    {
52 5
        if (! $this->lastVersion instanceof DateTimeImmutable) {
53
            throw new LogicException('There is no last version in the origin');
54
        }
55 5
        return $this->lastVersion;
56
    }
57
58 4
    public function hasLastVersion(): bool
59
    {
60 4
        return $this->lastVersion instanceof DateTimeImmutable;
61
    }
62
63 5
    public function destinationFilename(): string
64
    {
65 5
        return $this->destinationFilename;
66
    }
67
68 2
    public function downloadUrl(): string
69
    {
70 2
        return $this->url;
71
    }
72
}
73