Test Failed
Branch master (62528d)
by Zangra
14:14 queued 12:04
created

Customer::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 24
Code Lines 0

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 0
c 0
b 0
f 0
dl 0
loc 24
rs 10
cc 1
nc 1
nop 22

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
declare(strict_types=1);
3
4
namespace Bpost\BpostApiClient\Bpack247;
5
6
use Bpost\BpostApiClient\Exception\BpostLogicException\BpostInvalidValueException;
7
use Bpost\BpostApiClient\Exception\XmlException\BpostXmlNoUserIdFoundException;
8
use DateTime;
9
use DOMDocument;
10
use DOMElement;
11
use SimpleXMLElement;
12
13
/**
14
 * bPost Customer class
15
 *
16
 * @author Tijs Verkoyen <[email protected]>
17
 */
18
class Customer
19
{
20
    public const CUSTOMER_PREFERRED_LANGUAGE_NL = 'nl-BE';
21
    public const CUSTOMER_PREFERRED_LANGUAGE_FR = 'fr-BE';
22
    public const CUSTOMER_PREFERRED_LANGUAGE_EN = 'en-US';
23
24
    public const CUSTOMER_TITLE_MR = 'Mr.';
25
    public const CUSTOMER_TITLE_MS = 'Ms.';
26
27
    public function __construct(
28
        private ?bool $activated = null,
29
        private ?string $userID = null,
30
        private ?string $firstName = null,
31
        private ?string $lastName = null,
32
        private ?string $companyName = null,
33
        private ?string $street = null,
34
        private ?string $number = null,
35
        private ?string $email = null,
36
        private string $mobilePrefix = '0032',
37
        private ?string $mobileNumber = null,
38
        private ?string $postalCode = null,
39
        private array $packStations = [],
40
        private ?string $town = null,
41
        private ?string $preferredLanguage = null,
42
        private ?string $title = null,
43
        private ?bool $isComfortZoneUser = null,
44
        private ?DateTime $dateOfBirth = null,
45
        private ?string $deliveryCode = null,
46
        private ?bool $optIn = null,
47
        private ?bool $receivePromotions = null,
48
        private ?bool $useInformationForThirdParty = null,
49
        private ?string $userName = null,
50
    ) {}
51
52
53
    public function setActivated(?bool $activated): void
54
    {
55
        $this->activated = $activated;
56
    }
57
58
    public function getActivated(): ?bool
59
    {
60
        return $this->activated;
61
    }
62
63
    public function setCompanyName(?string $companyName): void
64
    {
65
        $this->companyName = $companyName;
66
    }
67
68
    public function getCompanyName(): ?string
69
    {
70
        return $this->companyName;
71
    }
72
73
    public function setDateOfBirth(?DateTime $dateOfBirth): void
74
    {
75
        $this->dateOfBirth = $dateOfBirth;
76
    }
77
78
    public function getDateOfBirth(): ?DateTime
79
    {
80
        return $this->dateOfBirth;
81
    }
82
83
    public function setDeliveryCode(?string $deliveryCode): void
84
    {
85
        $this->deliveryCode = $deliveryCode;
86
    }
87
88
    public function getDeliveryCode(): ?string
89
    {
90
        return $this->deliveryCode;
91
    }
92
93
    public function setEmail(?string $email): void
94
    {
95
        $this->email = $email;
96
    }
97
98
    public function getEmail(): ?string
99
    {
100
        return $this->email;
101
    }
102
103
    public function setFirstName(?string $firstName): void
104
    {
105
        $this->firstName = $firstName;
106
    }
107
108
    public function getFirstName(): ?string
109
    {
110
        return $this->firstName;
111
    }
112
113
    public function setIsComfortZoneUser(?bool $isComfortZoneUser): void
114
    {
115
        $this->isComfortZoneUser = $isComfortZoneUser;
116
    }
117
118
    public function getIsComfortZoneUser(): ?bool
119
    {
120
        return $this->isComfortZoneUser;
121
    }
122
123
    public function setLastName(?string $lastName): void
124
    {
125
        $this->lastName = $lastName;
126
    }
127
128
    public function getLastName(): ?string
129
    {
130
        return $this->lastName;
131
    }
132
133
    public function setMobileNumber(?string $mobileNumber): void
134
    {
135
        $this->mobileNumber = $mobileNumber;
136
    }
137
138
    public function getMobileNumber(): ?string
139
    {
140
        return $this->mobileNumber;
141
    }
142
143
    public function setMobilePrefix(string $mobilePrefix): void
144
    {
145
        $this->mobilePrefix = $mobilePrefix;
146
    }
147
148
    public function getMobilePrefix(): string
149
    {
150
        return $this->mobilePrefix;
151
    }
152
153
    public function setNumber(?string $number): void
154
    {
155
        $this->number = $number;
156
    }
157
158
    public function getNumber(): ?string
159
    {
160
        return $this->number;
161
    }
162
163
    public function setOptIn(?bool $optIn): void
164
    {
165
        $this->optIn = $optIn;
166
    }
167
168
    public function getOptIn(): ?bool
169
    {
170
        return $this->optIn;
171
    }
172
173
    public function addPackStation(CustomerPackStation $packStation): void
174
    {
175
        $this->packStations[] = $packStation;
176
    }
177
178
    public function setPackStations(array $packStations): void
179
    {
180
        $this->packStations = $packStations;
181
    }
182
183
    public function getPackStations(): array
184
    {
185
        return $this->packStations;
186
    }
187
188
    public function setPostalCode(?string $postalCode): void
189
    {
190
        $this->postalCode = $postalCode;
191
    }
192
193
    public function getPostalCode(): ?string
194
    {
195
        return $this->postalCode;
196
    }
197
198
    /**
199
     * @throws BpostInvalidValueException
200
     */
201
    public function setPreferredLanguage(?string $preferredLanguage): void
202
    {
203
        if ($preferredLanguage === null) {
204
            $this->preferredLanguage = null;
205
            return;
206
        }
207
        if (!in_array($preferredLanguage, self::getPossiblePreferredLanguageValues(), true)) {
208
            throw new BpostInvalidValueException('preferred language', $preferredLanguage, self::getPossiblePreferredLanguageValues());
209
        }
210
        $this->preferredLanguage = $preferredLanguage;
211
    }
212
213
    public function getPreferredLanguage(): ?string
214
    {
215
        return $this->preferredLanguage;
216
    }
217
218
    public static function getPossiblePreferredLanguageValues(): array
219
    {
220
        return [
221
            self::CUSTOMER_PREFERRED_LANGUAGE_NL,
222
            self::CUSTOMER_PREFERRED_LANGUAGE_FR,
223
            self::CUSTOMER_PREFERRED_LANGUAGE_EN,
224
        ];
225
    }
226
227
    public function setReceivePromotions(?bool $receivePromotions): void
228
    {
229
        $this->receivePromotions = $receivePromotions;
230
    }
231
232
    public function getReceivePromotions(): ?bool
233
    {
234
        return $this->receivePromotions;
235
    }
236
237
    public function setStreet(?string $street): void
238
    {
239
        $this->street = $street;
240
    }
241
242
    public function getStreet(): ?string
243
    {
244
        return $this->street;
245
    }
246
247
    /**
248
     * @throws BpostInvalidValueException
249
     */
250
    public function setTitle(?string $title): void
251
    {
252
        if ($title === null) {
253
            $this->title = null;
254
            return;
255
        }
256
        if (!in_array($title, self::getPossibleTitleValues(), true)) {
257
            throw new BpostInvalidValueException('title', $title, self::getPossibleTitleValues());
258
        }
259
        $this->title = $title;
260
    }
261
262
    public function getTitle(): ?string
263
    {
264
        return $this->title;
265
    }
266
267
    public static function getPossibleTitleValues(): array
268
    {
269
        return [
270
            self::CUSTOMER_TITLE_MR,
271
            self::CUSTOMER_TITLE_MS,
272
        ];
273
    }
274
275
    public function setTown(?string $town): void
276
    {
277
        $this->town = $town;
278
    }
279
280
    public function getTown(): ?string
281
    {
282
        return $this->town;
283
    }
284
285
    public function setUseInformationForThirdParty(?bool $useInformationForThirdParty): void
286
    {
287
        $this->useInformationForThirdParty = $useInformationForThirdParty;
288
    }
289
290
    public function getUseInformationForThirdParty(): ?bool
291
    {
292
        return $this->useInformationForThirdParty;
293
    }
294
295
    public function setUserID(?string $userID): void
296
    {
297
        $this->userID = $userID;
298
    }
299
300
    public function getUserID(): ?string
301
    {
302
        return $this->userID;
303
    }
304
305
    public function setUserName(?string $userName): void
306
    {
307
        $this->userName = $userName;
308
    }
309
310
    public function getUserName(): ?string
311
    {
312
        return $this->userName;
313
    }
314
315
    public function toXML(DOMDocument $document): DOMElement
316
    {
317
        $customer = $document->createElement(
318
            'Customer'
319
        );
320
        $customer->setAttribute(
321
            'xmlns',
322
            'http://schema.post.be/ServiceController/customer'
323
        );
324
        $customer->setAttribute(
325
            'xmlns:xsi',
326
            'http://www.w3.org/2001/XMLSchema-instance'
327
        );
328
        $customer->setAttribute(
329
            'xsi:schemaLocation',
330
            'http://schema.post.be/ServiceController/customer'
331
        );
332
333
        $document->appendChild($customer);
334
335
        $this->namingToXML($document, $customer);
336
        $this->addressToXML($document, $customer);
337
        $this->contactToXML($document, $customer);
338
        $this->postalCodeToXML($document, $customer);
339
        $this->preferredLanguageToXML($document, $customer);
340
        $this->titleToXML($document, $customer);
341
342
        return $customer;
343
    }
344
345
    /**
346
     * @throws \DateMalformedStringException
347
     * @throws BpostXmlNoUserIdFoundException
348
     * @throws BpostInvalidValueException
349
     */
350
    public static function createFromXML(SimpleXMLElement $xml): Customer
351
    {
352
        // @todo work with classmaps ...
353
        if (!isset($xml->UserID)) {
354
            throw new BpostXmlNoUserIdFoundException();
355
        }
356
357
        $customer = new Customer();
358
359
        if (isset($xml->UserID) && $xml->UserID != '') {
360
            $customer->setUserID((string) $xml->UserID);
361
        }
362
        if (isset($xml->FirstName) && $xml->FirstName != '') {
363
            $customer->setFirstName((string) $xml->FirstName);
364
        }
365
        if (isset($xml->LastName) && $xml->LastName != '') {
366
            $customer->setLastName((string) $xml->LastName);
367
        }
368
        if (isset($xml->Street) && $xml->Street != '') {
369
            $customer->setStreet((string) $xml->Street);
370
        }
371
        if (isset($xml->Number) && $xml->Number != '') {
372
            $customer->setNumber((string) $xml->Number);
373
        }
374
        if (isset($xml->CompanyName) && $xml->CompanyName != '') {
375
            $customer->setCompanyName((string) $xml->CompanyName);
376
        }
377
        if (isset($xml->DateOfBirth) && $xml->DateOfBirth != '') {
378
            $dateTime = new DateTime((string) $xml->DateOfBirth);
379
            $customer->setDateOfBirth($dateTime);
380
        }
381
        if (isset($xml->DeliveryCode) && $xml->DeliveryCode != '') {
382
            $customer->setDeliveryCode(
383
                (string) $xml->DeliveryCode
384
            );
385
        }
386
        if (isset($xml->Email) && $xml->Email != '') {
387
            $customer->setEmail((string) $xml->Email);
388
        }
389
        if (isset($xml->MobilePrefix) && $xml->MobilePrefix != '') {
390
            $customer->setMobilePrefix(
391
                trim((string) $xml->MobilePrefix)
392
            );
393
        }
394
        if (isset($xml->MobileNumber) && $xml->MobileNumber != '') {
395
            $customer->setMobileNumber(
396
                (string) $xml->MobileNumber
397
            );
398
        }
399
        if (isset($xml->Postalcode) && $xml->Postalcode != '') {
400
            $customer->setPostalCode(
401
                (string) $xml->Postalcode
402
            );
403
        }
404
        if (isset($xml->PreferredLanguage) && $xml->PreferredLanguage != '') {
405
            $customer->setPreferredLanguage(
406
                (string) $xml->PreferredLanguage
407
            );
408
        }
409
        if (isset($xml->ReceivePromotions) && $xml->ReceivePromotions != '') {
410
            $receivePromotions = in_array((string)$xml->ReceivePromotions, ['true','1'], true);
411
            $customer->setReceivePromotions($receivePromotions);
412
        }
413
        if (isset($xml->actived) && $xml->actived != '') {
414
            $activated = in_array((string)$xml->actived, ['true','1'], true);
415
            $customer->setActivated($activated);
416
        }
417
        if (isset($xml->Title) && $xml->Title != '') {
418
            $title = (string) $xml->Title;
419
            $title = ucfirst(strtolower($title));
420
            if (!str_ends_with($title, '.')) {
421
                $title .= '.';
422
            }
423
424
            $customer->setTitle($title);
425
        }
426
        if (isset($xml->Town) && $xml->Town != '') {
427
            $customer->setTown((string) $xml->Town);
428
        }
429
430
        if (isset($xml->PackStations->CustomerPackStation)) {
431
            foreach ($xml->PackStations->CustomerPackStation as $packStation) {
432
                $customer->addPackStation(CustomerPackStation::createFromXML($packStation));
433
            }
434
        }
435
436
        return $customer;
437
    }
438
439
    private function namingToXML(DOMDocument $document, DOMElement $customer): void
440
    {
441
        if ($this->getFirstName() !== null) {
442
            $customer->appendChild(
443
                $document->createElement(
444
                    'FirstName',
445
                    $this->getFirstName()
446
                )
447
            );
448
        }
449
        if ($this->getLastName() !== null) {
450
            $customer->appendChild(
451
                $document->createElement(
452
                    'LastName',
453
                    $this->getLastName()
454
                )
455
            );
456
        }
457
    }
458
459
    private function contactToXML(DOMDocument $document, DOMElement $customer): void
460
    {
461
        if ($this->getEmail() !== null) {
462
            $customer->appendChild(
463
                $document->createElement(
464
                    'Email',
465
                    $this->getEmail()
466
                )
467
            );
468
        }
469
        if ($this->getMobilePrefix() !== null) {
0 ignored issues
show
introduced by
The condition $this->getMobilePrefix() !== null is always true.
Loading history...
470
            $customer->appendChild(
471
                $document->createElement(
472
                    'MobilePrefix',
473
                    $this->getMobilePrefix()
474
                )
475
            );
476
        }
477
        if ($this->getMobileNumber() !== null) {
478
            $customer->appendChild(
479
                $document->createElement(
480
                    'MobileNumber',
481
                    $this->getMobileNumber()
482
                )
483
            );
484
        }
485
    }
486
487
    private function addressToXML(DOMDocument $document, DOMElement $customer): void
488
    {
489
        if ($this->getStreet() !== null) {
490
            $customer->appendChild(
491
                $document->createElement(
492
                    'Street',
493
                    $this->getStreet()
494
                )
495
            );
496
        }
497
        if ($this->getNumber() !== null) {
498
            $customer->appendChild(
499
                $document->createElement(
500
                    'Number',
501
                    $this->getNumber()
502
                )
503
            );
504
        }
505
    }
506
507
    private function preferredLanguageToXML(DOMDocument $document, DOMElement $customer): void
508
    {
509
        if ($this->getPreferredLanguage() !== null) {
510
            $customer->appendChild(
511
                $document->createElement(
512
                    'PreferredLanguage',
513
                    $this->getPreferredLanguage()
514
                )
515
            );
516
        }
517
    }
518
519
    private function titleToXML(DOMDocument $document, DOMElement $customer): void
520
    {
521
        if ($this->getTitle() !== null) {
522
            $customer->appendChild(
523
                $document->createElement(
524
                    'Title',
525
                    $this->getTitle()
526
                )
527
            );
528
        }
529
    }
530
531
    private function postalCodeToXML(DOMDocument $document, DOMElement $customer): void
532
    {
533
        if ($this->getPostalCode() !== null) {
534
            $customer->appendChild(
535
                $document->createElement(
536
                    'PostalCode',
537
                    $this->getPostalCode()
538
                )
539
            );
540
        }
541
    }
542
}
543