Completed
Pull Request — master (#237)
by
unknown
02:13
created

Customer::getPhoneCountryCode()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

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