|
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
|
|
|
|