Completed
Pull Request — master (#288)
by
unknown
05:42
created

Customer::getId()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

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