Customer::toXML()   F
last analyzed

Complexity

Conditions 11
Paths 1024

Size

Total Lines 103
Code Lines 64

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 94
CRAP Score 11

Importance

Changes 0
Metric Value
dl 0
loc 103
ccs 94
cts 94
cp 1
rs 3.1764
c 0
b 0
f 0
cc 11
eloc 64
nc 1024
nop 1
crap 11

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

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
706 1
            $receivePromotions = in_array((string) $xml->ReceivePromotions, array('true', '1'));
707 1
            $customer->setReceivePromotions($receivePromotions);
708 1
        }
709 1 View Code Duplication
        if (isset($xml->actived) && $xml->actived != '') {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
710 1
            $activated = in_array((string) $xml->actived, array('true', '1'));
711 1
            $customer->setActivated($activated);
712 1
        }
713 1
        if (isset($xml->Title) && $xml->Title != '') {
714 1
            $title = (string) $xml->Title;
715 1
            $title = ucfirst(strtolower($title));
716 1
            if (substr($title, -1) != '.') {
717 1
                $title .= '.';
718 1
            }
719
720 1
            $customer->setTitle($title);
721 1
        }
722 1
        if (isset($xml->Town) && $xml->Town != '') {
723 1
            $customer->setTown((string) $xml->Town);
724 1
        }
725
726 1
        if (isset($xml->PackStations->CustomerPackStation)) {
727 1
            foreach ($xml->PackStations->CustomerPackStation as $packStation) {
728 1
                $customer->addPackStation(CustomerPackStation::createFromXML($packStation));
729 1
            }
730 1
        }
731
732 1
        return $customer;
733
    }
734
}
735