Passed
Push — master ( 199cf7...129662 )
by Rogier
12:20
created

OrderData::isInvalid()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 1
dl 0
loc 3
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
1
<?php
2
3
namespace Rogierw\RwAcme\DTO;
4
5
use Rogierw\RwAcme\Http\Response;
6
use Rogierw\RwAcme\Support\Arr;
7
use Rogierw\RwAcme\Support\Url;
8
use Spatie\LaravelData\Data;
9
10
class OrderData extends Data
11
{
12
    public function __construct(
13
        public string $id,
14
        public string $url,
15
        public string $status,
16
        public string $expires,
17
        public array $identifiers,
18
        public array $domainValidationUrls,
19
        public string $finalizeUrl,
20
        public string $accountUrl,
21
        public string|null $certificateUrl,
22
        public bool $finalized = false,
23
    ) {}
24
25
    public static function fromResponse(Response $response, string $accountUrl = ''): OrderData
26
    {
27
        $url = Arr::get($response->getRawHeaders(), 'Location');
28
29
        if (empty($url)) {
30
            $url = Arr::get($response->getHeaders(), 'url');
31
        }
32
33
        $url = trim(rtrim($url, '?'));
34
35
        return new self(
36
            id: Url::extractId($url),
37
            url: $url,
38
            status: $response->getBody()['status'],
39
            expires: $response->getBody()['expires'],
40
            identifiers: $response->getBody()['identifiers'],
41
            domainValidationUrls: $response->getBody()['authorizations'],
42
            finalizeUrl: $response->getBody()['finalize'],
43
            accountUrl: $accountUrl,
44
            certificateUrl: Arr::get($response->getBody(), 'certificate'),
45
        );
46
    }
47
48
    public function setCertificateUrl(string $url): void
49
    {
50
        $this->certificateUrl = $url;
51
        $this->finalized = true;
52
    }
53
54
    public function isPending(): bool
55
    {
56
        return $this->status === 'pending';
57
    }
58
59
    public function isReady(): bool
60
    {
61
        return $this->status === 'ready';
62
    }
63
64
    public function isValid(): bool
65
    {
66
        return $this->status === 'valid';
67
    }
68
69
    public function isInvalid(): bool
70
    {
71
        return $this->status === 'invalid';
72
    }
73
74
    public function isFinalized(): bool
75
    {
76
        return ($this->finalized || $this->isValid());
77
    }
78
79
    public function isNotFinalized(): bool
80
    {
81
        return !$this->isFinalized();
82
    }
83
}
84