Completed
Push — master ( 4a5840...1f0278 )
by
unknown
11s
created

Account   D

Complexity

Total Complexity 45

Size/Duplication

Total Lines 670
Duplicated Lines 10.3 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 2
Bugs 1 Features 0
Metric Value
dl 69
loc 670
rs 4.0968
c 2
b 1
f 0
wmc 45
lcom 1
cbo 2

40 Methods

Rating   Name   Duplication   Size   Complexity  
A addAlternativePhone() 0 11 1
A addAddress() 16 16 1
A create() 0 4 1
A get() 0 4 1
A initialize() 0 8 1
A getId() 0 4 1
A getAddress() 0 4 1
A getFullname() 0 4 1
A getBirthDate() 0 4 1
A getPhoneAreaCode() 0 4 1
A getPhoneCountryCode() 0 4 1
A getPhoneNumber() 0 4 1
A getTaxDocumentType() 0 4 1
A getAlternativePhones() 0 4 1
A getCompany() 0 4 1
A getTaxDocumentNumber() 0 4 1
A getType() 0 4 1
A populate() 0 48 1
A setEmail() 0 6 1
A setName() 0 6 1
A setLastName() 0 6 1
A setBirthDate() 10 10 2
A setTaxDocument() 8 8 1
A setPhone() 9 9 1
A setIdentityDocument() 0 10 1
A setNationality() 0 6 1
A setBirthPlace() 0 6 1
A setParentsName() 0 8 1
A setSite() 0 6 1
A setTransparentAccount() 0 6 1
A setBusinessSegment() 0 6 1
A setCompanyName() 0 8 1
A initializeCompany() 0 6 2
A setCompanyOpeningDate() 0 10 2
A setCompanyTaxDocument() 9 9 1
A setCompanyMainActivity() 0 9 1
A setCompanyAddress() 17 17 1
A setCompanyPhone() 0 10 1
A setType() 0 6 1
A checkExistence() 0 14 3

How to fix   Duplicated Code    Complexity   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

Complex Class

 Tip:   Before tackling complexity, make sure that you eliminate any duplication first. This often can reduce the size of classes significantly.

Complex classes like Account 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 Account, and based on these observations, apply Extract Interface, too.

1
<?php
2
3
namespace Moip\Resource;
4
5
use Moip\Exceptions\ValidationException;
6
use stdClass;
7
8
/**
9
 * Class Account.
10
 */
11
class Account extends MoipResource
12
{
13
    /**
14
     * Path accounts API.
15
     *
16
     * @const string
17
     */
18
    const PATH = 'accounts';
19
20
    /**
21
     * Standard country .
22
     *
23
     * @const string
24
     */
25
    const ADDRESS_COUNTRY = 'BRA';
26
27
    /**
28
     * Standard document type.
29
     *
30
     * @const string
31
     */
32
    const TAX_DOCUMENT = 'CPF';
33
34
    /**
35
     * Standard company document type.
36
     *
37
     * @const string
38
     */
39
    const COMPANY_TAX_DOCUMENT = 'CNPJ';
40
41
    /**
42
     * Default Account Type.
43
     *
44
     * @var string
45
     */
46
    const ACCOUNT_TYPE = 'MERCHANT';
47
48
    /**
49
     * Initialize a new instance.
50
     */
51
    public function initialize()
52
    {
53
        $this->data = new stdClass();
54
        $this->data->email = new stdClass();
55
        $this->data->person = new stdClass();
56
        $this->data->person->alternativePhones = [];
57
        $this->data->type = self::ACCOUNT_TYPE;
58
    }
59
60
    /**
61
     * Add a new address to the account.
62
     *
63
     * @param string $street     Street address.
64
     * @param string $number     Number address.
65
     * @param string $district   Neighborhood address.
66
     * @param string $city       City address.
67
     * @param string $state      State address.
68
     * @param string $zip        The zip code billing address.
69
     * @param string $complement Address complement.
70
     * @param string $country    Country ISO-alpha3 format, BRA example.
71
     *
72
     * @return $this
73
     */
74 View Code Duplication
    public function addAddress($street, $number, $district, $city, $state, $zip, $complement = null, $country = self::ADDRESS_COUNTRY)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in 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...
75
    {
76
        $address = new stdClass();
77
        $address->street = $street;
78
        $address->streetNumber = $number;
79
        $address->complement = $complement;
80
        $address->district = $district;
81
        $address->city = $city;
82
        $address->state = $state;
83
        $address->country = $country;
84
        $address->zipCode = $zip;
85
86
        $this->data->person->address = $address;
87
88
        return $this;
89
    }
90
91
    /**
92
     * Add alternative phone to an account.
93
     *
94
     * @param int $areaCode    DDD telephone.
95
     * @param int $number      Telephone number.
96
     * @param int $countryCode Country code.
97
     *
98
     * @return \Moip\Resource\Account
99
     */
100
    public function addAlternativePhone($areaCode, $number, $countryCode = 55)
101
    {
102
        $alternativePhone = new stdClass();
103
        $alternativePhone->countryCode = $countryCode;
104
        $alternativePhone->areaCode = $areaCode;
105
        $alternativePhone->number = $number;
106
107
        $this->data->person->alternativePhones[] = $alternativePhone;
108
109
        return $this;
110
    }
111
112
    /**
113
     * Create a new account.
114
     *
115
     * @return \stdClass
116
     */
117
    public function create()
118
    {
119
        return $this->createResource(sprintf('/%s/%s/', MoipResource::VERSION, self::PATH));
120
    }
121
122
    /**
123
     * Find a account.
124
     *
125
     * @param string $moip_id
126
     *
127
     * @return stdClass
128
     */
129
    public function get($moip_id)
130
    {
131
        return $this->getByPath(sprintf('/%s/%s/%s', MoipResource::VERSION, self::PATH, $moip_id));
132
    }
133
134
    /**
135
     * Check if an account exists.
136
     *
137
     * @param string $tax_document
138
     *
139
     * @return bool
140
     */
141
    public function checkExistence($tax_document)
142
    {
143
        try {
144
            $this->getByPathNoPopulate(sprintf('/%s/%s/%s?tax_document=%s', MoipResource::VERSION, self::PATH, 'exists', $tax_document));
145
146
            return true;
147
        } catch (ValidationException $e) {
148
            if ($e->getStatusCode() != 404) {
149
                throw new ValidationException($e->getStatusCode(), $e->getErrors());
150
            }
151
        }
152
153
        return false;
154
    }
155
156
    /**
157
     * Get account id.
158
     *
159
     * @return string The buyer id.
160
     */
161
    public function getId()
162
    {
163
        return $this->getIfSet('id');
164
    }
165
166
    /**
167
     * Get account address.
168
     *
169
     * @return \stdClass Account's address.
170
     */
171
    public function getAddress()
172
    {
173
        return $this->getIfSet('address', $this->data->person);
174
    }
175
176
    /**
177
     * Get account fullname.
178
     *
179
     * @return string Account's full name.
180
     */
181
    public function getFullname()
182
    {
183
        return $this->getIfSet('name', $this->data->person).' '.$this->getIfSet('lastName', $this->data->person);
184
    }
185
186
    /**
187
     * Get birth date from account.
188
     *
189
     * @return \DateTime|null Date of birth of the credit card holder.
190
     */
191
    public function getBirthDate()
192
    {
193
        return $this->getIfSetDate('birthDate', $this->data->person);
194
    }
195
196
    /**
197
     * Get phone area code from account.
198
     *
199
     * @return int DDD telephone.
200
     */
201
    public function getPhoneAreaCode()
202
    {
203
        return $this->getIfSet('areaCode', $this->data->person->phone);
204
    }
205
206
    /**
207
     * Get phone country code from account.
208
     *
209
     * @return int Country code.
210
     */
211
    public function getPhoneCountryCode()
212
    {
213
        return $this->getIfSet('countryCode', $this->data->person->phone);
214
    }
215
216
    /**
217
     * Get phone number from account.
218
     *
219
     * @return int Telephone number.
220
     */
221
    public function getPhoneNumber()
222
    {
223
        return $this->getIfSet('number', $this->data->person->phone);
224
    }
225
226
    /**
227
     * Get tax document type from account.
228
     *
229
     * @return string Type of value: CPF and CNPJ
230
     */
231
    public function getTaxDocumentType()
232
    {
233
        return $this->getIfSet('type', $this->data->person->taxDocument);
234
    }
235
236
    /**
237
     * Get alternative phones.
238
     *
239
     * @return array
240
     */
241
    public function getAlternativePhones()
242
    {
243
        return $this->getIfSet('alternativePhones', $this->data->person);
244
    }
245
246
    /**
247
     * Get company data.
248
     *
249
     * @return array
250
     */
251
    public function getCompany()
252
    {
253
        return $this->getIfSet('company', $this->data);
254
    }
255
256
    /**
257
     * Get tax document number from account.
258
     *
259
     * @return string Document Number.
260
     */
261
    public function getTaxDocumentNumber()
262
    {
263
        return $this->getIfSet('number', $this->data->person->taxDocument);
264
    }
265
266
    /**
267
     * Get account type.
268
     *
269
     * @return string Document Number.
270
     */
271
    public function getType()
272
    {
273
        return $this->getIfSet('type', $this->data);
274
    }
275
276
    /**
277
     * Mount the seller structure from account.
278
     *
279
     * @param \stdClass $response
280
     *
281
     * @return \Moip\Resource\Account Account data
282
     */
283
    protected function populate(stdClass $response)
284
    {
285
        $account = clone $this;
286
        $account->data->email = new stdClass();
287
288
        $email = $this->getIfSet('email', $response);
289
290
        $account->data->email->address = $this->getIfSet('address', $email);
291
        $account->data->person = new stdClass();
292
293
        $person = $this->getIfSet('person', $response);
294
295
        $account->data->person->name = $this->getIfSet('name', $person);
296
        $account->data->person->lastName = $this->getIfSet('lastName', $person);
297
        $account->data->person->taxDocument = new stdClass();
298
299
        $taxDocument = $this->getIfSet('taxDocument', $person);
300
301
        $account->data->person->taxDocument->type = $this->getIfSet('type', $taxDocument);
302
        $account->data->person->taxDocument->number = $this->getIfSet('number', $taxDocument);
303
        $account->data->person->phone = new stdClass();
304
305
        $phone = $this->getIfSet('phone', $person);
306
307
        $account->data->person->phone->countryCode = $this->getIfSet('countryCode', $phone);
308
        $account->data->person->phone->areaCode = $this->getIfSet('areaCode', $phone);
309
        $account->data->person->phone->number = $this->getIfSet('number', $phone);
310
        $account->data->person->identityDocument = new stdClass();
311
312
        $identityDocument = $this->getIfSet('identityDocument', $person);
313
314
        $account->data->person->identityDocument->type = $this->getIfSet('type', $identityDocument);
315
        $account->data->person->identityDocument->number = $this->getIfSet('number', $identityDocument);
316
        $account->data->person->identityDocument->issuer = $this->getIfSet('issuer', $identityDocument);
317
        $account->data->person->identityDocument->issueDate = $this->getIfSet('issueDate', $identityDocument);
318
319
        $account->data->person->birthDate = $this->getIfSet('birthDate', $person);
320
        $account->data->person->address = $this->getIfSet('address', $person);
321
322
        $account->data->person->alternativePhones = $this->getIfSet('alternativePhones', $person);
323
324
        $account->data->company = $this->getIfSet('company', $response);
325
        $account->data->_links = $this->getIfSet('_links', $response);
326
        $account->data->type = $this->getIfSet('type', $response);
327
        $account->data->id = $this->getIfSet('id', $response);
328
329
        return $account;
330
    }
331
332
    /**
333
     * Set e-mail from account.
334
     *
335
     * @param string $email Email account.
336
     *
337
     * @return \Moip\Resource\Account
338
     */
339
    public function setEmail($email)
340
    {
341
        $this->data->email->address = $email;
342
343
        return $this;
344
    }
345
346
    /**
347
     * Set name from account.
348
     *
349
     * @param string $name Account's person name.
350
     *
351
     * @return \Moip\Resource\Account
352
     */
353
    public function setName($name)
354
    {
355
        $this->data->person->name = $name;
356
357
        return $this;
358
    }
359
360
    /**
361
     * Set name from account.
362
     *
363
     * @param string $lastname Account's person name.
364
     *
365
     * @return \Moip\Resource\Account
366
     */
367
    public function setLastName($lastname)
368
    {
369
        $this->data->person->lastName = $lastname;
370
371
        return $this;
372
    }
373
374
    /**
375
     * Set birth date from account.
376
     *
377
     * @param \DateTime|string $birthDate Date of birth of the credit card holder.
378
     *
379
     * @return \Moip\Resource\Account
380
     */
381 View Code Duplication
    public function setBirthDate($birthDate)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in 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...
382
    {
383
        if ($birthDate instanceof \DateTime) {
384
            $birthDate = $birthDate->format('Y-m-d');
385
        }
386
387
        $this->data->person->birthDate = $birthDate;
388
389
        return $this;
390
    }
391
392
    /**
393
     * Set tax document from account.
394
     *
395
     * @param string $number Document number.
396
     * @param string $type   Document type.
397
     *
398
     * @return \Moip\Resource\Account
399
     */
400 View Code Duplication
    public function setTaxDocument($number, $type = self::TAX_DOCUMENT)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in 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...
401
    {
402
        $this->data->person->taxDocument = new stdClass();
403
        $this->data->person->taxDocument->type = $type;
404
        $this->data->person->taxDocument->number = $number;
405
406
        return $this;
407
    }
408
409
    /**
410
     * Set phone from account.
411
     *
412
     * @param int $areaCode    DDD telephone.
413
     * @param int $number      Telephone number.
414
     * @param int $countryCode Country code.
415
     *
416
     * @return \Moip\Resource\Account
417
     */
418 View Code Duplication
    public function setPhone($areaCode, $number, $countryCode = 55)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in 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...
419
    {
420
        $this->data->person->phone = new stdClass();
421
        $this->data->person->phone->countryCode = $countryCode;
422
        $this->data->person->phone->areaCode = $areaCode;
423
        $this->data->person->phone->number = $number;
424
425
        return $this;
426
    }
427
428
    /**
429
     * Set identity document from account.
430
     *
431
     * @param string $number Número do documento.
432
     * @param string $issuer Emissor do documento.
433
     * @param $issueDate
434
     * @param string $type Tipo do documento. Valores possíveis: RG.
435
     *
436
     * @return Account
437
     */
438
    public function setIdentityDocument($number, $issuer, $issueDate, $type = 'RG')
439
    {
440
        $this->data->person->identityDocument = new stdClass();
441
        $this->data->person->identityDocument->type = $type;
442
        $this->data->person->identityDocument->number = $number;
443
        $this->data->person->identityDocument->issuer = $issuer;
444
        $this->data->person->identityDocument->issueDate = $issueDate;
445
446
        return $this;
447
    }
448
449
    /**
450
     * Set person nationality.
451
     *
452
     * @param string $nationality Abbreviation for nationality (3 max length).
453
     *
454
     * @return $this
455
     */
456
    public function setNationality($nationality = self::ADDRESS_COUNTRY)
457
    {
458
        $this->data->person->nationality = $nationality;
459
460
        return $this;
461
    }
462
463
    /**
464
     * Set person birth place.
465
     *
466
     * @param string $birthPlace Birth place (city).
467
     *
468
     * @return $this
469
     */
470
    public function setBirthPlace($birthPlace)
471
    {
472
        $this->data->person->birthPlace = $birthPlace;
473
474
        return $this;
475
    }
476
477
    /**
478
     * Set parents name.
479
     *
480
     * @param string $motherName Mother name.
481
     * @param string $fatherName Father name.
482
     *
483
     * @return $this
484
     */
485
    public function setParentsName($motherName, $fatherName)
486
    {
487
        $this->data->person->parentsName = new stdClass();
488
        $this->data->person->parentsName->mother = $motherName;
489
        $this->data->person->parentsName->father = $fatherName;
490
491
        return $this;
492
    }
493
494
    /**
495
     * Set site.
496
     *
497
     * @param string $site URL from site.
498
     *
499
     * @return $this
500
     */
501
    public function setSite($site)
502
    {
503
        $this->data->site = $site;
504
505
        return $this;
506
    }
507
508
    /**
509
     * Set transparent account.
510
     *
511
     * @param bool $transparentAccount Set true if you want create a transparent account.
512
     *
513
     * @return $this
514
     */
515
    public function setTransparentAccount($transparentAccount)
516
    {
517
        $this->data->transparentAccount = $transparentAccount;
518
519
        return $this;
520
    }
521
522
    /**
523
     * Set business segment.
524
     *
525
     * @param int $segmentId business segment id. Possible values available at: https://documentao-moip.readme.io/v2.0/reference#tabela-de-categorias-de-estabelecimento .
526
     *
527
     * @return $this
528
     */
529
    public function setBusinessSegment($segmentId)
530
    {
531
        $this->data->businessSegment->id = $segmentId;
532
533
        return $this;
534
    }
535
536
    /**
537
     * Set company name.
538
     *
539
     * @param string $name         Trading Name.
540
     * @param string $businessName Company Name.
541
     *
542
     * @return $this
543
     */
544
    public function setCompanyName($name, $businessName)
545
    {
546
        $this->initializeCompany();
547
        $this->data->company->name = $name;
548
        $this->data->company->businessName = $businessName;
549
550
        return $this;
551
    }
552
553
    /**
554
     * Initialize company node.
555
     */
556
    private function initializeCompany()
557
    {
558
        if (!isset($this->data->company)) {
559
            $this->data->company = new stdClass();
560
        }
561
    }
562
563
    /**
564
     * Set company opening date.
565
     *
566
     * @param \DateTime|string $openingDate .
567
     *
568
     * @return $this
569
     */
570
    public function setCompanyOpeningDate($openingDate)
571
    {
572
        if ($openingDate instanceof \DateTime) {
573
            $openingDate = $openingDate->format('Y-m-d');
574
        }
575
        $this->initializeCompany();
576
        $this->data->company->openingDate = $openingDate;
577
578
        return $this;
579
    }
580
581
    /**
582
     * Set company tax document.
583
     *
584
     * @param string $documentNumber .
585
     *
586
     * @return $this
587
     */
588 View Code Duplication
    public function setCompanyTaxDocument($documentNumber)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in 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...
589
    {
590
        $this->initializeCompany();
591
        $this->data->company->taxDocument = new stdClass();
592
        $this->data->company->taxDocument->type = self::COMPANY_TAX_DOCUMENT;
593
        $this->data->company->taxDocument->number = $documentNumber;
594
595
        return $this;
596
    }
597
598
    /**
599
     * Set company tax document.
600
     *
601
     * @param string $documentNumber .
0 ignored issues
show
Bug introduced by
There is no parameter named $documentNumber. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
602
     *
603
     * @return $this
604
     */
605
    public function setCompanyMainActivity($cnae, $description)
606
    {
607
        $this->initializeCompany();
608
        $this->data->company->mainActivity = new stdClass();
609
        $this->data->company->mainActivity->cnae = $cnae;
610
        $this->data->company->mainActivity->description = $description;
611
612
        return $this;
613
    }
614
615
    /**
616
     * Set address to company.
617
     *
618
     * @param string $street     Street address.
619
     * @param string $number     Number address.
620
     * @param string $district   Neighborhood address.
621
     * @param string $city       City address.
622
     * @param string $state      State address.
623
     * @param string $zip        The zip code billing address.
624
     * @param string $complement Address complement.
625
     * @param string $country    Country ISO-alpha3 format, BRA example.
626
     *
627
     * @return $this
628
     */
629 View Code Duplication
    public function setCompanyAddress($street, $number, $district, $city, $state, $zip, $complement = null, $country = self::ADDRESS_COUNTRY)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in 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...
630
    {
631
        $address = new stdClass();
632
        $address->street = $street;
633
        $address->streetNumber = $number;
634
        $address->complement = $complement;
635
        $address->district = $district;
636
        $address->city = $city;
637
        $address->state = $state;
638
        $address->country = $country;
639
        $address->zipCode = $zip;
640
641
        $this->initializeCompany();
642
        $this->data->company->address = $address;
643
644
        return $this;
645
    }
646
647
    /**
648
     * Set company phone.
649
     *
650
     * @param int $areaCode    DDD telephone.
651
     * @param int $number      Telephone number.
652
     * @param int $countryCode Country code.
653
     *
654
     * @return \Moip\Resource\Account
655
     */
656
    public function setCompanyPhone($areaCode, $number, $countryCode = 55)
657
    {
658
        $this->initializeCompany();
659
        $this->data->company->phone = new stdClass();
660
        $this->data->company->phone->countryCode = $countryCode;
661
        $this->data->company->phone->areaCode = $areaCode;
662
        $this->data->company->phone->number = $number;
663
664
        return $this;
665
    }
666
667
    /**
668
     * Set account type. Possible values: CONSUMER, MERCHANT.
669
     *
670
     * @param string $type
671
     *
672
     * @return \Moip\Resource\Account
673
     */
674
    public function setType($type)
675
    {
676
        $this->data->type = $type;
677
678
        return $this;
679
    }
680
}
681