Contact   F
last analyzed

Complexity

Total Complexity 84

Size/Duplication

Total Lines 683
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Importance

Changes 0
Metric Value
dl 0
loc 683
rs 1.917
c 0
b 0
f 0
wmc 84
lcom 1
cbo 0

53 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A __toString() 0 4 1
A getId() 0 4 1
A setId() 0 4 1
A getHash() 0 4 1
A setHash() 0 4 1
A getSubscribed() 0 4 1
A setSubscribed() 0 8 2
A getUnsubscribed() 0 4 1
A setUnsubscribed() 0 8 2
A getCreated() 0 4 1
A setCreated() 0 8 2
A getUpdated() 0 4 1
A setUpdated() 0 8 2
A getContactListId() 0 4 1
A setContactListId() 0 4 1
A getEmail() 0 4 1
A setEmail() 0 4 1
A getLanguage() 0 4 1
A setLanguage() 0 4 1
A getPoliteForm() 0 4 1
A setPoliteForm() 0 4 1
A getTitle() 0 4 1
A setTitle() 0 4 1
A getCompany() 0 4 1
A setCompany() 0 4 1
A getFirstname() 0 4 1
A setFirstname() 0 4 1
A getLastname() 0 4 1
A setLastname() 0 4 1
A getAddress() 0 4 1
A setAddress() 0 4 1
A getAddress2() 0 4 1
A setAddress2() 0 4 1
A getZip() 0 4 1
A setZip() 0 4 1
A getCity() 0 4 1
A setCity() 0 4 1
A getCountry() 0 4 1
A setCountry() 0 4 1
A getPhone() 0 4 1
A setPhone() 0 4 1
A getBirthday() 0 4 1
A setBirthday() 0 4 1
A getInactive() 0 4 1
A setInactive() 0 4 1
A getCustomValues() 0 4 1
A setCustomValues() 0 4 1
A setCustomValue() 0 4 1
A getCustomValue() 0 8 2
A hasCustomValue() 0 4 1
A toAPI() 0 36 5
D fromAPI() 0 72 23

How to fix   Complexity   

Complex Class

Complex classes like Contact 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. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.

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 Contact, and based on these observations, apply Extract Interface, too.

1
<?php
2
/**
3
 * Date: 20/08/15
4
 */
5
6
namespace Mailxpert\Model;
7
8
/**
9
 * Class Contact
10
 * @package Mailxpert\Model
11
 */
12
class Contact
13
{
14
    const POLITE_FORM_FORMAL = 'formal';
15
    const POLITE_FORM_INFORMAL = 'informal';
16
17
    const TITLE_MALE = 'male';
18
    const TITLE_FEMALE = 'female';
19
    const TITLE_COMPANY = 'company';
20
    const TITLE_FAMILY = 'family';
21
22
    /**
23
     * @var string
24
     */
25
    private $id;
26
27
    /**
28
     * @var string
29
     */
30
    private $hash;
31
32
    /**
33
     * @var  \DateTime
34
     */
35
    private $subscribed;
36
37
    /**
38
     * @var \DateTime
39
     */
40
    private $unsubscribed;
41
42
    /**
43
     * @var \DateTime
44
     */
45
    private $created;
46
47
    /**
48
     * @var \DateTime
49
     */
50
    private $updated;
51
52
    /**
53
     * @var string
54
     */
55
    private $contactListId;
56
57
    /**
58
     * @var string
59
     */
60
    private $email;
61
62
    /**
63
     * @var string
64
     */
65
    private $language;
66
67
    /**
68
     * @var string
69
     */
70
    private $politeForm;
71
72
    /**
73
     * @var string
74
     */
75
    private $title;
76
77
    /**
78
     * @var string
79
     */
80
    private $company;
81
82
    /**
83
     * @var string
84
     */
85
    private $firstname;
86
87
    /**
88
     * @var string
89
     */
90
    private $lastname;
91
92
    /**
93
     * @var string
94
     */
95
    private $address;
96
97
    /**
98
     * @var string
99
     */
100
    private $address2;
101
102
    /**
103
     * @var string
104
     */
105
    private $zip;
106
107
    /**
108
     * @var string
109
     */
110
    private $city;
111
112
    /**
113
     * @var string
114
     */
115
    private $country;
116
117
    /**
118
     * @var string
119
     */
120
    private $phone;
121
122
    /**
123
     * @var string
124
     */
125
    private $birthday;
126
127
    /**
128
     * @var bool
129
     */
130
    private $inactive;
131
132
    /**
133
     * @var array
134
     */
135
    private $customValues = [];
136
137
    /**
138
     * Contact constructor.
139
     *
140
     * @param string $email
141
     * @param string $id
142
     */
143
    public function __construct($email, $id = null)
144
    {
145
        $this->id = $id;
146
        $this->email = $email;
147
    }
148
149
    /**
150
     * @return string
151
     */
152
    public function __toString()
153
    {
154
        return (string) $this->getEmail();
155
    }
156
157
158
    /**
159
     * @return string
160
     */
161
    public function getId()
162
    {
163
        return $this->id;
164
    }
165
166
    /**
167
     * @param string $id
168
     */
169
    public function setId($id)
170
    {
171
        $this->id = $id;
172
    }
173
174
    /**
175
     * @return string
176
     */
177
    public function getHash()
178
    {
179
        return $this->hash;
180
    }
181
182
    /**
183
     * @param string $hash
184
     */
185
    public function setHash($hash)
186
    {
187
        $this->hash = $hash;
188
    }
189
190
    /**
191
     * @return \DateTime
192
     */
193
    public function getSubscribed()
194
    {
195
        return $this->subscribed;
196
    }
197
198
    /**
199
     * @param \DateTime|string $subscribed
200
     */
201
    public function setSubscribed($subscribed)
202
    {
203
        if (is_string($subscribed)) {
204
            $this->subscribed = new \DateTime($subscribed);
205
        } else {
206
            $this->subscribed = $subscribed;
207
        }
208
    }
209
210
    /**
211
     * @return \DateTime
212
     */
213
    public function getUnsubscribed()
214
    {
215
        return $this->unsubscribed;
216
    }
217
218
    /**
219
     * @param \DateTime $unsubscribed
220
     */
221
    public function setUnsubscribed($unsubscribed)
222
    {
223
        if (is_string($unsubscribed)) {
224
            $this->unsubscribed = new \DateTime($unsubscribed);
225
        } else {
226
            $this->unsubscribed = $unsubscribed;
227
        }
228
    }
229
230
    /**
231
     * @return \DateTime
232
     */
233
    public function getCreated()
234
    {
235
        return $this->created;
236
    }
237
238
    /**
239
     * @param \DateTime $created
240
     */
241
    public function setCreated($created)
242
    {
243
        if (is_string($created)) {
244
            $this->created = new \DateTime($created);
245
        } else {
246
            $this->created = $created;
247
        }
248
    }
249
250
    /**
251
     * @return \DateTime
252
     */
253
    public function getUpdated()
254
    {
255
        return $this->updated;
256
    }
257
258
    /**
259
     * @param \DateTime $updated
260
     */
261
    public function setUpdated($updated)
262
    {
263
        if (is_string($updated)) {
264
            $this->updated = new \DateTime($updated);
265
        } else {
266
            $this->updated = $updated;
267
        }
268
    }
269
270
    /**
271
     * @return string
272
     */
273
    public function getContactListId()
274
    {
275
        return $this->contactListId;
276
    }
277
278
    /**
279
     * @param string $contactListId
280
     */
281
    public function setContactListId($contactListId)
282
    {
283
        $this->contactListId = $contactListId;
284
    }
285
286
    /**
287
     * @return string
288
     */
289
    public function getEmail()
290
    {
291
        return $this->email;
292
    }
293
294
    /**
295
     * @param string $email
296
     */
297
    public function setEmail($email)
298
    {
299
        $this->email = $email;
300
    }
301
302
    /**
303
     * @return string
304
     */
305
    public function getLanguage()
306
    {
307
        return $this->language;
308
    }
309
310
    /**
311
     * @param string $language
312
     */
313
    public function setLanguage($language)
314
    {
315
        $this->language = $language;
316
    }
317
318
    /**
319
     * @return string
320
     */
321
    public function getPoliteForm()
322
    {
323
        return $this->politeForm;
324
    }
325
326
    /**
327
     * @param string $politeForm
328
     */
329
    public function setPoliteForm($politeForm)
330
    {
331
        $this->politeForm = $politeForm;
332
    }
333
334
    /**
335
     * @return string
336
     */
337
    public function getTitle()
338
    {
339
        return $this->title;
340
    }
341
342
    /**
343
     * @param string $title
344
     */
345
    public function setTitle($title)
346
    {
347
        $this->title = $title;
348
    }
349
350
    /**
351
     * @return string
352
     */
353
    public function getCompany()
354
    {
355
        return $this->company;
356
    }
357
358
    /**
359
     * @param string $company
360
     */
361
    public function setCompany($company)
362
    {
363
        $this->company = $company;
364
    }
365
366
    /**
367
     * @return string
368
     */
369
    public function getFirstname()
370
    {
371
        return $this->firstname;
372
    }
373
374
    /**
375
     * @param string $firstname
376
     */
377
    public function setFirstname($firstname)
378
    {
379
        $this->firstname = $firstname;
380
    }
381
382
    /**
383
     * @return string
384
     */
385
    public function getLastname()
386
    {
387
        return $this->lastname;
388
    }
389
390
    /**
391
     * @param string $lastname
392
     */
393
    public function setLastname($lastname)
394
    {
395
        $this->lastname = $lastname;
396
    }
397
398
    /**
399
     * @return string
400
     */
401
    public function getAddress()
402
    {
403
        return $this->address;
404
    }
405
406
    /**
407
     * @param string $address
408
     */
409
    public function setAddress($address)
410
    {
411
        $this->address = $address;
412
    }
413
414
    /**
415
     * @return string
416
     */
417
    public function getAddress2()
418
    {
419
        return $this->address2;
420
    }
421
422
    /**
423
     * @param string $address2
424
     */
425
    public function setAddress2($address2)
426
    {
427
        $this->address2 = $address2;
428
    }
429
430
    /**
431
     * @return string
432
     */
433
    public function getZip()
434
    {
435
        return $this->zip;
436
    }
437
438
    /**
439
     * @param string $zip
440
     */
441
    public function setZip($zip)
442
    {
443
        $this->zip = $zip;
444
    }
445
446
    /**
447
     * @return string
448
     */
449
    public function getCity()
450
    {
451
        return $this->city;
452
    }
453
454
    /**
455
     * @param string $city
456
     */
457
    public function setCity($city)
458
    {
459
        $this->city = $city;
460
    }
461
462
    /**
463
     * @return string
464
     */
465
    public function getCountry()
466
    {
467
        return $this->country;
468
    }
469
470
    /**
471
     * @param string $country
472
     */
473
    public function setCountry($country)
474
    {
475
        $this->country = $country;
476
    }
477
478
    /**
479
     * @return string
480
     */
481
    public function getPhone()
482
    {
483
        return $this->phone;
484
    }
485
486
    /**
487
     * @param string $phone
488
     */
489
    public function setPhone($phone)
490
    {
491
        $this->phone = $phone;
492
    }
493
494
    /**
495
     * @return string
496
     */
497
    public function getBirthday()
498
    {
499
        return $this->birthday;
500
    }
501
502
    /**
503
     * @param string $birthday
504
     */
505
    public function setBirthday($birthday)
506
    {
507
        $this->birthday = $birthday;
508
    }
509
510
    /**
511
     * @return bool
512
     */
513
    public function getInactive()
514
    {
515
        return $this->inactive;
516
    }
517
518
    /**
519
     * @param bool $inactive
520
     */
521
    public function setInactive($inactive)
522
    {
523
        $this->inactive = $inactive;
524
    }
525
526
    /**
527
     * @return array
528
     */
529
    public function getCustomValues()
530
    {
531
        return $this->customValues;
532
    }
533
534
    /**
535
     * @param array $customValues
536
     */
537
    public function setCustomValues($customValues)
538
    {
539
        $this->customValues = $customValues;
540
    }
541
542
    /**
543
     * @param string $alias
544
     * @param string $value
545
     */
546
    public function setCustomValue($alias, $value)
547
    {
548
        $this->customValues[trim($alias)] = $value;
549
    }
550
551
    /**
552
     * @param string      $alias
553
     * @param string|null $default
554
     *
555
     * @return null
556
     */
557
    public function getCustomValue($alias, $default = null)
558
    {
559
        if ($this->hasCustomValue($alias)) {
560
            return $this->customValues[trim($alias)];
561
        } else {
562
            return $default;
563
        }
564
    }
565
566
    /**
567
     * @param string $alias
568
     *
569
     * @return bool
570
     */
571
    public function hasCustomValue($alias)
572
    {
573
        return isset($this->customValues[trim($alias)]);
574
    }
575
576
    /**
577
     * @param array $exclude
578
     * @param bool  $clean
579
     *
580
     * @return array
581
     */
582
    public function toAPI(array $exclude = [], $clean = true)
583
    {
584
        $data = [
585
            "email" => $this->getEmail(),
586
            "contact_list_id" => $this->getContactListId(),
587
            "language" => $this->getLanguage(),
588
            "polite_form" => $this->getPoliteForm(),
589
            "title" => $this->getTitle(),
590
            "company" => $this->getCompany(),
591
            "firstname" => $this->getFirstname(),
592
            "lastname" => $this->getLastname(),
593
            "address" => $this->getAddress(),
594
            "address2" => $this->getAddress2(),
595
            "zip" => $this->getZip(),
596
            "city" => $this->getCity(),
597
            "country" => $this->getCountry(),
598
            "phone" => $this->getPhone(),
599
            "birthday" => $this->getBirthday(),
600
            "inactive" => $this->getInactive(),
601
            "custom_values" => $this->getCustomValues(),
602
        ];
603
604
        if ($clean) {
605
            foreach ($data as $key => $value) {
606
                if (empty($value)) {
607
                    unset($data[$key]);
608
                }
609
            }
610
        }
611
612
        foreach ($exclude as $field) {
613
            unset($data[$field]);
614
        }
615
616
        return $data;
617
    }
618
619
    /**
620
     * @param array $data
621
     */
622
    public function fromAPI(array $data)
623
    {
624
        foreach ($data as $key => $value) {
625
            switch ($key) {
626
                case "hash":
627
                    $this->setHash($value);
628
                    break;
629
                case "contact_list_id":
630
                    $this->setContactListId($value);
631
                    break;
632
                case "language":
633
                    $this->setLanguage($value);
634
                    break;
635
                case "polite_form":
636
                    $this->setPoliteForm($value);
637
                    break;
638
                case "title":
639
                    $this->setTitle($value);
640
                    break;
641
                case "company":
642
                    $this->setCompany($value);
643
                    break;
644
                case "firstname":
645
                    $this->setFirstname($value);
646
                    break;
647
                case "lastname":
648
                    $this->setLastname($value);
649
                    break;
650
                case "address":
651
                    $this->setAddress($value);
652
                    break;
653
                case "address2":
654
                    $this->setAddress2($value);
655
                    break;
656
                case "zip":
657
                    $this->setZip($value);
658
                    break;
659
                case "city":
660
                    $this->setCity($value);
661
                    break;
662
                case "country":
663
                    $this->setCountry($value);
664
                    break;
665
                case "phone":
666
                    $this->setPhone($value);
667
                    break;
668
                case "birthday":
669
                    $this->setBirthday($value);
670
                    break;
671
                case "inactive":
672
                    $this->setInactive($value);
673
                    break;
674
                case "custom_values":
675
                    $this->setCustomValues($value);
676
                    break;
677
                case "subscribed":
678
                    $this->setSubscribed($value);
679
                    break;
680
                case "unsubscribed":
681
                    $this->setUnsubscribed($value);
682
                    break;
683
                case "created":
684
                    $this->setCreated($value);
685
                    break;
686
                case "updated":
687
                    $this->setUpdated($value);
688
                    break;
689
                default:
690
                    break;
691
            }
692
        }
693
    }
694
}
695