Completed
Pull Request — master (#173)
by
unknown
01:49
created

Account::setTaxDocument()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 5

Duplication

Lines 8
Ratio 100 %

Importance

Changes 0
Metric Value
cc 1
eloc 5
nc 1
nop 2
dl 8
loc 8
rs 9.4285
c 0
b 0
f 0
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 stdClass
140
     */
141
    public function checkAccountExists($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
        return false;
153
    }
154
155
    /**
156
     * Get account id.
157
     *
158
     * @return string The buyer id.
159
     */
160
    public function getId()
161
    {
162
        return $this->getIfSet('id');
163
    }
164
165
    /**
166
     * Get account address.
167
     *
168
     * @return \stdClass Account's address.
169
     */
170
    public function getAddress()
171
    {
172
        return $this->getIfSet('address', $this->data->person);
173
    }
174
175
    /**
176
     * Get account fullname.
177
     *
178
     * @return string Account's full name.
179
     */
180
    public function getFullname()
181
    {
182
        return $this->getIfSet('name', $this->data->person).' '.$this->getIfSet('lastName', $this->data->person);
183
    }
184
185
    /**
186
     * Get birth date from account.
187
     *
188
     * @return \DateTime|null Date of birth of the credit card holder.
189
     */
190
    public function getBirthDate()
191
    {
192
        return $this->getIfSetDate('birthDate', $this->data->person);
193
    }
194
195
    /**
196
     * Get phone area code from account.
197
     *
198
     * @return int DDD telephone.
199
     */
200
    public function getPhoneAreaCode()
201
    {
202
        return $this->getIfSet('areaCode', $this->data->person->phone);
203
    }
204
205
    /**
206
     * Get phone country code from account.
207
     *
208
     * @return int Country code.
209
     */
210
    public function getPhoneCountryCode()
211
    {
212
        return $this->getIfSet('countryCode', $this->data->person->phone);
213
    }
214
215
    /**
216
     * Get phone number from account.
217
     *
218
     * @return int Telephone number.
219
     */
220
    public function getPhoneNumber()
221
    {
222
        return $this->getIfSet('number', $this->data->person->phone);
223
    }
224
225
    /**
226
     * Get tax document type from account.
227
     *
228
     * @return string Type of value: CPF and CNPJ
229
     */
230
    public function getTaxDocumentType()
231
    {
232
        return $this->getIfSet('type', $this->data->person->taxDocument);
233
    }
234
235
    /**
236
     * Get alternative phones.
237
     *
238
     * @return array
239
     */
240
    public function getAlternativePhones()
241
    {
242
        return $this->getIfSet('alternativePhones', $this->data->person);
243
    }
244
245
    /**
246
     * Get company data.
247
     *
248
     * @return array
249
     */
250
    public function getCompany()
251
    {
252
        return $this->getIfSet('company', $this->data);
253
    }
254
255
    /**
256
     * Get tax document number from account.
257
     *
258
     * @return string Document Number.
259
     */
260
    public function getTaxDocumentNumber()
261
    {
262
        return $this->getIfSet('number', $this->data->person->taxDocument);
263
    }
264
265
    /**
266
     * Get account type.
267
     *
268
     * @return string Document Number.
269
     */
270
    public function getType()
271
    {
272
        return $this->getIfSet('type', $this->data);
273
    }
274
275
    /**
276
     * Mount the seller structure from account.
277
     *
278
     * @param \stdClass $response
279
     *
280
     * @return \Moip\Resource\Account Account data
281
     */
282
    protected function populate(stdClass $response)
283
    {
284
        $account = clone $this;
285
        $account->data->email = new stdClass();
286
287
        $email = $this->getIfSet('email', $response);
288
289
        $account->data->email->address = $this->getIfSet('address', $email);
290
        $account->data->person = new stdClass();
291
292
        $person = $this->getIfSet('person', $response);
293
294
        $account->data->person->name = $this->getIfSet('name', $person);
295
        $account->data->person->lastName = $this->getIfSet('lastName', $person);
296
        $account->data->person->taxDocument = new stdClass();
297
298
        $taxDocument = $this->getIfSet('taxDocument', $person);
299
300
        $account->data->person->taxDocument->type = $this->getIfSet('type', $taxDocument);
301
        $account->data->person->taxDocument->number = $this->getIfSet('number', $taxDocument);
302
        $account->data->person->phone = new stdClass();
303
304
        $phone = $this->getIfSet('phone', $person);
305
306
        $account->data->person->phone->countryCode = $this->getIfSet('countryCode', $phone);
307
        $account->data->person->phone->areaCode = $this->getIfSet('areaCode', $phone);
308
        $account->data->person->phone->number = $this->getIfSet('number', $phone);
309
        $account->data->person->identityDocument = new stdClass();
310
311
        $identityDocument = $this->getIfSet('identityDocument', $person);
312
313
        $account->data->person->identityDocument->type = $this->getIfSet('type', $identityDocument);
314
        $account->data->person->identityDocument->number = $this->getIfSet('number', $identityDocument);
315
        $account->data->person->identityDocument->issuer = $this->getIfSet('issuer', $identityDocument);
316
        $account->data->person->identityDocument->issueDate = $this->getIfSet('issueDate', $identityDocument);
317
318
        $account->data->person->birthDate = $this->getIfSet('birthDate', $person);
319
        $account->data->person->address = $this->getIfSet('address', $person);
320
321
        $account->data->person->alternativePhones = $this->getIfSet('alternativePhones', $person);
322
323
        $account->data->company = $this->getIfSet('company', $response);
324
        $account->data->_links = $this->getIfSet('_links', $response);
325
        $account->data->type = $this->getIfSet('type', $response);
326
        $account->data->id = $this->getIfSet('id', $response);
327
328
        return $account;
329
    }
330
331
    /**
332
     * Set e-mail from account.
333
     *
334
     * @param string $email Email account.
335
     *
336
     * @return \Moip\Resource\Account
337
     */
338
    public function setEmail($email)
339
    {
340
        $this->data->email->address = $email;
341
342
        return $this;
343
    }
344
345
    /**
346
     * Set name from account.
347
     *
348
     * @param string $name Account's person name.
349
     *
350
     * @return \Moip\Resource\Account
351
     */
352
    public function setName($name)
353
    {
354
        $this->data->person->name = $name;
355
356
        return $this;
357
    }
358
359
    /**
360
     * Set name from account.
361
     *
362
     * @param string $lastname Account's person name.
363
     *
364
     * @return \Moip\Resource\Account
365
     */
366
    public function setLastName($lastname)
367
    {
368
        $this->data->person->lastName = $lastname;
369
370
        return $this;
371
    }
372
373
    /**
374
     * Set birth date from account.
375
     *
376
     * @param \DateTime|string $birthDate Date of birth of the credit card holder.
377
     *
378
     * @return \Moip\Resource\Account
379
     */
380 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...
381
    {
382
        if ($birthDate instanceof \DateTime) {
383
            $birthDate = $birthDate->format('Y-m-d');
384
        }
385
386
        $this->data->person->birthDate = $birthDate;
387
388
        return $this;
389
    }
390
391
    /**
392
     * Set tax document from account.
393
     *
394
     * @param string $number Document number.
395
     * @param string $type   Document type.
396
     *
397
     * @return \Moip\Resource\Account
398
     */
399 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...
400
    {
401
        $this->data->person->taxDocument = new stdClass();
402
        $this->data->person->taxDocument->type = $type;
403
        $this->data->person->taxDocument->number = $number;
404
405
        return $this;
406
    }
407
408
    /**
409
     * Set phone from account.
410
     *
411
     * @param int $areaCode    DDD telephone.
412
     * @param int $number      Telephone number.
413
     * @param int $countryCode Country code.
414
     *
415
     * @return \Moip\Resource\Account
416
     */
417 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...
418
    {
419
        $this->data->person->phone = new stdClass();
420
        $this->data->person->phone->countryCode = $countryCode;
421
        $this->data->person->phone->areaCode = $areaCode;
422
        $this->data->person->phone->number = $number;
423
424
        return $this;
425
    }
426
427
    /**
428
     * Set identity document from account.
429
     *
430
     * @param string $number Número do documento.
431
     * @param string $issuer Emissor do documento.
432
     * @param $issueDate
433
     * @param string $type Tipo do documento. Valores possíveis: RG.
434
     *
435
     * @return Account
436
     */
437
    public function setIdentityDocument($number, $issuer, $issueDate, $type = 'RG')
438
    {
439
        $this->data->person->identityDocument = new stdClass();
440
        $this->data->person->identityDocument->type = $type;
441
        $this->data->person->identityDocument->number = $number;
442
        $this->data->person->identityDocument->issuer = $issuer;
443
        $this->data->person->identityDocument->issueDate = $issueDate;
444
445
        return $this;
446
    }
447
448
    /**
449
     * Set person nationality.
450
     *
451
     * @param string $nationality Abbreviation for nationality (3 max length).
452
     *
453
     * @return $this
454
     */
455
    public function setNationality($nationality = self::ADDRESS_COUNTRY)
456
    {
457
        $this->data->person->nationality = $nationality;
458
459
        return $this;
460
    }
461
462
    /**
463
     * Set person birth place.
464
     *
465
     * @param string $birthPlace Birth place (city).
466
     *
467
     * @return $this
468
     */
469
    public function setBirthPlace($birthPlace)
470
    {
471
        $this->data->person->birthPlace = $birthPlace;
472
473
        return $this;
474
    }
475
476
    /**
477
     * Set parents name.
478
     *
479
     * @param string $motherName Mother name.
480
     * @param string $fatherName Father name.
481
     *
482
     * @return $this
483
     */
484
    public function setParentsName($motherName, $fatherName)
485
    {
486
        $this->data->person->parentsName = new stdClass();
487
        $this->data->person->parentsName->mother = $motherName;
488
        $this->data->person->parentsName->father = $fatherName;
489
490
        return $this;
491
    }
492
493
    /**
494
     * Set site.
495
     *
496
     * @param string $site URL from site.
497
     *
498
     * @return $this
499
     */
500
    public function setSite($site)
501
    {
502
        $this->data->site = $site;
503
504
        return $this;
505
    }
506
507
    /**
508
     * Set transparent account.
509
     *
510
     * @param bool $transparentAccount Set true if you want create a transparent account.
511
     *
512
     * @return $this
513
     */
514
    public function setTransparentAccount($transparentAccount)
515
    {
516
        $this->data->transparentAccount = $transparentAccount;
517
518
        return $this;
519
    }
520
521
    /**
522
     * Set business segment.
523
     *
524
     * @param int $segmentId business segment id. Possible values available at: https://documentao-moip.readme.io/v2.0/reference#tabela-de-categorias-de-estabelecimento .
525
     *
526
     * @return $this
527
     */
528
    public function setBusinessSegment($segmentId)
529
    {
530
        $this->data->businessSegment->id = $segmentId;
531
532
        return $this;
533
    }
534
535
    /**
536
     * Set company name.
537
     *
538
     * @param string $name         Trading Name.
539
     * @param string $businessName Company Name.
540
     *
541
     * @return $this
542
     */
543
    public function setCompanyName($name, $businessName)
544
    {
545
        $this->initializeCompany();
546
        $this->data->company->name = $name;
547
        $this->data->company->businessName = $businessName;
548
549
        return $this;
550
    }
551
552
    /**
553
     * Initialize company node.
554
     */
555
    private function initializeCompany()
556
    {
557
        if (!isset($this->data->company)) {
558
            $this->data->company = new stdClass();
559
        }
560
    }
561
562
    /**
563
     * Set company opening date.
564
     *
565
     * @param \DateTime|string $openingDate .
566
     *
567
     * @return $this
568
     */
569
    public function setCompanyOpeningDate($openingDate)
570
    {
571
        if ($openingDate instanceof \DateTime) {
572
            $openingDate = $openingDate->format('Y-m-d');
573
        }
574
        $this->initializeCompany();
575
        $this->data->company->openingDate = $openingDate;
576
577
        return $this;
578
    }
579
580
    /**
581
     * Set company tax document.
582
     *
583
     * @param string $documentNumber .
584
     *
585
     * @return $this
586
     */
587 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...
588
    {
589
        $this->initializeCompany();
590
        $this->data->company->taxDocument = new stdClass();
591
        $this->data->company->taxDocument->type = self::COMPANY_TAX_DOCUMENT;
592
        $this->data->company->taxDocument->number = $documentNumber;
593
594
        return $this;
595
    }
596
597
    /**
598
     * Set company tax document.
599
     *
600
     * @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...
601
     *
602
     * @return $this
603
     */
604
    public function setCompanyMainActivity($cnae, $description)
605
    {
606
        $this->initializeCompany();
607
        $this->data->company->mainActivity = new stdClass();
608
        $this->data->company->mainActivity->cnae = $cnae;
609
        $this->data->company->mainActivity->description = $description;
610
611
        return $this;
612
    }
613
614
    /**
615
     * Set address to company.
616
     *
617
     * @param string $street     Street address.
618
     * @param string $number     Number address.
619
     * @param string $district   Neighborhood address.
620
     * @param string $city       City address.
621
     * @param string $state      State address.
622
     * @param string $zip        The zip code billing address.
623
     * @param string $complement Address complement.
624
     * @param string $country    Country ISO-alpha3 format, BRA example.
625
     *
626
     * @return $this
627
     */
628 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...
629
    {
630
        $address = new stdClass();
631
        $address->street = $street;
632
        $address->streetNumber = $number;
633
        $address->complement = $complement;
634
        $address->district = $district;
635
        $address->city = $city;
636
        $address->state = $state;
637
        $address->country = $country;
638
        $address->zipCode = $zip;
639
640
        $this->initializeCompany();
641
        $this->data->company->address = $address;
642
643
        return $this;
644
    }
645
646
    /**
647
     * Set company phone.
648
     *
649
     * @param int $areaCode    DDD telephone.
650
     * @param int $number      Telephone number.
651
     * @param int $countryCode Country code.
652
     *
653
     * @return \Moip\Resource\Account
654
     */
655
    public function setCompanyPhone($areaCode, $number, $countryCode = 55)
656
    {
657
        $this->initializeCompany();
658
        $this->data->company->phone = new stdClass();
659
        $this->data->company->phone->countryCode = $countryCode;
660
        $this->data->company->phone->areaCode = $areaCode;
661
        $this->data->company->phone->number = $number;
662
663
        return $this;
664
    }
665
666
    /**
667
     * Set account type. Possible values: CONSUMER, MERCHANT.
668
     *
669
     * @param string $type
670
     *
671
     * @return \Moip\Resource\Account
672
     */
673
    public function setType($type)
674
    {
675
        $this->data->type = $type;
676
677
        return $this;
678
    }
679
}
680