Completed
Pull Request — 23 (#431)
by Harald
10:27
created

Customer::_initialize()   F

Complexity

Conditions 17
Paths 256

Size

Total Lines 79
Code Lines 50

Duplication

Lines 31
Ratio 39.24 %

Importance

Changes 0
Metric Value
cc 17
eloc 50
nc 256
nop 1
dl 31
loc 79
rs 3.8585
c 0
b 0
f 0

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
namespace Braintree;
3
4
/**
5
 * Braintree Customer module
6
 * Creates and manages Customers
7
 *
8
 * <b>== More information ==</b>
9
 *
10
 * For more detailed information on Customers, see {@link http://www.braintreepayments.com/gateway/customer-api http://www.braintreepaymentsolutions.com/gateway/customer-api}
11
 *
12
 * @package    Braintree
13
 * @category   Resources
14
 * @copyright  2015 Braintree, a division of PayPal, Inc.
15
 *
16
 * @property-read array  $addresses
17
 * @property-read array  $paymentMethods
18
 * @property-read string $company
19
 * @property-read string $createdAt
20
 * @property-read array  $creditCards
21
 * @property-read array  $paypalAccounts
22
 * @property-read array  $applePayCards
23
 * @property-read array  $androidPayCards
24
 * @property-read array  $amexExpressCheckoutCards
25
 * @property-read array  $venmoAccounts
26
 * @property-read array  $coinbaseAccounts
27
 * @property-read array  $customFields custom fields passed with the request
28
 * @property-read string $email
29
 * @property-read string $fax
30
 * @property-read string $firstName
31
 * @property-read string $id
32
 * @property-read string $lastName
33
 * @property-read string $phone
34
 * @property-read string $updatedAt
35
 * @property-read string $website
36
 */
37
class Customer extends Base
38
{
39
    /**
40
     *
41
     * @return Customer[]
42
     */
43
    public static function all()
44
    {
45
        return Configuration::gateway()->customer()->all();
46
    }
47
48
    /**
49
     *
50
     * @param string $query
51
     * @param int[] $ids
52
     * @return Customer|Customer[]
53
     */
54
    public static function fetch($query, $ids)
55
    {
56
        return Configuration::gateway()->customer()->fetch($query, $ids);
57
    }
58
59
    /**
60
     *
61
     * @param array $attribs
62
     * @return Result\Successful|Result\Error
63
     */
64
    public static function create($attribs = [])
65
    {
66
        return Configuration::gateway()->customer()->create($attribs);
67
    }
68
69
    /**
70
     *
71
     * @param array $attribs
72
     * @return Customer
73
     */
74
    public static function createNoValidate($attribs = [])
75
    {
76
        return Configuration::gateway()->customer()->createNoValidate($attribs);
77
    }
78
79
    /**
80
     * @deprecated since version 2.3.0
81
     * @param string $queryString
82
     * @return Result\Successful
83
     */
84
    public static function createFromTransparentRedirect($queryString)
85
    {
86
        return Configuration::gateway()->customer()->createFromTransparentRedirect($queryString);
0 ignored issues
show
Bug Compatibility introduced by
The expression \Braintree\Configuration...Redirect($queryString); of type Braintree\Result\Successful|Braintree\Result\Error adds the type Braintree\Result\Error to the return on line 86 which is incompatible with the return type documented by Braintree\Customer::createFromTransparentRedirect of type Braintree\Result\Successful.
Loading history...
Deprecated Code introduced by
The method Braintree\CustomerGatewa...omTransparentRedirect() has been deprecated with message: since version 2.3.0

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
87
    }
88
89
    /**
90
     * @deprecated since version 2.3.0
91
     * @return string
92
     */
93
    public static function createCustomerUrl()
94
    {
95
        return Configuration::gateway()->customer()->createCustomerUrl();
0 ignored issues
show
Deprecated Code introduced by
The method Braintree\CustomerGateway::createCustomerUrl() has been deprecated with message: since version 2.3.0

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
96
    }
97
98
    /**
99
     *
100
     * @throws Exception\NotFound
101
     * @param string $id customer id
102
     * @return Customer
103
     */
104
    public static function find($id)
105
    {
106
        return Configuration::gateway()->customer()->find($id);
0 ignored issues
show
Bug Compatibility introduced by
The expression \Braintree\Configuration...>customer()->find($id); of type Braintree\Customer|boolean adds the type boolean to the return on line 106 which is incompatible with the return type documented by Braintree\Customer::find of type Braintree\Customer.
Loading history...
107
    }
108
109
    /**
110
     *
111
     * @param int $customerId
112
     * @param array $transactionAttribs
113
     * @return Result\Successful|Result\Error
114
     */
115
    public static function credit($customerId, $transactionAttribs)
116
    {
117
        return Configuration::gateway()->customer()->credit($customerId, $transactionAttribs);
118
    }
119
120
    /**
121
     *
122
     * @throws Exception\ValidationError
123
     * @param type $customerId
124
     * @param type $transactionAttribs
125
     * @return Transaction
126
     */
127
    public static function creditNoValidate($customerId, $transactionAttribs)
128
    {
129
        return Configuration::gateway()->customer()->creditNoValidate($customerId, $transactionAttribs);
130
    }
131
132
    /**
133
     *
134
     * @throws Exception on invalid id or non-200 http response code
135
     * @param int $customerId
136
     * @return Result\Successful
137
     */
138
    public static function delete($customerId)
139
    {
140
        return Configuration::gateway()->customer()->delete($customerId);
141
    }
142
143
    /**
144
     *
145
     * @param int $customerId
146
     * @param array $transactionAttribs
147
     * @return Transaction
148
     */
149
    public static function sale($customerId, $transactionAttribs)
150
    {
151
        return Configuration::gateway()->customer()->sale($customerId, $transactionAttribs);
152
    }
153
154
    /**
155
     *
156
     * @param int $customerId
157
     * @param array $transactionAttribs
158
     * @return Transaction
159
     */
160
    public static function saleNoValidate($customerId, $transactionAttribs)
161
    {
162
        return Configuration::gateway()->customer()->saleNoValidate($customerId, $transactionAttribs);
163
    }
164
165
    /**
166
     *
167
     * @throws InvalidArgumentException
168
     * @param string $query
169
     * @return ResourceCollection
170
     */
171
    public static function search($query)
172
    {
173
        return Configuration::gateway()->customer()->search($query);
174
    }
175
176
    /**
177
     *
178
     * @throws Exception\Unexpected
179
     * @param int $customerId
180
     * @param array $attributes
181
     * @return Result\Successful|Result\Error
182
     */
183
    public static function update($customerId, $attributes)
184
    {
185
        return Configuration::gateway()->customer()->update($customerId, $attributes);
186
    }
187
188
    /**
189
     *
190
     * @throws Exception\Unexpected
191
     * @param int $customerId
192
     * @param array $attributes
193
     * @return CustomerGateway
194
     */
195
    public static function updateNoValidate($customerId, $attributes)
196
    {
197
        return Configuration::gateway()->customer()->updateNoValidate($customerId, $attributes);
198
    }
199
200
    /**
201
     *
202
     * @deprecated since version 2.3.0
203
     * @return string
204
     */
205
    public static function updateCustomerUrl()
206
    {
207
        return Configuration::gateway()->customer()->updateCustomerUrl();
0 ignored issues
show
Deprecated Code introduced by
The method Braintree\CustomerGateway::updateCustomerUrl() has been deprecated with message: since version 2.3.0

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
208
    }
209
210
    /**
211
     *
212
     * @deprecated since version 2.3.0
213
     * @param string $queryString
214
     * @return Result\Successful|Result\Error
215
     */
216
    public static function updateFromTransparentRedirect($queryString)
217
    {
218
        return Configuration::gateway()->customer()->updateFromTransparentRedirect($queryString);
0 ignored issues
show
Deprecated Code introduced by
The method Braintree\CustomerGatewa...omTransparentRedirect() has been deprecated with message: since version 2.3.0

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
219
    }
220
221
    /* instance methods */
222
223
    /**
224
     * sets instance properties from an array of values
225
     *
226
     * @ignore
227
     * @access protected
228
     * @param array $customerAttribs array of customer data
229
     */
230
    protected function _initialize($customerAttribs)
231
    {
232
        $this->_attributes = $customerAttribs;
233
234
        $addressArray = [];
235 View Code Duplication
        if (isset($customerAttribs['addresses'])) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
236
237
            foreach ($customerAttribs['addresses'] AS $address) {
238
                $addressArray[] = Address::factory($address);
239
            }
240
        }
241
        $this->_set('addresses', $addressArray);
242
243
        $creditCardArray = [];
244 View Code Duplication
        if (isset($customerAttribs['creditCards'])) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
245
            foreach ($customerAttribs['creditCards'] AS $creditCard) {
246
                $creditCardArray[] = CreditCard::factory($creditCard);
247
            }
248
        }
249
        $this->_set('creditCards', $creditCardArray);
250
251
        $coinbaseAccountArray = [];
252 View Code Duplication
        if (isset($customerAttribs['coinbaseAccounts'])) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
253
            foreach ($customerAttribs['coinbaseAccounts'] AS $coinbaseAccount) {
254
                $coinbaseAccountArray[] = CoinbaseAccount::factory($coinbaseAccount);
255
            }
256
        }
257
        $this->_set('coinbaseAccounts', $coinbaseAccountArray);
258
259
        $paypalAccountArray = [];
260 View Code Duplication
        if (isset($customerAttribs['paypalAccounts'])) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
261
            foreach ($customerAttribs['paypalAccounts'] AS $paypalAccount) {
262
                $paypalAccountArray[] = PayPalAccount::factory($paypalAccount);
263
            }
264
        }
265
        $this->_set('paypalAccounts', $paypalAccountArray);
266
267
        $applePayCardArray = [];
268 View Code Duplication
        if (isset($customerAttribs['applePayCards'])) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
269
            foreach ($customerAttribs['applePayCards'] AS $applePayCard) {
270
                $applePayCardArray[] = ApplePayCard::factory($applePayCard);
271
            }
272
        }
273
        $this->_set('applePayCards', $applePayCardArray);
274
275
        $androidPayCardArray = [];
276 View Code Duplication
        if (isset($customerAttribs['androidPayCards'])) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
277
            foreach ($customerAttribs['androidPayCards'] AS $androidPayCard) {
278
                $androidPayCardArray[] = AndroidPayCard::factory($androidPayCard);
279
            }
280
        }
281
        $this->_set('androidPayCards', $androidPayCardArray);
282
283
        $amexExpressCheckoutCardArray = [];
284
        if (isset($customerAttribs['amexExpressCheckoutCards'])) {
285
            foreach ($customerAttribs['amexExpressCheckoutCards'] AS $amexExpressCheckoutCard) {
286
                $amexExpressCheckoutCardArray[] = AmexExpressCheckoutCard::factory($amexExpressCheckoutCard);
287
            }
288
        }
289
        $this->_set('amexExpressCheckoutCards', $amexExpressCheckoutCardArray);
290
291
        $venmoAccountArray = array();
292
        if (isset($customerAttribs['venmoAccounts'])) {
293
            foreach ($customerAttribs['venmoAccounts'] AS $venmoAccount) {
294
                $venmoAccountArray[] = VenmoAccount::factory($venmoAccount);
295
            }
296
        }
297
        $this->_set('venmoAccounts', $venmoAccountArray);
298
299
        $this->_set('paymentMethods', array_merge(
300
            $this->creditCards,
301
            $this->paypalAccounts,
302
            $this->applePayCards,
303
            $this->coinbaseAccounts,
304
            $this->androidPayCards,
305
            $this->amexExpressCheckoutCards,
306
            $this->venmoAccounts
307
        ));
308
    }
309
310
    /**
311
     * returns a string representation of the customer
312
     * @return string
313
     */
314
    public function  __toString()
315
    {
316
        return __CLASS__ . '[' .
317
                Util::attributesToString($this->_attributes) .']';
318
    }
319
320
    /**
321
     * returns false if comparing object is not a Customer,
322
     * or is a Customer with a different id
323
     *
324
     * @param object $otherCust customer to compare against
325
     * @return boolean
326
     */
327
    public function isEqual($otherCust)
328
    {
329
        return !($otherCust instanceof Customer) ? false : $this->id === $otherCust->id;
330
    }
331
332
    /**
333
     * returns an array containt all of the customer's payment methods
334
     *
335
     * @deprecated since version 3.1.0 - use the paymentMethods property directly
336
     *
337
     * @return array
338
     */
339
    public function paymentMethods()
340
    {
341
        return $this->paymentMethods;
342
    }
343
344
    /**
345
     * returns the customer's default payment method
346
     *
347
     * @return CreditCard|PayPalAccount
348
     */
349
    public function defaultPaymentMethod()
350
    {
351
        $defaultPaymentMethods = array_filter($this->paymentMethods, 'Braintree\Customer::_defaultPaymentMethodFilter');
352
        return current($defaultPaymentMethods);
353
    }
354
355
    public static function _defaultPaymentMethodFilter($paymentMethod)
356
    {
357
        return $paymentMethod->isDefault();
358
    }
359
360
    /* private class properties  */
361
362
    /**
363
     * @access protected
364
     * @var array registry of customer data
365
     */
366
    protected $_attributes = [
367
        'addresses'   => '',
368
        'company'     => '',
369
        'creditCards' => '',
370
        'email'       => '',
371
        'fax'         => '',
372
        'firstName'   => '',
373
        'id'          => '',
374
        'lastName'    => '',
375
        'phone'       => '',
376
        'createdAt'   => '',
377
        'updatedAt'   => '',
378
        'website'     => '',
379
        ];
380
381
    /**
382
     *  factory method: returns an instance of Customer
383
     *  to the requesting method, with populated properties
384
     *
385
     * @ignore
386
     * @param array $attributes
387
     * @return Customer
388
     */
389
    public static function factory($attributes)
390
    {
391
        $instance = new Customer();
392
        $instance->_initialize($attributes);
393
        return $instance;
394
    }
395
}
396
class_alias('Braintree\Customer', 'Braintree_Customer');
397