DomainRegistrationRequest::jsonSerialize()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 23
Code Lines 19

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 21
CRAP Score 1

Importance

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