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

Customer::setTaxDocumentPJ()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 5

Duplication

Lines 8
Ratio 100 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 1
eloc 5
c 2
b 0
f 0
nc 1
nop 2
dl 8
loc 8
rs 9.4285
1
<?php
2
3
namespace Moip\Resource;
4
5
use stdClass;
6
use UnexpectedValueException;
7
use ArrayIterator;
8
use Moip\Helper\Filters;
9
use Moip\Helper\Pagination;
10
11
/**
12
 * Class Customer.
13
 */
14
class Customer extends MoipResource
15
{
16
    /**
17
     * Path customers API.
18
     *
19
     * @const string
20
     */
21
    const PATH = 'customers';
22
23
    /**
24
     * Address Type.
25
     *
26
     * @const string
27
     */
28
    const ADDRESS_BILLING = 'BILLING';
29
30
    /**
31
     * Address Type.
32
     *
33
     * @const string
34
     */
35
    const ADDRESS_SHIPPING = 'SHIPPING';
36
37
    /**
38
     * Standard country .
39
     *
40
     * @const string
41
     */
42
    const ADDRESS_COUNTRY = 'BRA';
43
44
    /**
45
     *  Document type PF.
46
     *
47
     * @const string
48
     */
49
    const TAX_DOCUMENT = 'CPF';
50
51
    /**
52
     * Initialize a new instance.
53
     */
54
55
     /**
56
      *  Document type PJ.
57
      *
58
      * @const string
59
      */
60
     const TAX_DOCUMENT_PJ = 'CNPJ';
61
62
     /**
63
      * Initialize a new instance.
64
      */
65
66
    public function initialize()
67
    {
68
        $this->data = new stdClass();
69
    }
70
71
    /**
72
     * @return \Moip\Resource\CustomerCreditCard
73
     */
74
    public function creditCard()
75
    {
76
        return new CustomerCreditCard($this->moip);
77
    }
78
79
    /**
80
     * Add a new address to the customer.
81
     *
82
     * @param string $type       Address type: SHIPPING or BILLING.
83
     * @param string $street     Street address.
84
     * @param string $number     Number address.
85
     * @param string $district   Neighborhood address.
86
     * @param string $city       City address.
87
     * @param string $state      State address.
88
     * @param string $zip        The zip code billing address.
89
     * @param string $complement Address complement.
90
     * @param string $country    Country ISO-alpha3 format, BRA example.
91
     *
92
     * @return $this
93
     */
94
    public function addAddress($type, $street, $number, $district, $city, $state, $zip, $complement = null, $country = self::ADDRESS_COUNTRY)
95
    {
96
        $address = new stdClass();
97
        $address->street = $street;
98
        $address->streetNumber = $number;
99
        $address->complement = $complement;
100
        $address->district = $district;
101
        $address->city = $city;
102
        $address->state = $state;
103
        $address->country = $country;
104
        $address->zipCode = $zip;
105
106
        switch ($type) {
107
            case self::ADDRESS_BILLING:
108
                $this->data->billingAddress = $address;
109
                break;
110
            case self::ADDRESS_SHIPPING:
111
                $this->data->shippingAddress = $address;
112
                break;
113
            default:
114
                throw new UnexpectedValueException(sprintf('%s não é um tipo de endereço válido', $type));
115
        }
116
117
        return $this;
118
    }
119
120
    /**
121
     * Create a new customer.
122
     *
123
     * @return \stdClass
124
     */
125
    public function create()
126
    {
127
        return $this->createResource(sprintf('/%s/%s/', MoipResource::VERSION, self::PATH));
128
    }
129
130
    /**
131
     * Find a customer.
132
     *
133
     * @param string $moip_id
134
     *
135
     * @return \Moip\Resource\Customer|stdClass
136
     */
137
    public function get($moip_id)
138
    {
139
        return $this->getByPath(sprintf('/%s/%s/%s', MoipResource::VERSION, self::PATH, $moip_id));
140
    }
141
142
    /**
143
     * Create a new Customers list instance.
144
     *
145
     * @return \Moip\Resource\CustomerList
146
     */
147
    public function getList(Pagination $pagination = null, Filters $filters = null, $qParam = '')
148
    {
149
        $customerList = new CustomerList($this->moip);
150
151
        return $customerList->get($pagination, $filters, $qParam);
152
    }
153
154
    /**
155
     * Get customer id.
156
     *
157
     * @return string The buyer id.
158
     */
159
    public function getId()
160
    {
161
        return $this->getIfSet('id');
162
    }
163
164
    /**
165
     * Get customer address.
166
     *
167
     * @return \stdClass Customer's address.
168
     */
169
    public function getBillingAddress()
170
    {
171
        return $this->getIfSet('billingAddress');
172
    }
173
174
    /**
175
     * Get customer address.
176
     *
177
     * @return \stdClass Customer's address.
178
     */
179
    public function getShippingAddress()
180
    {
181
        return $this->getIfSet('shippingAddress');
182
    }
183
184
    /**
185
     * Get customer fullname.
186
     *
187
     * @return string Customer's full name.
188
     */
189
    public function getFullname()
190
    {
191
        return $this->getIfSet('fullname');
192
    }
193
194
    /**
195
     * Get funding instrument from customer.
196
     *
197
     * @return \stdClass Structure that is the means of payment.
198
     */
199
    public function getFundingInstrument()
200
    {
201
        return $this->getIfSet('fundingInstrument');
202
    }
203
204
    /**
205
     * Get birth date from customer.
206
     *
207
     * @return \DateTime|null Date of birth of the credit card holder.
208
     */
209
    public function getBirthDate()
210
    {
211
        return $this->getIfSetDate('birthDate');
212
    }
213
214
    /**
215
     * Get phone area code from customer.
216
     *
217
     * @return int DDD telephone.
218
     */
219
    public function getPhoneAreaCode()
220
    {
221
        return $this->getIfSet('areaCode', $this->data->phone);
222
    }
223
224
    /**
225
     * Get phone country code from customer.
226
     *
227
     * @return int Country code.
228
     */
229
    public function getPhoneCountryCode()
230
    {
231
        return $this->getIfSet('countryCode', $this->data->phone);
232
    }
233
234
    /**
235
     * Get phone number from customer.
236
     *
237
     * @return int Telephone number.
238
     */
239
    public function getPhoneNumber()
240
    {
241
        return $this->getIfSet('number', $this->data->phone);
242
    }
243
244
    /**
245
     * Get tax document type from customer.
246
     *
247
     * @return string Type of value: CPF and CNPJ
248
     */
249
    public function getTaxDocumentType()
250
    {
251
        return $this->getIfSet('type', $this->data->taxDocument);
252
    }
253
254
    /**
255
     * Get tax document number from customer.
256
     *
257
     * @return string Document Number.
258
     */
259
    public function getTaxDocumentNumber()
260
    {
261
        return $this->getIfSet('number', $this->data->taxDocument);
262
    }
263
264
    /**
265
     * Mount the buyer structure from customer.
266
     *
267
     * @param \stdClass $response
268
     *
269
     * @return Customer Customer information.
270
     */
271
    protected function populate(stdClass $response)
272
    {
273
        $customer = clone $this;
274
        $customer->data = new stdClass();
275
        $customer->data->id = $this->getIfSet('id', $response);
276
        $customer->data->ownId = $this->getIfSet('ownId', $response);
277
        $customer->data->fullname = $this->getIfSet('fullname', $response);
278
        $customer->data->email = $this->getIfSet('email', $response);
279
        $customer->data->phone = new stdClass();
280
281
        $phone = $this->getIfSet('phone', $response);
282
283
        $customer->data->phone->countryCode = $this->getIfSet('countryCode', $phone);
284
        $customer->data->phone->areaCode = $this->getIfSet('areaCode', $phone);
285
        $customer->data->phone->number = $this->getIfSet('number', $phone);
286
        $customer->data->birthDate = $this->getIfSet('birthDate', $response);
287
        $customer->data->taxDocument = new stdClass();
288
        $customer->data->taxDocument->type = $this->getIfSet('type', $this->getIfSet('taxDocument', $response));
289
        $customer->data->taxDocument->number = $this->getIfSet('number', $this->getIfSet('taxDocument', $response));
290
        $customer->data->addresses = [];
291
        $customer->data->shippingAddress = $this->getIfSet('shippingAddress', $response);
292
        $customer->data->billingAddress = $this->getIfSet('billingAddress', $response);
293
        $customer->data->fundingInstrument = $this->getIfSet('fundingInstrument', $response);
294
295
        $customer->data->_links = $this->getIfSet('_links', $response);
296
297
        return $customer;
298
    }
299
300
    /**
301
     * Set Own id from customer.
302
     *
303
     * @param string $ownId Customer's own id. external reference.
304
     *
305
     * @return $this
306
     */
307
    public function setOwnId($ownId)
308
    {
309
        $this->data->ownId = $ownId;
310
311
        return $this;
312
    }
313
314
    /**
315
     * Set fullname from customer.
316
     *
317
     * @param string $fullname Customer's full name.
318
     *
319
     * @return $this
320
     */
321
    public function setFullname($fullname)
322
    {
323
        $this->data->fullname = $fullname;
324
325
        return $this;
326
    }
327
328
    /**
329
     * Set e-mail from customer.
330
     *
331
     * @param string $email Email customer.
332
     *
333
     * @return $this
334
     */
335
    public function setEmail($email)
336
    {
337
        $this->data->email = $email;
338
339
        return $this;
340
    }
341
342
    /**
343
     * Set credit card from customer.
344
     *
345
     * @param int                          $expirationMonth Card expiration month.
346
     * @param int                          $expirationYear  Year card expiration.
347
     * @param int                          $number          Card number.
348
     * @param int                          $cvc             Card Security Code.
349
     * @param \Moip\Resource\Customer|null $holder          Cardholder.
350
     *
351
     * @return $this
352
     */
353
    public function setCreditCard($expirationMonth, $expirationYear, $number, $cvc, Holder $holder = null)
354
    {
355
        if ($holder === null) {
356
            $holder = $this;
357
        }
358
        $birthdate = $holder->getBirthDate();
359
        if ($birthdate instanceof \DateTime) {
360
            $birthdate = $birthdate->format('Y-m-d');
361
        }
362
363
        $this->data->fundingInstrument = new stdClass();
364
        $this->data->fundingInstrument->method = Payment::METHOD_CREDIT_CARD;
365
        $this->data->fundingInstrument->creditCard = new stdClass();
366
        $this->data->fundingInstrument->creditCard->expirationMonth = $expirationMonth;
367
        $this->data->fundingInstrument->creditCard->expirationYear = $expirationYear;
368
        $this->data->fundingInstrument->creditCard->number = $number;
369
        $this->data->fundingInstrument->creditCard->cvc = $cvc;
370
        $this->data->fundingInstrument->creditCard->holder = new stdClass();
371
        $this->data->fundingInstrument->creditCard->holder->fullname = $holder->getFullname();
372
        $this->data->fundingInstrument->creditCard->holder->birthdate = $birthdate;
373
        $this->data->fundingInstrument->creditCard->holder->taxDocument = new stdClass();
374
        $this->data->fundingInstrument->creditCard->holder->taxDocument->type = $holder->getTaxDocumentType();
375
        $this->data->fundingInstrument->creditCard->holder->taxDocument->number = $holder->getTaxDocumentNumber();
376
        $this->data->fundingInstrument->creditCard->holder->phone = new stdClass();
377
        $this->data->fundingInstrument->creditCard->holder->phone->countryCode = $holder->getPhoneCountryCode();
378
        $this->data->fundingInstrument->creditCard->holder->phone->areaCode = $holder->getPhoneAreaCode();
379
        $this->data->fundingInstrument->creditCard->holder->phone->number = $holder->getPhoneNumber();
380
381
        return $this;
382
    }
383
384
    /**
385
     * Set birth date from customer.
386
     *
387
     * @param \DateTime|string $birthDate Date of birth of the credit card holder.
388
     *
389
     * @return $this
390
     */
391 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...
392
    {
393
        if ($birthDate instanceof \DateTime) {
394
            $birthDate = $birthDate->format('Y-m-d');
395
        }
396
397
        $this->data->birthDate = $birthDate;
398
399
        return $this;
400
    }
401
402
    /**
403
     * Set tax document (CPF) from customer.
404
     *
405
     * @param string $number Document number.
406
     * @param string $type   Document type.
407
     *
408
     * @return $this
409
     */
410 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...
411
    {
412
        $this->data->taxDocument = new stdClass();
413
        $this->data->taxDocument->type = $type;
414
        $this->data->taxDocument->number = $number;
415
416
        return $this;
417
    }
418
419
    /**
420
     * Set tax document (CNPJ) from customer.
421
     *
422
     * @param string $number Document number.
423
     * @param string $type   Document type.
424
     *
425
     * @return $this
426
     */
427 View Code Duplication
    public function setTaxDocumentPJ($number, $type = self::TAX_DOCUMENT_PJ)
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...
428
    {
429
        $this->data->taxDocument = new stdClass();
430
        $this->data->taxDocument->type = $type;
431
        $this->data->taxDocument->number = $number;
432
433
        return $this;
434
    }
435
436
    /**
437
     * Set phone from customer.
438
     *
439
     * @param int $areaCode    DDD telephone.
440
     * @param int $number      Telephone number.
441
     * @param int $countryCode Country code.
442
     *
443
     * @return $this
444
     */
445 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...
446
    {
447
        $this->data->phone = new stdClass();
448
        $this->data->phone->countryCode = $countryCode;
449
        $this->data->phone->areaCode = $areaCode;
450
        $this->data->phone->number = $number;
451
452
        return $this;
453
    }
454
}
455