Completed
Pull Request — master (#248)
by
unknown
02:33
created

Account::getPasswordLink()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

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