Customer::toArray()   D
last analyzed

Complexity

Conditions 10
Paths 512

Size

Total Lines 33

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 21
CRAP Score 10

Importance

Changes 0
Metric Value
dl 0
loc 33
ccs 21
cts 21
cp 1
rs 4.1777
c 0
b 0
f 0
cc 10
nc 512
nop 0
crap 10

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
namespace Billogram\Model\Customer;
4
5
use Billogram\Model\CreatableFromArray;
6
7
/**
8
 * @author Ibrahim Hizeoui <[email protected]>
9
 */
10
class Customer implements CreatableFromArray
11
{
12
    /**
13
     * @var int
14
     */
15
    private $customerNo;
16
17
    /**
18
     * @var string
19
     */
20
    private $name;
21
22
    /**
23
     * @var string
24
     */
25
    private $notes;
26
27
    /**
28
     * @var string
29
     */
30
    private $orgNo;
31
32
    /**
33
     * @var string
34
     */
35
    private $vatNo;
36
37
    /**
38
     * @var CustomerContact
39
     */
40
    private $contact;
41
42
    /**
43
     * @var CustomerBillingAddress
44
     */
45
    private $address;
46
47
    /**
48
     * @var CustomerDeliveryAddress
49
     */
50
    private $deliveryAddress;
51
52
    /**
53
     * @var \DateTime
54
     */
55
    private $createdAt;
56
57
    /**
58
     * @var \DateTime
59
     */
60
    private $updatedAt;
61
62
    /**
63
     * @var string
64
     */
65
    private $companyType;
66
67
    /**
68
     * @return int
69
     */
70
    public function getCustomerNo()
71
    {
72
        return $this->customerNo;
73
    }
74
75
    /**
76
     * @param int $customerNo
77
     *
78
     * @return Customer
79
     */
80 2
    public function withCustomerNo(int $customerNo)
81
    {
82 2
        $new = clone $this;
83 2
        $new->customerNo = $customerNo;
84
85 2
        return $new;
86
    }
87
88
    /**
89
     * @return string
90
     */
91
    public function getName()
92
    {
93
        return $this->name;
94
    }
95
96
    /**
97
     * @param string $name
98
     *
99
     * @return Customer
100
     */
101 2
    public function withName(string $name)
102
    {
103 2
        $new = clone $this;
104 2
        $new->name = $name;
105
106 2
        return $new;
107
    }
108
109
    /**
110
     * @return string
111
     */
112
    public function getNotes()
113
    {
114
        return $this->notes;
115
    }
116
117
    /**
118
     * @param string $notes
119
     *
120
     * @return Customer
121
     */
122 2
    public function withNotes(string $notes)
123
    {
124 2
        $new = clone $this;
125 2
        $new->notes = $notes;
126
127 2
        return $new;
128
    }
129
130
    /**
131
     * @return string
132
     */
133
    public function getOrgNo()
134
    {
135
        return $this->orgNo;
136
    }
137
138
    /**
139
     * @param string $orgNo
140
     *
141
     * @return Customer
142
     */
143 2
    public function withOrgNo(string $orgNo)
144
    {
145 2
        $new = clone $this;
146 2
        $new->orgNo = $orgNo;
147
148 2
        return $new;
149
    }
150
151
    /**
152
     * @return string
153
     */
154
    public function getVatNo()
155
    {
156
        return $this->vatNo;
157
    }
158
159
    /**
160
     * @param string $vatNo
161
     *
162
     * @return Customer
163
     */
164 2
    public function withVatNo(string $vatNo)
165
    {
166 2
        $new = clone $this;
167 2
        $new->vatNo = $vatNo;
168
169 2
        return $new;
170
    }
171
172
    /**
173
     * @return CustomerContact
174
     */
175
    public function getContact()
176
    {
177
        return $this->contact;
178
    }
179
180
    /**
181
     * @param CustomerContact $contact
182
     *
183
     * @return Customer
184
     */
185 2
    public function withContact(CustomerContact $contact)
186
    {
187 2
        $new = clone $this;
188 2
        $new->contact = $contact;
189
190 2
        return $new;
191
    }
192
193
    /**
194
     * @return CustomerBillingAddress
195
     */
196
    public function getAddress()
197
    {
198
        return $this->address;
199
    }
200
201
    /**
202
     * @param CustomerBillingAddress $address
203
     *
204
     * @return Customer
205
     */
206 2
    public function withAddress(CustomerBillingAddress $address)
207
    {
208 2
        $new = clone $this;
209 2
        $new->address = $address;
210
211 2
        return $new;
212
    }
213
214
    /**
215
     * @return CustomerDeliveryAddress
216
     */
217
    public function getDeliveryAddress()
218
    {
219
        return $this->deliveryAddress;
220
    }
221
222
    /**
223
     * @param CustomerDeliveryAddress $deliveryAddress
224
     *
225
     * @return Customer
226
     */
227 2
    public function withDeliveryAddress(CustomerDeliveryAddress $deliveryAddress)
228
    {
229 2
        $new = clone $this;
230 2
        $new->deliveryAddress = $deliveryAddress;
231
232 2
        return $new;
233
    }
234
235
    /**
236
     * @return string
237
     */
238
    public function getCompanyType()
239
    {
240
        return $this->companyType;
241
    }
242
243
    /**
244
     * @param string $companyType
245
     *
246
     * @return Customer
247
     */
248 2
    public function withCompanyType(string $companyType)
249
    {
250 2
        $new = clone $this;
251 2
        $new->companyType = $companyType;
252
253 2
        return $new;
254
    }
255
256
    /**
257
     * @return \DateTime
258
     */
259
    public function getCreatedAt(): \DateTime
260
    {
261
        return $this->createdAt;
262
    }
263
264
    /**
265
     * @return \DateTime
266
     */
267
    public function getUpdatedAt(): \DateTime
268
    {
269
        return $this->updatedAt;
270
    }
271
272
    /**
273
     * @param \DateTime $updatedAt
274
     */
275
    public function setUpdatedAt(\DateTime $updatedAt)
276
    {
277
        $this->updatedAt = $updatedAt;
278
    }
279
280 8
    public static function createFromArray(array $data): self
281
    {
282 8
        $customer = new self();
283 8
        if (array_key_exists('data', $data)) {
284 4
            $data = $data['data'];
285
        }
286
287 8
        if (isset($data['address'])) {
288 6
            $customer->address = CustomerBillingAddress::createFromArray($data['address']);
289
        }
290 8
        if (isset($data['contact'])) {
291 3
            $customer->contact = CustomerContact::createFromArray($data['contact']);
292
        }
293 8
        if (isset($data['delivery_address'])) {
294 3
            $customer->deliveryAddress = CustomerDeliveryAddress::createFromArray($data['delivery_address']);
295
        }
296
297 8
        $customer->customerNo = $data['customer_no'] ?? null;
298 8
        $customer->name = $data['name'] ?? null;
299 8
        $customer->notes = $data['notes'] ?? null;
300 8
        $customer->orgNo = $data['org_no'] ?? null;
301 8
        $customer->vatNo = $data['vat_no'] ?? null;
302
303 8
        $customer->createdAt = isset($data['created_at']) ? new \DateTime($data['created_at']) : null;
304 8
        $customer->updatedAt = $data['updated_at'] ?? null;
305 8
        $customer->companyType = $data['company_type'] ?? null;
306
307 8
        return $customer;
308
    }
309
310 4
    public function toArray()
311
    {
312 4
        $data = [];
313 4
        if (null !== $this->customerNo) {
314 2
            $data['customer_no'] = $this->customerNo;
315
        }
316 4
        if (null !== $this->name) {
317 2
            $data['name'] = $this->name;
318
        }
319 4
        if (null !== $this->notes) {
320 2
            $data['notes'] = $this->notes;
321
        }
322 4
        if (null !== $this->orgNo) {
323 2
            $data['org_no'] = $this->orgNo;
324
        }
325 4
        if (null !== $this->vatNo) {
326 2
            $data['vat_no'] = $this->vatNo ?? null;
327
        }
328 4
        if (null !== $this->contact) {
329 2
            $data['contact'] = $this->contact->toArray();
330
        }
331 4
        if (null !== $this->address) {
332 2
            $data['address'] = $this->address->toArray();
333
        }
334 4
        if (null !== $this->deliveryAddress) {
335 2
            $data['delivery_address'] = $this->deliveryAddress->toArray();
336
        }
337 4
        if (null !== $this->companyType) {
338 2
            $data['company_type'] = $this->companyType;
339
        }
340
341 4
        return $data;
342
    }
343
}
344