Completed
Push — master ( d68558...35dd5c )
by Anton
03:40
created

Builder   D

Complexity

Total Complexity 155

Size/Duplication

Total Lines 723
Duplicated Lines 13.97 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 1
Bugs 1 Features 0
Metric Value
wmc 155
c 1
b 1
f 0
lcom 1
cbo 1
dl 101
loc 723
rs 4.4444

16 Methods

Rating   Name   Duplication   Size   Complexity  
B confirmationEvent() 31 31 2
B loginEvent() 31 31 2
B registrationEvent() 0 38 2
A __construct() 0 12 3
A build() 0 11 1
A replace() 0 6 3
A addIdentity() 0 5 1
A addWebsiteData() 0 9 2
B addIpData() 18 18 7
D addBrowserData() 0 81 32
D addUserData() 0 89 34
C addCCTransactionData() 0 61 25
C addCardData() 0 31 11
D addBillingData() 0 46 17
B addProductData() 21 21 8
B addCustomField() 0 16 5

How to fix   Duplicated Code    Complexity   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

Complex Class

 Tip:   Before tackling complexity, make sure that you eliminate any duplication first. This often can reduce the size of classes significantly.

Complex classes like Builder often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.

Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.

While breaking up the class, it is a good idea to analyze how other classes use Builder, and based on these observations, apply Extract Interface, too.

1
<?php
2
3
namespace Covery\Client\Envelopes;
4
5
use Covery\Client\EnvelopeInterface;
6
use Covery\Client\IdentityNodeInterface;
7
8
class Builder
9
{
10
    /**
11
     * @var string
12
     */
13
    private $type;
14
    /**
15
     * @var string
16
     */
17
    private $sequenceId;
18
    /**
19
     * @var IdentityNodeInterface[]
20
     */
21
    private $identities = array();
22
    /**
23
     * @var array
24
     */
25
    private $data = array();
26
27
    /**
28
     * Returns builder for confirmation event
29
     *
30
     * @param string $sequenceId
31
     * @param string $userId
32
     * @param int|null $timestamp If null provided, takes current time
33
     * @param bool|null $isEmailConfirmed
34
     * @param bool|null $idPhoneConfirmed
35
     *
36
     * @return Builder
37
     */
38 View Code Duplication
    public static function confirmationEvent(
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...
39
        $sequenceId,
40
        $userId,
41
        $timestamp = null,
42
        $isEmailConfirmed = null,
43
        $idPhoneConfirmed = null
44
    ) {
45
        $builder = new self('confirmation', $sequenceId);
46
        if ($timestamp === null) {
47
            $timestamp = time();
48
        }
49
50
        return $builder->addUserData(
51
            null,
52
            $userId,
53
            null,
54
            null,
55
            null,
56
            null,
57
            null,
58
            null,
59
            null,
60
            null,
61
            null,
62
            null,
63
            $timestamp,
64
            $isEmailConfirmed,
65
            $idPhoneConfirmed,
66
            null
67
        );
68
    }
69
70
    /**
71
     * Returns builder for login event
72
     *
73
     * @param string $sequenceId
74
     * @param string $userId
75
     * @param int|null $timestamp
76
     * @param string|null $email
77
     * @param bool|null $failed
78
     *
79
     * @return Builder
80
     */
81 View Code Duplication
    public static function loginEvent(
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...
82
        $sequenceId,
83
        $userId,
84
        $timestamp = null,
85
        $email = null,
86
        $failed = null
87
    ) {
88
        $builder = new self('login', $sequenceId);
89
        if ($timestamp === null) {
90
            $timestamp = time();
91
        }
92
93
        return $builder->addUserData(
94
            $email,
95
            $userId,
96
            null,
97
            null,
98
            null,
99
            null,
100
            null,
101
            null,
102
            null,
103
            null,
104
            null,
105
            $timestamp,
106
            null,
107
            null,
108
            null,
109
            $failed
110
        );
111
    }
112
113
    /**
114
     * Returns builder for registration event
115
     *
116
     * @param string $sequenceId
117
     * @param string $userId
118
     * @param int|null $timestamp
119
     * @param string|null $email
120
     * @param string|null $userName
121
     * @param string|null $firstName
122
     * @param string|null $lastName
123
     * @param int|null $age
124
     * @param string|null $gender
125
     * @param string|null $phone
126
     * @param string|null $country
127
     * @param string|null $socialType
128
     *
129
     * @return Builder
130
     */
131
    public static function registrationEvent(
132
        $sequenceId,
133
        $userId,
134
        $timestamp = null,
135
        $email = null,
136
        $userName = null,
137
        $firstName = null,
138
        $lastName = null,
139
        $age = null,
140
        $gender = null,
141
        $phone = null,
142
        $country = null,
143
        $socialType = null
144
    ) {
145
        $builder = new self('registration', $sequenceId);
146
        if ($timestamp === null) {
147
            $timestamp = time();
148
        }
149
150
        return $builder->addUserData(
151
            $email,
152
            $userId,
153
            $phone,
154
            $userName,
155
            $firstName,
156
            $lastName,
157
            $gender,
158
            $age,
159
            $country,
160
            $socialType,
161
            $timestamp,
162
            null,
163
            null,
164
            null,
165
            null,
166
            null
167
        );
168
    }
169
170
    /**
171
     * Builder constructor.
172
     *
173
     * @param string $envelopeType
174
     * @param string $sequenceId
175
     */
176
    public function __construct($envelopeType, $sequenceId)
177
    {
178
        if (!is_string($envelopeType)) {
179
            throw new \InvalidArgumentException('Envelope type must be string');
180
        }
181
        if (!is_string($sequenceId)) {
182
            throw new \InvalidArgumentException('Sequence ID must be string');
183
        }
184
185
        $this->type = $envelopeType;
186
        $this->sequenceId = $sequenceId;
187
    }
188
189
    /**
190
     * Returns built envelope
191
     *
192
     * @return EnvelopeInterface
193
     */
194
    public function build()
195
    {
196
        return new Envelope(
197
            $this->type,
198
            $this->sequenceId,
199
            $this->identities,
200
            array_filter($this->data, function ($data) {
201
                return $data !== null;
202
            })
203
        );
204
    }
205
206
    /**
207
     * Replaces value in internal array if provided value not empty
208
     *
209
     * @param string $key
210
     * @param string|int|float|null $value
211
     */
212
    private function replace($key, $value)
213
    {
214
        if (!array_key_exists($key, $this->data) || !empty($value)) {
215
            $this->data[$key] = $value;
216
        }
217
    }
218
219
    /**
220
     * Adds identity node
221
     *
222
     * @param IdentityNodeInterface $identity
223
     *
224
     * @return $this
225
     */
226
    public function addIdentity(IdentityNodeInterface $identity)
227
    {
228
        $this->identities[] = $identity;
229
        return $this;
230
    }
231
232
    /**
233
     * Provides website URL to envelope
234
     *
235
     * @param string $websiteUrl
236
     *
237
     * @return $this
238
     */
239
    public function addWebsiteData($websiteUrl = '')
240
    {
241
        if (!is_string($websiteUrl)) {
242
            throw new \InvalidArgumentException('Website URL must be string');
243
        }
244
245
        $this->replace('website_url', $websiteUrl);
246
        return $this;
247
    }
248
249
    /**
250
     * Provides IP information for envelope
251
     *
252
     * @param string|null $ip User's IP address
253
     * @param string|null $realIp User's real IP address, if available
254
     * @param string|null $merchantIp Your website's IP address
255
     *
256
     * @return $this
257
     */
258 View Code Duplication
    public function addIpData($ip = '', $realIp = '', $merchantIp = '')
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...
259
    {
260
        if ($ip !== null && !is_string($ip)) {
261
            throw new \InvalidArgumentException('IP must be string');
262
        }
263
        if ($realIp !== null && !is_string($realIp)) {
264
            throw new \InvalidArgumentException('Real IP must be string');
265
        }
266
        if ($merchantIp !== null && !is_string($merchantIp)) {
267
            throw new \InvalidArgumentException('Merchant IP must be string');
268
        }
269
270
        $this->replace('ip', $ip);
271
        $this->replace('real_ip', $realIp);
272
        $this->replace('merchant_ip', $merchantIp);
273
274
        return $this;
275
    }
276
277
    /**
278
     * Provides browser information for envelope
279
     *
280
     * @param string|null $deviceFingerprint
281
     * @param string|null $userAgent
282
     * @param string|null $cpuClass
283
     * @param string|null $screenOrientation
284
     * @param string|null $screenResolution
285
     * @param string|null $os
286
     * @param int|null $timezoneOffset
287
     * @param string|null $languages
288
     * @param string|null $language
289
     * @param string|null $languageBrowser
290
     * @param string|null $languageUser
291
     * @param string|null $languageSystem
292
     * @param bool|null $cookieEnabled
293
     * @param bool|null $doNotTrack
294
     * @param bool|null $ajaxValidation
295
     *
296
     * @return $this
297
     */
298
    public function addBrowserData(
299
        $deviceFingerprint = '',
300
        $userAgent = '',
301
        $cpuClass = '',
302
        $screenOrientation = '',
303
        $screenResolution = '',
304
        $os = '',
305
        $timezoneOffset = null,
306
        $languages = '',
307
        $language = '',
308
        $languageBrowser = '',
309
        $languageUser = '',
310
        $languageSystem = '',
311
        $cookieEnabled = false,
312
        $doNotTrack = false,
313
        $ajaxValidation = false
314
    ) {
315
        if ($deviceFingerprint !== null && !is_string($deviceFingerprint)) {
316
            throw new \InvalidArgumentException('Device fingerprint must be string');
317
        }
318
        if ($userAgent !== null && !is_string($userAgent)) {
319
            throw new \InvalidArgumentException('User agent must be string');
320
        }
321
        if ($cpuClass !== null && !is_string($cpuClass)) {
322
            throw new \InvalidArgumentException('CPU class must be string');
323
        }
324
        if ($screenOrientation !== null && !is_string($screenOrientation)) {
325
            throw new \InvalidArgumentException('Screen orientation must be string');
326
        }
327
        if ($screenResolution !== null && !is_string($screenResolution)) {
328
            throw new \InvalidArgumentException('Screen resolution must be string');
329
        }
330
        if ($os !== null && !is_string($os)) {
331
            throw new \InvalidArgumentException('OS must be string');
332
        }
333
        if ($timezoneOffset !== null && $timezoneOffset !== null && !is_int($timezoneOffset)) {
334
            throw new \InvalidArgumentException('Timezone offset must be integer or null');
335
        }
336
        if ($languages !== null && !is_string($languages)) {
337
            throw new \InvalidArgumentException('Languages must be string');
338
        }
339
        if ($language !== null && !is_string($language)) {
340
            throw new \InvalidArgumentException('Language must be string');
341
        }
342
        if ($languageBrowser !== null && !is_string($languageBrowser)) {
343
            throw new \InvalidArgumentException('Browser language must be string');
344
        }
345
        if ($languageUser !== null && !is_string($languageUser)) {
346
            throw new \InvalidArgumentException('User language must be string');
347
        }
348
        if ($languageSystem !== null && !is_string($languageSystem)) {
349
            throw new \InvalidArgumentException('System language must be string');
350
        }
351
        if ($cookieEnabled !== null && !is_bool($cookieEnabled)) {
352
            throw new \InvalidArgumentException('Cookie enabled flag must be boolean');
353
        }
354
        if ($doNotTrack !== null && !is_bool($doNotTrack)) {
355
            throw new \InvalidArgumentException('DNT flag must be boolean');
356
        }
357
        if ($ajaxValidation !== null && !is_bool($ajaxValidation)) {
358
            throw new \InvalidArgumentException('AJAX validation flag must be boolean');
359
        }
360
361
        $this->replace('device_fingerprint', $deviceFingerprint);
362
        $this->replace('user_agent', $userAgent);
363
        $this->replace('cpu_class', $cpuClass);
364
        $this->replace('screen_orientation', $screenOrientation);
365
        $this->replace('screen_resolution', $screenResolution);
366
        $this->replace('os', $os);
367
        $this->replace('timezone_offset', $timezoneOffset);
368
        $this->replace('languages', $languages);
369
        $this->replace('language', $language);
370
        $this->replace('language_browser', $languageBrowser);
371
        $this->replace('language_system', $languageSystem);
372
        $this->replace('language_user', $languageUser);
373
        $this->replace('cookie_enabled', $cookieEnabled);
0 ignored issues
show
Bug introduced by
It seems like $cookieEnabled defined by parameter $cookieEnabled on line 311 can also be of type boolean; however, Covery\Client\Envelopes\Builder::replace() does only seem to accept string|integer|double|null, maybe add an additional type check?

This check looks at variables that have been passed in as parameters and are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
374
        $this->replace('do_not_track', $doNotTrack);
0 ignored issues
show
Bug introduced by
It seems like $doNotTrack defined by parameter $doNotTrack on line 312 can also be of type boolean; however, Covery\Client\Envelopes\Builder::replace() does only seem to accept string|integer|double|null, maybe add an additional type check?

This check looks at variables that have been passed in as parameters and are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
375
        $this->replace('ajax_validation', $ajaxValidation);
0 ignored issues
show
Bug introduced by
It seems like $ajaxValidation defined by parameter $ajaxValidation on line 313 can also be of type boolean; however, Covery\Client\Envelopes\Builder::replace() does only seem to accept string|integer|double|null, maybe add an additional type check?

This check looks at variables that have been passed in as parameters and are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
376
377
        return $this;
378
    }
379
380
    /**
381
     * Provides user data for envelope
382
     *
383
     * @param string|null $email
384
     * @param string|null $userId
385
     * @param string|null $phone
386
     * @param string|null $userName
387
     * @param string|null $firstName
388
     * @param string|null $lastName
389
     * @param string|null $gender
390
     * @param int|null $age
391
     * @param string|null $country
392
     * @param string|null $socialType
393
     * @param int|null $registrationTimestamp
394
     * @param int|null $loginTimeStamp
395
     * @param int|null $confirmationTimeStamp
396
     * @param bool|null $emailConfirmed
397
     * @param bool|null $phoneConfirmed
398
     * @param bool|null $loginFailed
399
     *
400
     * @return $this
401
     */
402
    public function addUserData(
403
        $email = '',
404
        $userId = '',
405
        $phone = '',
406
        $userName = '',
407
        $firstName = '',
408
        $lastName = '',
409
        $gender = '',
410
        $age = 0,
411
        $country = '',
412
        $socialType = '',
413
        $registrationTimestamp = 0,
414
        $loginTimeStamp = 0,
415
        $confirmationTimeStamp = 0,
416
        $emailConfirmed = false,
417
        $phoneConfirmed = false,
418
        $loginFailed = false
419
    ) {
420
        if ($email !== null && !is_string($email)) {
421
            throw new \InvalidArgumentException('Email must be string');
422
        }
423
        if (is_int($userId)) {
424
            $userId = strval($userId);
425
        }
426
        if ($userId !== null && !is_string($userId)) {
427
            throw new \InvalidArgumentException('UserId must be string or integer');
428
        }
429
        if ($phone !== null && !is_string($phone)) {
430
            throw new \InvalidArgumentException('Phone must be string');
431
        }
432
        if ($userName !== null && !is_string($userName)) {
433
            throw new \InvalidArgumentException('User name must be string');
434
        }
435
        if ($firstName !== null && !is_string($firstName)) {
436
            throw new \InvalidArgumentException('First name must be string');
437
        }
438
        if ($lastName !== null && !is_string($lastName)) {
439
            throw new \InvalidArgumentException('Last name must be string');
440
        }
441
        if ($gender !== null && !is_string($gender)) {
442
            throw new \InvalidArgumentException('Gender must be string');
443
        }
444
        if ($age !== null && !is_int($age)) {
445
            throw new \InvalidArgumentException('Age must be integer');
446
        }
447
        if ($country !== null && !is_string($country)) {
448
            throw new \InvalidArgumentException('Country must be string');
449
        }
450
        if ($socialType !== null && !is_string($socialType)) {
451
            throw new \InvalidArgumentException('Social type must be string');
452
        }
453
        if ($registrationTimestamp !== null && !is_int($registrationTimestamp)) {
454
            throw new \InvalidArgumentException('Registration timestamp must be integer');
455
        }
456
        if ($loginTimeStamp !== null && !is_int($loginTimeStamp)) {
457
            throw new \InvalidArgumentException('Login timestamp must be integer');
458
        }
459
        if ($confirmationTimeStamp !== null && !is_int($confirmationTimeStamp)) {
460
            throw new \InvalidArgumentException('Confirmation timestamp must be integer');
461
        }
462
        if ($emailConfirmed !== null && !is_bool($emailConfirmed)) {
463
            throw new \InvalidArgumentException('Email confirmed flag must be boolean');
464
        }
465
        if ($phoneConfirmed !== null && !is_bool($phoneConfirmed)) {
466
            throw new \InvalidArgumentException('Phone confirmed flag must be boolean');
467
        }
468
        if ($loginFailed !== null && !is_bool($loginFailed)) {
469
            throw new \InvalidArgumentException('Login failed flag must be boolean');
470
        }
471
472
        $this->replace('email', $email);
473
        $this->replace('user_merchant_id', $userId);
474
        $this->replace('phone', $phone);
475
        $this->replace('user_name', $userName);
476
        $this->replace('firstname', $firstName);
477
        $this->replace('lastname', $lastName);
478
        $this->replace('gender', $gender);
479
        $this->replace('age', $age);
480
        $this->replace('country', $country);
481
        $this->replace('social_type', $socialType);
482
        $this->replace('registration_timestamp', $registrationTimestamp);
483
        $this->replace('login_timestamp', $loginTimeStamp);
484
        $this->replace('confirmation_timestamp', $confirmationTimeStamp);
485
        $this->replace('email_confirmed', $emailConfirmed);
0 ignored issues
show
Bug introduced by
It seems like $emailConfirmed defined by parameter $emailConfirmed on line 416 can also be of type boolean; however, Covery\Client\Envelopes\Builder::replace() does only seem to accept string|integer|double|null, maybe add an additional type check?

This check looks at variables that have been passed in as parameters and are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
486
        $this->replace('phone_confirmed', $phoneConfirmed);
0 ignored issues
show
Bug introduced by
It seems like $phoneConfirmed defined by parameter $phoneConfirmed on line 417 can also be of type boolean; however, Covery\Client\Envelopes\Builder::replace() does only seem to accept string|integer|double|null, maybe add an additional type check?

This check looks at variables that have been passed in as parameters and are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
487
        $this->replace('login_failed', $loginFailed);
0 ignored issues
show
Bug introduced by
It seems like $loginFailed defined by parameter $loginFailed on line 418 can also be of type boolean; however, Covery\Client\Envelopes\Builder::replace() does only seem to accept string|integer|double|null, maybe add an additional type check?

This check looks at variables that have been passed in as parameters and are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
488
489
        return $this;
490
    }
491
492
    /**
493
     * Provides credit card data to envelope
494
     *
495
     * @param string|null $transactionId
496
     * @param string|null $transactionSource
497
     * @param string|null $transactionType
498
     * @param string|null $transactionMode
499
     * @param string|null $transactionTimestamp
500
     * @param string|null $transactionCurrency
501
     * @param string|null $transactionAmount
502
     * @param float|null $amountConverted
503
     * @param string|null $paymentMethod
504
     * @param string|null $paymentSystem
505
     * @param string|null $paymentMidName
506
     *
507
     * @return $this
508
     */
509
    public function addCCTransactionData(
510
        $transactionId,
511
        $transactionSource,
512
        $transactionType,
513
        $transactionMode,
514
        $transactionTimestamp,
515
        $transactionCurrency,
516
        $transactionAmount,
517
        $amountConverted = 0.0,
518
        $paymentMethod = '',
519
        $paymentSystem = '',
520
        $paymentMidName = ''
521
    ) {
522
        if ($transactionId !== null && !is_string($transactionId)) {
523
            throw new \InvalidArgumentException('Transaction ID must be string');
524
        }
525
        if ($transactionSource !== null && !is_string($transactionSource)) {
526
            throw new \InvalidArgumentException('Transaction source must be string');
527
        }
528
        if ($transactionType !== null && !is_string($transactionType)) {
529
            throw new \InvalidArgumentException('Transaction type must be string');
530
        }
531
        if ($transactionMode !== null && !is_string($transactionMode)) {
532
            throw new \InvalidArgumentException('Transaction mode must be string');
533
        }
534
        if ($transactionTimestamp !== null && !is_int($transactionTimestamp)) {
535
            throw new \InvalidArgumentException('Transaction timestamp must be integer');
536
        }
537
        if ($transactionAmount !== null && !is_int($transactionAmount) && !is_float($transactionAmount)) {
538
            throw new \InvalidArgumentException('Transaction amount must be float');
539
        }
540
        if ($transactionCurrency !== null && !is_string($transactionCurrency)) {
541
            throw new \InvalidArgumentException('Transaction currency must be string');
542
        }
543
        if ($paymentMethod !== null && !is_string($paymentMethod)) {
544
            throw new \InvalidArgumentException('Payment method must be string');
545
        }
546
        if ($paymentSystem !== null && !is_string($paymentSystem)) {
547
            throw new \InvalidArgumentException('Payment system must be string');
548
        }
549
        if ($paymentMidName !== null && !is_string($paymentMidName)) {
550
            throw new \InvalidArgumentException('Payment MID name must be string');
551
        }
552
        if ($amountConverted !== null && !is_int($amountConverted) && !is_float($amountConverted)) {
553
            throw new \InvalidArgumentException('Transaction amount converted must be float');
554
        }
555
556
        $this->replace('transaction_id', $transactionId);
557
        $this->replace('transaction_source', $transactionSource);
558
        $this->replace('transaction_type', $transactionType);
559
        $this->replace('transaction_mode', $transactionMode);
560
        $this->replace('transaction_timestamp', $transactionTimestamp);
561
        $this->replace('transaction_amount', floatval($transactionAmount));
562
        $this->replace('transaction_amount_converted', floatval($amountConverted));
563
        $this->replace('transaction_currency', $transactionCurrency);
564
        $this->replace('payment_method', $paymentMethod);
565
        $this->replace('payment_system', $paymentSystem);
566
        $this->replace('payment_mid', $paymentMidName);
567
568
        return $this;
569
    }
570
571
    /**
572
     * Provides Card data to envelope
573
     *
574
     * @param int|null $cardBin
575
     * @param string|null $cardLast4
576
     * @param int|null $expirationMonth
577
     * @param int|null $expirationYear
578
     * @param string|null $cardId
579
     *
580
     * @return $this
581
     */
582
    public function addCardData(
583
        $cardBin,
584
        $cardLast4,
585
        $expirationMonth,
586
        $expirationYear,
587
        $cardId = ''
588
    ) {
589
        if ($cardId !== null && !is_string($cardId)) {
590
            throw new \InvalidArgumentException('Card ID must be string');
591
        }
592
        if ($cardBin !== null && !is_int($cardBin)) {
593
            throw new \InvalidArgumentException('Card BIN must be integer');
594
        }
595
        if ($cardLast4 !== null && !is_string($cardLast4)) {
596
            throw new \InvalidArgumentException('Card last4  must be string');
597
        }
598
        if ($expirationMonth !== null && !is_int($expirationMonth)) {
599
            throw new \InvalidArgumentException('Expiration month must be integer');
600
        }
601
        if ($expirationYear !== null && !is_int($expirationYear)) {
602
            throw new \InvalidArgumentException('Expiration year must be integer');
603
        }
604
605
        $this->replace('card_id', $cardId);
606
        $this->replace('card_bin', $cardBin);
607
        $this->replace('card_last4', $cardLast4);
608
        $this->replace('expiration_month', $expirationMonth);
609
        $this->replace('expiration_year', $expirationYear);
610
611
        return $this;
612
    }
613
614
    /**
615
     * Provides billing data to envelope
616
     *
617
     * @param string|null $billingFirstName
618
     * @param string|null $billingLastName
619
     * @param string|null $billingFullName
620
     * @param string|null $billingCountry
621
     * @param string|null $billingState
622
     * @param string|null $billingCity
623
     * @param string|null $billingAddress
624
     * @param string|null $billingZip
625
     *
626
     * @return $this
627
     */
628
    public function addBillingData(
629
        $billingFirstName = '',
630
        $billingLastName = '',
631
        $billingFullName = '',
632
        $billingCountry = '',
633
        $billingState = '',
634
        $billingCity = '',
635
        $billingAddress = '',
636
        $billingZip = ''
637
    ) {
638
        if ($billingFirstName !== null && !is_string($billingFirstName)) {
639
            throw new \InvalidArgumentException('Billing first name must be string');
640
        }
641
        if ($billingLastName !== null && !is_string($billingLastName)) {
642
            throw new \InvalidArgumentException('Billing last name must be string');
643
        }
644
        if ($billingFullName !== null && !is_string($billingFullName)) {
645
            throw new \InvalidArgumentException('Billing full name must be string');
646
        }
647
        if ($billingCountry !== null && !is_string($billingCountry)) {
648
            throw new \InvalidArgumentException('Billing country name must be string');
649
        }
650
        if ($billingState !== null && !is_string($billingState)) {
651
            throw new \InvalidArgumentException('Billing state must be string');
652
        }
653
        if ($billingCity !== null && !is_string($billingCity)) {
654
            throw new \InvalidArgumentException('Billing city must be string');
655
        }
656
        if ($billingAddress !== null && !is_string($billingAddress)) {
657
            throw new \InvalidArgumentException('Billing address must be string');
658
        }
659
        if ($billingZip !== null && !is_string($billingZip)) {
660
            throw new \InvalidArgumentException('Billing zip must be string');
661
        }
662
663
        $this->replace('billing_firstname', $billingFirstName);
664
        $this->replace('billing_lastname', $billingLastName);
665
        $this->replace('billing_fullname', $billingFullName);
666
        $this->replace('billing_country', $billingCountry);
667
        $this->replace('billing_state', $billingState);
668
        $this->replace('billing_city', $billingCity);
669
        $this->replace('billing_address', $billingAddress);
670
        $this->replace('billing_zip', $billingZip);
671
672
        return $this;
673
    }
674
675
    /**
676
     * Provides product information to envelope
677
     *
678
     * @param float|null $productQuantity
679
     * @param string|null $productName
680
     * @param string|null $productDescription
681
     *
682
     * @return $this
683
     */
684 View Code Duplication
    public function addProductData(
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...
685
        $productQuantity = 0.0,
686
        $productName = '',
687
        $productDescription = ''
688
    ) {
689
        if ($productQuantity !== null && !is_int($productQuantity) && !is_float($productQuantity)) {
690
            throw new \InvalidArgumentException('Product quantity must be int or float');
691
        }
692
        if ($productName !== null && !is_string($productName)) {
693
            throw new \InvalidArgumentException('Product name must be string');
694
        }
695
        if ($productDescription !== null && !is_string($productDescription)) {
696
            throw new \InvalidArgumentException('Product description must be string');
697
        }
698
699
        $this->replace('product_quantity', $productQuantity);
700
        $this->replace('product_name', $productName);
701
        $this->replace('product_description', $productDescription);
702
703
        return $this;
704
    }
705
706
    /**
707
     * Adds custom data field to envelope
708
     *
709
     * @param string $name
710
     * @param string $value
711
     *
712
     * @return $this
713
     */
714
    public function addCustomField($name, $value)
715
    {
716
        if (!is_string($name)) {
717
            throw new \InvalidArgumentException('Custom field name must be string');
718
        }
719
        if (!is_string($value)) {
720
            throw new \InvalidArgumentException('Custom field value must be string');
721
        }
722
723
        if (strlen($name) < 8 || substr($name, 0, 7) !== 'custom_') {
724
            $name = 'custom_' . $name;
725
        }
726
727
        $this->replace($name, $value);
728
        return $this;
729
    }
730
}
731