Passed
Pull Request — master (#10)
by
unknown
02:50
created

DomainRegistrationRequest::__construct()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 38
Code Lines 18

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 18
CRAP Score 3.0013

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 3
eloc 18
c 2
b 0
f 0
nc 2
nop 16
dl 0
loc 38
ccs 18
cts 19
cp 0.9474
crap 3.0013
rs 9.6666

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 13
    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 13
        if ($duration < 1 || $duration > 10) {
53
            throw new InvalidArgumentException('Duration must be between 1 and 10 years.');
54
        }
55
56 13
        $this->duration              = $duration;
57 13
        $this->authCode              = $authCode;
58 13
        $this->customerId            = $customerId;
59 13
        $this->registrant_contact_id = $registrantContactId;
60 13
        $this->admin_contact_id      = $adminContactId;
61 13
        $this->tech_contact_id       = $techContactId;
62 13
        $this->billing_contact_id    = $billingContactId;
63 13
        $this->registrant_contact    = $registrant;
64 13
        $this->admin_contact         = $admin;
65 13
        $this->tech_contact          = $tech;
66 13
        $this->billing_contact       = $billing;
67 13
        $this->nameserverList        = $nameserverList;
68 13
        $this->privacy               = $privacy;
69 13
        $this->currency              = $currency;
70 13
        $this->register_premium      = $registerPremium;
71 13
        $this->coupon_code           = $couponCode;
72
    }
73
74
    /**
75
     * @param array<string> $nameserverList
76
     */
77 13
    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 13
        return new self(
96 13
            $duration,
97 13
            $authCode,
98 13
            $customerId,
99 13
            $registrantContactId,
100 13
            $adminContactId,
101 13
            $techContactId,
102 13
            $billingContactId,
103 13
            $registrant,
104 13
            $admin,
105 13
            $tech,
106 13
            $billing,
107 13
            $nameserverList,
108 13
            $privacy,
109 13
            $currency,
110 13
            $registerPremium,
111 13
            $couponCode
112 13
        );
113
    }
114
115
    /**
116
     * @return array<string, mixed>
117
     */
118 13
    public function jsonSerialize(): array
119
    {
120 13
        $domain = [
121 13
            'duration'              => $this->duration,
122 13
            'auth_code'             => $this->authCode,
123 13
            'registrant_contact_id' => $this->registrant_contact_id,
124 13
            'admin_contact_id'      => $this->admin_contact_id,
125 13
            'tech_contact_id'       => $this->tech_contact_id,
126 13
            'billing_contact_id'    => $this->billing_contact_id,
127 13
            'registrant_contact'    => $this->registrant_contact?->jsonSerialize(),
128 13
            'admin_contact'         => $this->admin_contact?->jsonSerialize(),
129 13
            'tech_contact'          => $this->tech_contact?->jsonSerialize(),
130 13
            'billing_contact'       => $this->billing_contact?->jsonSerialize(),
131 13
            'customer_id'           => $this->customerId,
132 13
            'name_server_list'      => $this->nameserverList,
133 13
            'privacy'               => $this->privacy,
134 13
        ];
135
136 13
        return [
137 13
            'domain'           => array_filter($domain, fn ($value) => $value !== null),
138 13
            'currency'         => $this->currency,
139 13
            'register_premium' => $this->register_premium,
140 13
            'coupon_code'      => $this->coupon_code,
141 13
        ];
142
    }
143
}
144