OrderData::__construct()   A
last analyzed

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 $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