Passed
Push — master ( 5c7966...d5d6cf )
by Rogier
01:30
created

OrderData::fromResponse()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 19
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

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