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

OrderData::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 12
Code Lines 0

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 0
c 1
b 0
f 0
dl 0
loc 12
rs 10
cc 1
nc 1
nop 10

How to fix   Many Parameters   

Many Parameters

Methods with many parameters are not only hard to understand, but their parameters also often become inconsistent when you need more, or different data.

There are several approaches to avoid long parameter lists:

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