UrlResponse::url()   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 1
Bugs 0 Features 0
Metric Value
eloc 1
c 1
b 0
f 0
dl 0
loc 3
ccs 2
cts 2
cp 1
rs 10
cc 1
nc 1
nop 0
crap 1
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();
0 ignored issues
show
Bug introduced by
The property lastModified is declared read-only in PhpCfdi\SatCatalogosPopulate\Origins\UrlResponse.
Loading history...
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