Total Complexity | 105 |
Total Lines | 775 |
Duplicated Lines | 0 % |
Changes | 2 | ||
Bugs | 1 | Features | 0 |
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 |
||
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() |
||
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() |
||
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) |
||
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) |
||
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) { |
||
|
|||
676 | $customer->appendChild( |
||
677 | $document->createElement( |
||
678 | 'FirstName', |
||
679 | $this->getFirstName() |
||
680 | ) |
||
681 | ); |
||
682 | } |
||
683 | if ($this->getLastName() !== null) { |
||
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) { |
||
700 | $customer->appendChild( |
||
701 | $document->createElement( |
||
702 | 'Email', |
||
703 | $this->getEmail() |
||
704 | ) |
||
705 | ); |
||
706 | } |
||
707 | if ($this->getMobilePrefix() !== null) { |
||
708 | $customer->appendChild( |
||
709 | $document->createElement( |
||
710 | 'MobilePrefix', |
||
711 | $this->getMobilePrefix() |
||
712 | ) |
||
713 | ); |
||
714 | } |
||
715 | if ($this->getMobileNumber() !== null) { |
||
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) { |
||
732 | $customer->appendChild( |
||
733 | $document->createElement( |
||
734 | 'Street', |
||
735 | $this->getStreet() |
||
736 | ) |
||
737 | ); |
||
738 | } |
||
739 | if ($this->getNumber() !== null) { |
||
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) { |
||
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) |
||
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) { |
||
788 | $customer->appendChild( |
||
789 | $document->createElement( |
||
790 | 'PostalCode', |
||
791 | $this->getPostalCode() |
||
792 | ) |
||
793 | ); |
||
794 | } |
||
795 | } |
||
796 | } |
||
797 |