Customer   F
last analyzed

Complexity

Total Complexity 105

Size/Duplication

Total Lines 775
Duplicated Lines 0 %

Importance

Changes 2
Bugs 1 Features 0
Metric Value
wmc 105
eloc 209
dl 0
loc 775
rs 2
c 2
b 1
f 0

55 Methods

Rating   Name   Duplication   Size   Complexity  
A getReceivePromotions() 0 3 1
A setMobilePrefix() 0 3 1
A setReceivePromotions() 0 3 1
A setCompanyName() 0 3 1
A getStreet() 0 3 1
A getEmail() 0 3 1
A getFirstName() 0 3 1
A getNumber() 0 3 1
A setUserName() 0 3 1
A getPreferredLanguage() 0 3 1
A getUseInformationForThirdParty() 0 3 1
A setPreferredLanguage() 0 11 2
A setPackStations() 0 3 1
A setEmail() 0 3 1
A getMobileNumber() 0 3 1
F createFromXML() 0 87 39
A toXML() 0 28 1
A getDeliveryCode() 0 3 1
A getCompanyName() 0 3 1
A getUserID() 0 3 1
A setLastName() 0 3 1
A getPossiblePreferredLanguageValues() 0 6 1
A titleToXML() 0 7 2
A setActivated() 0 3 1
A setDateOfBirth() 0 3 1
A setNumber() 0 3 1
A setFirstName() 0 3 1
A setOptIn() 0 3 1
A setIsComfortZoneUser() 0 3 1
A getPackStations() 0 3 1
A setUseInformationForThirdParty() 0 3 1
A getLastName() 0 3 1
A preferredLanguageToXML() 0 7 2
A setMobileNumber() 0 3 1
A getIsComfortZoneUser() 0 3 1
A getUserName() 0 3 1
A setTitle() 0 7 2
A getTitle() 0 3 1
A postalCodeToXML() 0 7 2
A getDateOfBirth() 0 3 1
A setDeliveryCode() 0 3 1
A addressToXML() 0 15 3
A getPostalCode() 0 3 1
A setStreet() 0 3 1
A getTown() 0 3 1
A getActivated() 0 3 1
A getPossibleTitleValues() 0 5 1
A setUserID() 0 3 1
A setPostalCode() 0 3 1
A getOptIn() 0 3 1
A namingToXML() 0 15 3
A getMobilePrefix() 0 3 1
A addPackStation() 0 3 1
A contactToXML() 0 23 4
A setTown() 0 3 1

How to fix   Complexity   

Complex Class

Complex classes like Customer often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.

Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.

While breaking up the class, it is a good idea to analyze how other classes use Customer, and based on these observations, apply Extract Interface, too.

1
<?php
2
3
namespace Bpost\BpostApiClient\Bpack247;
4
5
use Bpost\BpostApiClient\Exception\BpostLogicException\BpostInvalidValueException;
6
use Bpost\BpostApiClient\Exception\XmlException\BpostXmlNoUserIdFoundException;
7
use DateTime;
8
use DOMDocument;
9
use DOMElement;
10
use SimpleXMLElement;
11
12
/**
13
 * bPost Customer class
14
 *
15
 * @author Tijs Verkoyen <[email protected]>
16
 */
17
class Customer
18
{
19
    const CUSTOMER_PREFERRED_LANGUAGE_NL = 'nl-BE';
20
    const CUSTOMER_PREFERRED_LANGUAGE_FR = 'fr-BE';
21
    const CUSTOMER_PREFERRED_LANGUAGE_EN = 'en-US';
22
23
    const CUSTOMER_TITLE_MR = 'Mr.';
24
    const CUSTOMER_TITLE_MS = 'Ms.';
25
26
    /**
27
     * @var bool
28
     */
29
    private $activated;
30
31
    /**
32
     * @var string
33
     */
34
    private $userID;
35
36
    /**
37
     * @var string
38
     */
39
    private $firstName;
40
41
    /**
42
     * @var string
43
     */
44
    private $lastName;
45
46
    /**
47
     * @var string
48
     */
49
    private $companyName;
50
51
    /**
52
     * @var string
53
     */
54
    private $street;
55
56
    /**
57
     * @var string
58
     */
59
    private $number;
60
61
    /**
62
     * @var string
63
     */
64
    private $email;
65
66
    /**
67
     * @var string
68
     */
69
    private $mobilePrefix = '0032';
70
71
    /**
72
     * @var string
73
     */
74
    private $mobileNumber;
75
76
    /**
77
     * @var string
78
     */
79
    private $postalCode;
80
81
    /**
82
     * @var array
83
     */
84
    private $packStations = array();
85
86
    /**
87
     * @var string
88
     */
89
    private $town;
90
91
    /**
92
     * @var string
93
     */
94
    private $preferredLanguage;
95
96
    /**
97
     * @var string
98
     */
99
    private $title;
100
101
    /**
102
     * @var bool
103
     */
104
    private $isComfortZoneUser;
105
106
    /**
107
     * @var DateTime
108
     */
109
    private $dateOfBirth;
110
111
    /**
112
     * @var string
113
     */
114
    private $deliveryCode;
115
116
    /**
117
     * @var bool
118
     */
119
    private $optIn;
120
121
    /**
122
     * @var bool
123
     */
124
    private $receivePromotions;
125
126
    /**
127
     * @var bool
128
     */
129
    private $useInformationForThirdParty;
130
131
    /**
132
     * @var string
133
     */
134
    private $userName;
135
136
    /**
137
     * @param bool $activated
138
     */
139
    public function setActivated($activated)
140
    {
141
        $this->activated = $activated;
142
    }
143
144
    /**
145
     * @return bool
146
     */
147
    public function getActivated()
148
    {
149
        return $this->activated;
150
    }
151
152
    /**
153
     * @param string $companyName
154
     */
155
    public function setCompanyName($companyName)
156
    {
157
        $this->companyName = $companyName;
158
    }
159
160
    /**
161
     * @return string
162
     */
163
    public function getCompanyName()
164
    {
165
        return $this->companyName;
166
    }
167
168
    /**
169
     * @param DateTime $dateOfBirth
170
     */
171
    public function setDateOfBirth($dateOfBirth)
172
    {
173
        $this->dateOfBirth = $dateOfBirth;
174
    }
175
176
    /**
177
     * @return DateTime
178
     */
179
    public function getDateOfBirth()
180
    {
181
        return $this->dateOfBirth;
182
    }
183
184
    /**
185
     * @param string $deliveryCode
186
     */
187
    public function setDeliveryCode($deliveryCode)
188
    {
189
        $this->deliveryCode = $deliveryCode;
190
    }
191
192
    /**
193
     * @return string
194
     */
195
    public function getDeliveryCode()
196
    {
197
        return $this->deliveryCode;
198
    }
199
200
    /**
201
     * @param string $email
202
     */
203
    public function setEmail($email)
204
    {
205
        $this->email = $email;
206
    }
207
208
    /**
209
     * @return string
210
     */
211
    public function getEmail()
212
    {
213
        return $this->email;
214
    }
215
216
    /**
217
     * @param string $firstName
218
     */
219
    public function setFirstName($firstName)
220
    {
221
        $this->firstName = $firstName;
222
    }
223
224
    /**
225
     * @return string
226
     */
227
    public function getFirstName()
228
    {
229
        return $this->firstName;
230
    }
231
232
    /**
233
     * @param bool $isComfortZoneUser
234
     */
235
    public function setIsComfortZoneUser($isComfortZoneUser)
236
    {
237
        $this->isComfortZoneUser = $isComfortZoneUser;
238
    }
239
240
    /**
241
     * @return bool
242
     */
243
    public function getIsComfortZoneUser()
244
    {
245
        return $this->isComfortZoneUser;
246
    }
247
248
    /**
249
     * @param string $lastName
250
     */
251
    public function setLastName($lastName)
252
    {
253
        $this->lastName = $lastName;
254
    }
255
256
    /**
257
     * @return string
258
     */
259
    public function getLastName()
260
    {
261
        return $this->lastName;
262
    }
263
264
    /**
265
     * @param string $mobileNumber
266
     */
267
    public function setMobileNumber($mobileNumber)
268
    {
269
        $this->mobileNumber = $mobileNumber;
270
    }
271
272
    /**
273
     * @return string
274
     */
275
    public function getMobileNumber()
276
    {
277
        return $this->mobileNumber;
278
    }
279
280
    /**
281
     * @param string $mobilePrefix
282
     */
283
    public function setMobilePrefix($mobilePrefix)
284
    {
285
        $this->mobilePrefix = $mobilePrefix;
286
    }
287
288
    /**
289
     * @return string
290
     */
291
    public function getMobilePrefix()
292
    {
293
        return $this->mobilePrefix;
294
    }
295
296
    /**
297
     * @param string $number
298
     */
299
    public function setNumber($number)
300
    {
301
        $this->number = $number;
302
    }
303
304
    /**
305
     * @return string
306
     */
307
    public function getNumber()
308
    {
309
        return $this->number;
310
    }
311
312
    /**
313
     * @param bool $optIn
314
     */
315
    public function setOptIn($optIn)
316
    {
317
        $this->optIn = $optIn;
318
    }
319
320
    /**
321
     * @return bool
322
     */
323
    public function getOptIn()
324
    {
325
        return $this->optIn;
326
    }
327
328
    /**
329
     * @param CustomerPackStation $packStation
330
     */
331
    public function addPackStation(CustomerPackStation $packStation)
332
    {
333
        $this->packStations[] = $packStation;
334
    }
335
336
    /**
337
     * @param array $packStations
338
     */
339
    public function setPackStations($packStations)
340
    {
341
        $this->packStations = $packStations;
342
    }
343
344
    /**
345
     * @return array
346
     */
347
    public function getPackStations()
348
    {
349
        return $this->packStations;
350
    }
351
352
    /**
353
     * @param string $postalCode
354
     */
355
    public function setPostalCode($postalCode)
356
    {
357
        $this->postalCode = $postalCode;
358
    }
359
360
    /**
361
     * @return string
362
     */
363
    public function getPostalCode()
364
    {
365
        return $this->postalCode;
366
    }
367
368
    /**
369
     * @param string $preferredLanguage
370
     *
371
     * @throws BpostInvalidValueException
372
     */
373
    public function setPreferredLanguage($preferredLanguage)
374
    {
375
        if (!in_array($preferredLanguage, self::getPossiblePreferredLanguageValues())) {
376
            throw new BpostInvalidValueException(
377
                'preferred language',
378
                $preferredLanguage,
379
                self::getPossiblePreferredLanguageValues()
380
            );
381
        }
382
383
        $this->preferredLanguage = $preferredLanguage;
384
    }
385
386
    /**
387
     * @return string
388
     */
389
    public function getPreferredLanguage()
390
    {
391
        return $this->preferredLanguage;
392
    }
393
394
    /**
395
     * @return array
396
     */
397
    public static function getPossiblePreferredLanguageValues()
398
    {
399
        return array(
400
            self::CUSTOMER_PREFERRED_LANGUAGE_NL,
401
            self::CUSTOMER_PREFERRED_LANGUAGE_FR,
402
            self::CUSTOMER_PREFERRED_LANGUAGE_EN,
403
        );
404
    }
405
406
    /**
407
     * @param bool $receivePromotions
408
     */
409
    public function setReceivePromotions($receivePromotions)
410
    {
411
        $this->receivePromotions = $receivePromotions;
412
    }
413
414
    /**
415
     * @return bool
416
     */
417
    public function getReceivePromotions()
418
    {
419
        return $this->receivePromotions;
420
    }
421
422
    /**
423
     * @param string $street
424
     */
425
    public function setStreet($street)
426
    {
427
        $this->street = $street;
428
    }
429
430
    /**
431
     * @return string
432
     */
433
    public function getStreet()
434
    {
435
        return $this->street;
436
    }
437
438
    /**
439
     * @param string $title
440
     *
441
     * @throws BpostInvalidValueException
442
     */
443
    public function setTitle($title)
444
    {
445
        if (!in_array($title, self::getPossibleTitleValues())) {
446
            throw new BpostInvalidValueException('title', $title, self::getPossibleTitleValues());
447
        }
448
449
        $this->title = $title;
450
    }
451
452
    /**
453
     * @return string
454
     */
455
    public function getTitle()
456
    {
457
        return $this->title;
458
    }
459
460
    /**
461
     * @return array
462
     */
463
    public static function getPossibleTitleValues()
464
    {
465
        return array(
466
            self::CUSTOMER_TITLE_MR,
467
            self::CUSTOMER_TITLE_MS,
468
        );
469
    }
470
471
    /**
472
     * @param string $town
473
     */
474
    public function setTown($town)
475
    {
476
        $this->town = $town;
477
    }
478
479
    /**
480
     * @return string
481
     */
482
    public function getTown()
483
    {
484
        return $this->town;
485
    }
486
487
    /**
488
     * @param bool $useInformationForThirdParty
489
     */
490
    public function setUseInformationForThirdParty($useInformationForThirdParty)
491
    {
492
        $this->useInformationForThirdParty = $useInformationForThirdParty;
493
    }
494
495
    /**
496
     * @return bool
497
     */
498
    public function getUseInformationForThirdParty()
499
    {
500
        return $this->useInformationForThirdParty;
501
    }
502
503
    /**
504
     * @param string $userID
505
     */
506
    public function setUserID($userID)
507
    {
508
        $this->userID = $userID;
509
    }
510
511
    /**
512
     * @return string
513
     */
514
    public function getUserID()
515
    {
516
        return $this->userID;
517
    }
518
519
    /**
520
     * @param string $userName
521
     */
522
    public function setUserName($userName)
523
    {
524
        $this->userName = $userName;
525
    }
526
527
    /**
528
     * @return string
529
     */
530
    public function getUserName()
531
    {
532
        return $this->userName;
533
    }
534
535
    /**
536
     * Return the object as an array for usage in the XML
537
     *
538
     * @param DOMDocument $document
539
     *
540
     * @return DOMElement
541
     */
542
    public function toXML(DOMDocument $document)
543
    {
544
        $customer = $document->createElement(
545
            'Customer'
546
        );
547
        $customer->setAttribute(
548
            'xmlns',
549
            'http://schema.post.be/ServiceController/customer'
550
        );
551
        $customer->setAttribute(
552
            'xmlns:xsi',
553
            'http://www.w3.org/2001/XMLSchema-instance'
554
        );
555
        $customer->setAttribute(
556
            'xsi:schemaLocation',
557
            'http://schema.post.be/ServiceController/customer'
558
        );
559
560
        $document->appendChild($customer);
561
562
        $this->namingToXML($document, $customer);
563
        $this->addressToXML($document, $customer);
564
        $this->contactToXML($document, $customer);
565
        $this->postalCodeToXML($document, $customer);
566
        $this->preferredLanguageToXML($document, $customer);
567
        $this->titleToXML($document, $customer);
568
569
        return $customer;
570
    }
571
572
    /**
573
     * @param SimpleXMLElement $xml
574
     *
575
     * @return Customer
576
     *
577
     * @throws BpostInvalidValueException
578
     * @throws BpostXmlNoUserIdFoundException
579
     */
580
    public static function createFromXML(SimpleXMLElement $xml)
581
    {
582
        // @todo work with classmaps ...
583
        if (!isset($xml->UserID)) {
584
            throw new BpostXmlNoUserIdFoundException();
585
        }
586
587
        $customer = new Customer();
588
589
        if (isset($xml->UserID) && $xml->UserID != '') {
590
            $customer->setUserID((string) $xml->UserID);
591
        }
592
        if (isset($xml->FirstName) && $xml->FirstName != '') {
593
            $customer->setFirstName((string) $xml->FirstName);
594
        }
595
        if (isset($xml->LastName) && $xml->LastName != '') {
596
            $customer->setLastName((string) $xml->LastName);
597
        }
598
        if (isset($xml->Street) && $xml->Street != '') {
599
            $customer->setStreet((string) $xml->Street);
600
        }
601
        if (isset($xml->Number) && $xml->Number != '') {
602
            $customer->setNumber((string) $xml->Number);
603
        }
604
        if (isset($xml->CompanyName) && $xml->CompanyName != '') {
605
            $customer->setCompanyName((string) $xml->CompanyName);
606
        }
607
        if (isset($xml->DateOfBirth) && $xml->DateOfBirth != '') {
608
            $dateTime = new DateTime((string) $xml->DateOfBirth);
609
            $customer->setDateOfBirth($dateTime);
610
        }
611
        if (isset($xml->DeliveryCode) && $xml->DeliveryCode != '') {
612
            $customer->setDeliveryCode(
613
                (string) $xml->DeliveryCode
614
            );
615
        }
616
        if (isset($xml->Email) && $xml->Email != '') {
617
            $customer->setEmail((string) $xml->Email);
618
        }
619
        if (isset($xml->MobilePrefix) && $xml->MobilePrefix != '') {
620
            $customer->setMobilePrefix(
621
                trim((string) $xml->MobilePrefix)
622
            );
623
        }
624
        if (isset($xml->MobileNumber) && $xml->MobileNumber != '') {
625
            $customer->setMobileNumber(
626
                (string) $xml->MobileNumber
627
            );
628
        }
629
        if (isset($xml->Postalcode) && $xml->Postalcode != '') {
630
            $customer->setPostalCode(
631
                (string) $xml->Postalcode
632
            );
633
        }
634
        if (isset($xml->PreferredLanguage) && $xml->PreferredLanguage != '') {
635
            $customer->setPreferredLanguage(
636
                (string) $xml->PreferredLanguage
637
            );
638
        }
639
        if (isset($xml->ReceivePromotions) && $xml->ReceivePromotions != '') {
640
            $receivePromotions = in_array((string) $xml->ReceivePromotions, array('true', '1'));
641
            $customer->setReceivePromotions($receivePromotions);
642
        }
643
        if (isset($xml->actived) && $xml->actived != '') {
644
            $activated = in_array((string) $xml->actived, array('true', '1'));
645
            $customer->setActivated($activated);
646
        }
647
        if (isset($xml->Title) && $xml->Title != '') {
648
            $title = (string) $xml->Title;
649
            $title = ucfirst(strtolower($title));
650
            if (substr($title, -1) != '.') {
651
                $title .= '.';
652
            }
653
654
            $customer->setTitle($title);
655
        }
656
        if (isset($xml->Town) && $xml->Town != '') {
657
            $customer->setTown((string) $xml->Town);
658
        }
659
660
        if (isset($xml->PackStations->CustomerPackStation)) {
661
            foreach ($xml->PackStations->CustomerPackStation as $packStation) {
662
                $customer->addPackStation(CustomerPackStation::createFromXML($packStation));
663
            }
664
        }
665
666
        return $customer;
667
    }
668
669
    /**
670
     * @param DOMDocument $document
671
     * @param DOMElement  $customer
672
     */
673
    private function namingToXML(DOMDocument $document, DOMElement $customer)
674
    {
675
        if ($this->getFirstName() !== null) {
0 ignored issues
show
introduced by
The condition $this->getFirstName() !== null is always true.
Loading history...
676
            $customer->appendChild(
677
                $document->createElement(
678
                    'FirstName',
679
                    $this->getFirstName()
680
                )
681
            );
682
        }
683
        if ($this->getLastName() !== null) {
0 ignored issues
show
introduced by
The condition $this->getLastName() !== null is always true.
Loading history...
684
            $customer->appendChild(
685
                $document->createElement(
686
                    'LastName',
687
                    $this->getLastName()
688
                )
689
            );
690
        }
691
    }
692
693
    /**
694
     * @param DOMDocument $document
695
     * @param DOMElement  $customer
696
     */
697
    private function contactToXML(DOMDocument $document, DOMElement $customer)
698
    {
699
        if ($this->getEmail() !== null) {
0 ignored issues
show
introduced by
The condition $this->getEmail() !== null is always true.
Loading history...
700
            $customer->appendChild(
701
                $document->createElement(
702
                    'Email',
703
                    $this->getEmail()
704
                )
705
            );
706
        }
707
        if ($this->getMobilePrefix() !== null) {
0 ignored issues
show
introduced by
The condition $this->getMobilePrefix() !== null is always true.
Loading history...
708
            $customer->appendChild(
709
                $document->createElement(
710
                    'MobilePrefix',
711
                    $this->getMobilePrefix()
712
                )
713
            );
714
        }
715
        if ($this->getMobileNumber() !== null) {
0 ignored issues
show
introduced by
The condition $this->getMobileNumber() !== null is always true.
Loading history...
716
            $customer->appendChild(
717
                $document->createElement(
718
                    'MobileNumber',
719
                    $this->getMobileNumber()
720
                )
721
            );
722
        }
723
    }
724
725
    /**
726
     * @param DOMDocument $document
727
     * @param DOMElement  $customer
728
     */
729
    private function addressToXML(DOMDocument $document, DOMElement $customer)
730
    {
731
        if ($this->getStreet() !== null) {
0 ignored issues
show
introduced by
The condition $this->getStreet() !== null is always true.
Loading history...
732
            $customer->appendChild(
733
                $document->createElement(
734
                    'Street',
735
                    $this->getStreet()
736
                )
737
            );
738
        }
739
        if ($this->getNumber() !== null) {
0 ignored issues
show
introduced by
The condition $this->getNumber() !== null is always true.
Loading history...
740
            $customer->appendChild(
741
                $document->createElement(
742
                    'Number',
743
                    $this->getNumber()
744
                )
745
            );
746
        }
747
    }
748
749
    /**
750
     * @param DOMDocument $document
751
     * @param DOMElement  $customer
752
     */
753
    private function preferredLanguageToXML(DOMDocument $document, DOMElement $customer)
754
    {
755
        if ($this->getPreferredLanguage() !== null) {
0 ignored issues
show
introduced by
The condition $this->getPreferredLanguage() !== null is always true.
Loading history...
756
            $customer->appendChild(
757
                $document->createElement(
758
                    'PreferredLanguage',
759
                    $this->getPreferredLanguage()
760
                )
761
            );
762
        }
763
    }
764
765
    /**
766
     * @param DOMDocument $document
767
     * @param DOMElement  $customer
768
     */
769
    private function titleToXML(DOMDocument $document, DOMElement $customer)
770
    {
771
        if ($this->getTitle() !== null) {
0 ignored issues
show
introduced by
The condition $this->getTitle() !== null is always true.
Loading history...
772
            $customer->appendChild(
773
                $document->createElement(
774
                    'Title',
775
                    $this->getTitle()
776
                )
777
            );
778
        }
779
    }
780
781
    /**
782
     * @param DOMDocument $document
783
     * @param DOMElement  $customer
784
     */
785
    private function postalCodeToXML(DOMDocument $document, DOMElement $customer)
786
    {
787
        if ($this->getPostalCode() !== null) {
0 ignored issues
show
introduced by
The condition $this->getPostalCode() !== null is always true.
Loading history...
788
            $customer->appendChild(
789
                $document->createElement(
790
                    'PostalCode',
791
                    $this->getPostalCode()
792
                )
793
            );
794
        }
795
    }
796
}
797