Completed
Pull Request — master (#252)
by
unknown
04:45
created

Account::getPasswordLink()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 0
dl 0
loc 4
rs 10
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 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 account type.
358
     *
359
     * @return string Document Number.
360
     */
361
    public function getType()
362
    {
363
        return $this->getIfSet('type', $this->data);
364
    }
365
366
    /**
367
     * Get transparent account (true/false).
368
     *
369
     * @return bool
370
     */
371
    public function getTransparentAccount()
372
    {
373
        return $this->getIfSet('transparentAccount', $this->data);
374
    }
375
376
    /**
377
     * Get account created at.
378
     *
379
     * @return string
380
     */
381
    public function getCreatedAt()
382
    {
383
        return $this->getIfSet('createdAt', $this->data);
384
    }
385
386
    /**
387
     * Get link to set the password of created account.
388
     * 
389
     * @return string
390
     */
391
    public function getPasswordLink()
392
    {
393
        return $this->getIfSet('href', $this->data->_links->setPassword);
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 = new stdClass();
449
        
450
        $_links = $this->getIfSet('_links', $response);
451
        $account->data->_links->setPassword = new stdClass();
452
        
453
        $setPassword = $this->getIfSet('setPassword', $_links);
454
        $account->data->_links->setPassword->href = $this->getIfSet('href', $setPassword);
455
456
        $account->data->type = $this->getIfSet('type', $response);
457
        $account->data->id = $this->getIfSet('id', $response);
458
        $account->data->accessToken = $this->getIfSet('accessToken', $response);
459
        $account->data->channelId = $this->getIfSet('channelId', $response);
460
        $account->data->transparentAccount = $this->getIfSet('transparentAccount', $response);
461
        $account->data->createdAt = $this->getIfSet('createdAt', $response);
462
463
        return $account;
464
    }
465
466
    /**
467
     * Set e-mail from account.
468
     *
469
     * @param string $email Email account.
470
     *
471
     * @return \Moip\Resource\Account
472
     */
473
    public function setEmail($email)
474
    {
475
        $this->data->email->address = $email;
476
477
        return $this;
478
    }
479
480
    /**
481
     * Set name from account.
482
     *
483
     * @param string $name Account's person name.
484
     *
485
     * @return \Moip\Resource\Account
486
     */
487
    public function setName($name)
488
    {
489
        $this->data->person->name = $name;
490
491
        return $this;
492
    }
493
494
    /**
495
     * Set name from account.
496
     *
497
     * @param string $lastname Account's person name.
498
     *
499
     * @return \Moip\Resource\Account
500
     */
501
    public function setLastName($lastname)
502
    {
503
        $this->data->person->lastName = $lastname;
504
505
        return $this;
506
    }
507
508
    /**
509
     * Set birth date from account.
510
     *
511
     * @param \DateTime|string $birthDate Date of birth of the credit card holder.
512
     *
513
     * @return \Moip\Resource\Account
514
     */
515 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...
516
    {
517
        if ($birthDate instanceof \DateTime) {
518
            $birthDate = $birthDate->format('Y-m-d');
519
        }
520
521
        $this->data->person->birthDate = $birthDate;
522
523
        return $this;
524
    }
525
526
    /**
527
     * Set tax document from account.
528
     *
529
     * @param string $number Document number.
530
     * @param string $type   Document type.
531
     *
532
     * @return \Moip\Resource\Account
533
     */
534 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...
535
    {
536
        $this->data->person->taxDocument = new stdClass();
537
        $this->data->person->taxDocument->type = $type;
538
        $this->data->person->taxDocument->number = $number;
539
540
        return $this;
541
    }
542
543
    /**
544
     * Set phone from account.
545
     *
546
     * @param int $areaCode    DDD telephone.
547
     * @param int $number      Telephone number.
548
     * @param int $countryCode Country code.
549
     *
550
     * @return \Moip\Resource\Account
551
     */
552 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...
553
    {
554
        $this->data->person->phone = new stdClass();
555
        $this->data->person->phone->countryCode = $countryCode;
556
        $this->data->person->phone->areaCode = $areaCode;
557
        $this->data->person->phone->number = $number;
558
559
        return $this;
560
    }
561
562
    /**
563
     * Set identity document from account.
564
     *
565
     * @param string $number Número do documento.
566
     * @param string $issuer Emissor do documento.
567
     * @param $issueDate
568
     * @param string $type Tipo do documento. Valores possíveis: RG.
569
     *
570
     * @return Account
571
     */
572
    public function setIdentityDocument($number, $issuer, $issueDate, $type = 'RG')
573
    {
574
        $this->data->person->identityDocument = new stdClass();
575
        $this->data->person->identityDocument->type = $type;
576
        $this->data->person->identityDocument->number = $number;
577
        $this->data->person->identityDocument->issuer = $issuer;
578
        $this->data->person->identityDocument->issueDate = $issueDate;
579
580
        return $this;
581
    }
582
583
    /**
584
     * Set person nationality.
585
     *
586
     * @param string $nationality Abbreviation for nationality (3 max length).
587
     *
588
     * @return $this
589
     */
590
    public function setNationality($nationality = self::ADDRESS_COUNTRY)
591
    {
592
        $this->data->person->nationality = $nationality;
593
594
        return $this;
595
    }
596
597
    /**
598
     * Set person birth place.
599
     *
600
     * @param string $birthPlace Birth place (city).
601
     *
602
     * @return $this
603
     */
604
    public function setBirthPlace($birthPlace)
605
    {
606
        $this->data->person->birthPlace = $birthPlace;
607
608
        return $this;
609
    }
610
611
    /**
612
     * Set parents name.
613
     *
614
     * @param string $motherName Mother name.
615
     * @param string $fatherName Father name.
616
     *
617
     * @return $this
618
     */
619
    public function setParentsName($motherName, $fatherName)
620
    {
621
        $this->data->person->parentsName = new stdClass();
622
        $this->data->person->parentsName->mother = $motherName;
623
        $this->data->person->parentsName->father = $fatherName;
624
625
        return $this;
626
    }
627
628
    /**
629
     * Set site.
630
     *
631
     * @param string $site URL from site.
632
     *
633
     * @return $this
634
     */
635
    public function setSite($site)
636
    {
637
        $this->data->site = $site;
638
639
        return $this;
640
    }
641
642
    /**
643
     * Set transparent account.
644
     *
645
     * @param bool $transparentAccount Set true if you want create a transparent account.
646
     *
647
     * @return $this
648
     */
649
    public function setTransparentAccount($transparentAccount)
650
    {
651
        $this->data->transparentAccount = $transparentAccount;
652
653
        return $this;
654
    }
655
656
    /**
657
     * Set business segment.
658
     *
659
     * @param int $segmentId business segment id. Possible values available at: https://documentao-moip.readme.io/v2.0/reference#tabela-de-categorias-de-estabelecimento .
660
     *
661
     * @return $this
662
     */
663
    public function setBusinessSegment($segmentId)
664
    {
665
        $this->data->businessSegment->id = $segmentId;
666
667
        return $this;
668
    }
669
670
    /**
671
     * Set company name.
672
     *
673
     * @param string $name         Trading Name.
674
     * @param string $businessName Company Name.
675
     *
676
     * @return $this
677
     */
678
    public function setCompanyName($name, $businessName)
679
    {
680
        $this->initializeCompany();
681
        $this->data->company->name = $name;
682
        $this->data->company->businessName = $businessName;
683
684
        return $this;
685
    }
686
687
    /**
688
     * Initialize company node.
689
     */
690
    private function initializeCompany()
691
    {
692
        if (!isset($this->data->company)) {
693
            $this->data->company = new stdClass();
694
        }
695
    }
696
697
    /**
698
     * Set company opening date.
699
     *
700
     * @param \DateTime|string $openingDate .
701
     *
702
     * @return $this
703
     */
704
    public function setCompanyOpeningDate($openingDate)
705
    {
706
        if ($openingDate instanceof \DateTime) {
707
            $openingDate = $openingDate->format('Y-m-d');
708
        }
709
        $this->initializeCompany();
710
        $this->data->company->openingDate = $openingDate;
711
712
        return $this;
713
    }
714
715
    /**
716
     * Set company tax document.
717
     *
718
     * @param string $documentNumber .
719
     *
720
     * @return $this
721
     */
722 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...
723
    {
724
        $this->initializeCompany();
725
        $this->data->company->taxDocument = new stdClass();
726
        $this->data->company->taxDocument->type = self::COMPANY_TAX_DOCUMENT;
727
        $this->data->company->taxDocument->number = $documentNumber;
728
729
        return $this;
730
    }
731
732
    /**
733
     * Set company tax document.
734
     *
735
     * @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...
736
     *
737
     * @return $this
738
     */
739
    public function setCompanyMainActivity($cnae, $description)
740
    {
741
        $this->initializeCompany();
742
        $this->data->company->mainActivity = new stdClass();
743
        $this->data->company->mainActivity->cnae = $cnae;
744
        $this->data->company->mainActivity->description = $description;
745
746
        return $this;
747
    }
748
749
    /**
750
     * Set address to company.
751
     *
752
     * @param string $street     Street address.
753
     * @param string $number     Number address.
754
     * @param string $district   Neighborhood address.
755
     * @param string $city       City address.
756
     * @param string $state      State address.
757
     * @param string $zip        The zip code billing address.
758
     * @param string $complement Address complement.
759
     * @param string $country    Country ISO-alpha3 format, BRA example.
760
     *
761
     * @return $this
762
     */
763 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...
764
    {
765
        $address = new stdClass();
766
        $address->street = $street;
767
        $address->streetNumber = $number;
768
        $address->complement = $complement;
769
        $address->district = $district;
770
        $address->city = $city;
771
        $address->state = $state;
772
        $address->country = $country;
773
        $address->zipCode = $zip;
774
775
        $this->initializeCompany();
776
        $this->data->company->address = $address;
777
778
        return $this;
779
    }
780
781
    /**
782
     * Set company phone.
783
     *
784
     * @param int $areaCode    DDD telephone.
785
     * @param int $number      Telephone number.
786
     * @param int $countryCode Country code.
787
     *
788
     * @return \Moip\Resource\Account
789
     */
790
    public function setCompanyPhone($areaCode, $number, $countryCode = 55)
791
    {
792
        $this->initializeCompany();
793
        $this->data->company->phone = new stdClass();
794
        $this->data->company->phone->countryCode = $countryCode;
795
        $this->data->company->phone->areaCode = $areaCode;
796
        $this->data->company->phone->number = $number;
797
798
        return $this;
799
    }
800
801
    /**
802
     * Set account type. Possible values: CONSUMER, MERCHANT.
803
     *
804
     * @param string $type
805
     *
806
     * @return \Moip\Resource\Account
807
     */
808
    public function setType($type)
809
    {
810
        $this->data->type = $type;
811
812
        return $this;
813
    }
814
}
815