OrderData   A
last analyzed

Complexity

Total Complexity 11

Size/Duplication

Total Lines 73
Duplicated Lines 0 %

Importance

Changes 2
Bugs 1 Features 0
Metric Value
wmc 11
eloc 23
c 2
b 1
f 0
dl 0
loc 73
rs 10

9 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 12 1
A isInvalid() 0 3 1
A fromResponse() 0 20 2
A isNotFinalized() 0 3 1
A isPending() 0 3 1
A isFinalized() 0 3 2
A isValid() 0 3 1
A setCertificateUrl() 0 4 1
A isReady() 0 3 1
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 $certificateUrl,
22
        public bool $finalized = false,
23
    ) {
24
    }
25
26
    public static function fromResponse(Response $response, string $accountUrl = ''): OrderData
27
    {
28
        $url = $response->getHeader('location');
29
30
        if (empty($url)) {
31
            $url = $response->getRequestedUrl();
32
        }
33
34
        $url = trim(rtrim($url, '?'));
35
36
        return new self(
37
            id: Url::extractId($url),
38
            url: $url,
39
            status: $response->getBody()['status'],
40
            expires: $response->getBody()['expires'],
41
            identifiers: $response->getBody()['identifiers'],
42
            domainValidationUrls: $response->getBody()['authorizations'],
43
            finalizeUrl: $response->getBody()['finalize'],
44
            accountUrl: $accountUrl,
45
            certificateUrl: Arr::get($response->getBody(), 'certificate'),
46
        );
47
    }
48
49
    public function setCertificateUrl(string $url): void
50
    {
51
        $this->certificateUrl = $url;
52
        $this->finalized = true;
53
    }
54
55
    public function isPending(): bool
56
    {
57
        return $this->status === 'pending';
58
    }
59
60
    public function isReady(): bool
61
    {
62
        return $this->status === 'ready';
63
    }
64
65
    public function isValid(): bool
66
    {
67
        return $this->status === 'valid';
68
    }
69
70
    public function isInvalid(): bool
71
    {
72
        return $this->status === 'invalid';
73
    }
74
75
    public function isFinalized(): bool
76
    {
77
        return ($this->finalized || $this->isValid());
78
    }
79
80
    public function isNotFinalized(): bool
81
    {
82
        return !$this->isFinalized();
83
    }
84
}
85