1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace PhpCfdi\SatCatalogosPopulate\Origins; |
6
|
|
|
|
7
|
|
|
use DateTimeImmutable; |
8
|
|
|
use Psr\Http\Message\ResponseInterface; |
9
|
|
|
use Stringable; |
10
|
|
|
|
11
|
|
|
class UrlResponse |
12
|
|
|
{ |
13
|
|
|
private readonly DateTimeImmutable $lastModified; |
14
|
|
|
|
15
|
25 |
|
public function __construct( |
16
|
|
|
private readonly string $url, |
17
|
|
|
private readonly int $httpStatus, |
18
|
|
|
DateTimeImmutable $lastModified = null, |
19
|
|
|
private readonly Stringable|string $body = '' |
20
|
|
|
) { |
21
|
25 |
|
$this->lastModified = ($lastModified) ?: new DateTimeImmutable(); |
|
|
|
|
22
|
|
|
} |
23
|
|
|
|
24
|
|
|
public static function createFromResponse(ResponseInterface $response, string $url): self |
25
|
|
|
{ |
26
|
|
|
$lastModified = null; |
27
|
|
|
if ($response->hasHeader('Last-Modified')) { |
28
|
|
|
/** @noinspection PhpUnhandledExceptionInspection */ |
29
|
|
|
$lastModified = new DateTimeImmutable($response->getHeaderLine('Last-Modified')); |
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
return new self($url, $response->getStatusCode(), $lastModified, $response->getBody()); |
33
|
|
|
} |
34
|
|
|
|
35
|
23 |
|
public function url(): string |
36
|
|
|
{ |
37
|
23 |
|
return $this->url; |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
public function httpStatus(): int |
41
|
|
|
{ |
42
|
|
|
return $this->httpStatus; |
43
|
|
|
} |
44
|
|
|
|
45
|
2 |
|
public function lastModified(): DateTimeImmutable |
46
|
|
|
{ |
47
|
2 |
|
return $this->lastModified; |
48
|
|
|
} |
49
|
|
|
|
50
|
8 |
|
public function isSuccess(): bool |
51
|
|
|
{ |
52
|
8 |
|
return 200 === $this->httpStatus; |
53
|
|
|
} |
54
|
|
|
|
55
|
4 |
|
public function dateMatch(DateTimeImmutable $date): bool |
56
|
|
|
{ |
57
|
4 |
|
return ($this->lastModified->format('U') === $date->format('U')); |
58
|
|
|
} |
59
|
|
|
|
60
|
18 |
|
public function body(): string |
61
|
|
|
{ |
62
|
18 |
|
return (string) $this->body; |
63
|
|
|
} |
64
|
|
|
} |
65
|
|
|
|