Test Failed
Pull Request — master (#10)
by
unknown
14:44
created

DomainRegistrationRequest::create()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 35
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 1
eloc 17
c 2
b 0
f 0
nc 1
nop 16
dl 0
loc 35
ccs 0
cts 18
cp 0
crap 2
rs 9.7

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 Level23\Dynadot\Dto;
4
5
use InvalidArgumentException;
6
use JsonSerializable;
7
8
class DomainRegistrationRequest implements JsonSerializable
9
{
10
    private int $duration;
11
    private string $authCode;
12
    private int $customerId;
13
    /** @var array<string> */
14
    private array $nameserverList;
15
    private string $privacy;
16
17
    public ?int $registrant_contact_id;
18
    public ?int $admin_contact_id;
19
    public ?int $tech_contact_id;
20
    public ?int $billing_contact_id;
21
22
    public ?Contact $registrant_contact;
23
    public ?Contact $admin_contact;
24
    public ?Contact $tech_contact;
25
    public ?Contact $billing_contact;
26
27
    public string $currency;
28
    public bool   $register_premium;
29
    public ?string $coupon_code;
30
31
    /**
32
     * @param array<string> $nameserverList
33
     */
34
    private function __construct(
35
        int $duration,
36
        string $authCode,
37
        int $customerId,
38
        ?int $registrantContactId,
39
        ?int $adminContactId,
40
        ?int $techContactId,
41
        ?int $billingContactId,
42
        ?Contact $registrant,
43
        ?Contact $admin,
44
        ?Contact $tech,
45
        ?Contact $billing,
46
        array $nameserverList,
47
        string $privacy,
48
        string $currency,
49
        bool $registerPremium,
50
        ?string $couponCode
51
    ) {
52
        if ($duration < 1 || $duration > 10) {
53
            throw new InvalidArgumentException('Duration must be between 1 and 10 years.');
54
        }
55
56
        $this->duration = $duration;
57
        $this->authCode = $authCode;
58
        $this->customerId = $customerId;
59
        $this->registrant_contact_id = $registrantContactId;
60
        $this->admin_contact_id = $adminContactId;
61
        $this->tech_contact_id = $techContactId;
62
        $this->billing_contact_id = $billingContactId;
63
        $this->registrant_contact = $registrant;
64
        $this->admin_contact = $admin;
65
        $this->tech_contact = $tech;
66
        $this->billing_contact = $billing;
67
        $this->nameserverList = $nameserverList;
68
        $this->privacy = $privacy;
69
        $this->currency = $currency;
70
        $this->register_premium = $registerPremium;
71
        $this->coupon_code = $couponCode;
72
    }
73
74
    /**
75
     * @param array<string> $nameserverList
76
     */
77
    public static function create(
78
        int $duration,
79
        string $authCode,
80
        int $customerId,
81
        array $nameserverList,
82
        string $privacy = 'off',
83
        string $currency = 'USD',
84
        bool $registerPremium = false,
85
        ?string $couponCode = null,
86
        ?int $registrantContactId = null,
87
        ?int $adminContactId = null,
88
        ?int $techContactId = null,
89
        ?int $billingContactId = null,
90
        ?Contact $registrant = null,
91
        ?Contact $admin = null,
92
        ?Contact $tech = null,
93
        ?Contact $billing = null
94
    ): self {
95
        return new self(
96
            $duration,
97
            $authCode,
98
            $customerId,
99
            $registrantContactId,
100
            $adminContactId,
101
            $techContactId,
102
            $billingContactId,
103
            $registrant,
104
            $admin,
105
            $tech,
106
            $billing,
107
            $nameserverList,
108
            $privacy,
109
            $currency,
110
            $registerPremium,
111
            $couponCode
112
        );
113
    }
114
115
    /**
116
     * @return array<string, mixed>
117
     */
118
    public function jsonSerialize(): array
119
    {
120
        $domain = [
121
            'duration'                => $this->duration,
122
            'auth_code'               => $this->authCode,
123
            'registrant_contact_id'   => $this->registrant_contact_id,
124
            'admin_contact_id'        => $this->admin_contact_id,
125
            'tech_contact_id'         => $this->tech_contact_id,
126
            'billing_contact_id'      => $this->billing_contact_id,
127
            'registrant_contact'      => $this->registrant_contact?->jsonSerialize(),
128
            'admin_contact'           => $this->admin_contact?->jsonSerialize(),
129
            'tech_contact'            => $this->tech_contact?->jsonSerialize(),
130
            'billing_contact'         => $this->billing_contact?->jsonSerialize(),
131
            'customer_id'             => $this->customerId,
132
            'name_server_list'        => $this->nameserverList,
133
            'privacy'                 => $this->privacy,
134
        ];
135
136
        return [
137
            'domain'           => array_filter($domain, fn($value) => $value !== null),
138
            'currency'         => $this->currency,
139
            'register_premium' => $this->register_premium,
140
            'coupon_code'      => $this->coupon_code,
141
        ];
142
    }
143
}
144