Completed
Push — master ( 2b6000...f18e18 )
by Anton
02:02
created

Builder::addWebsiteData()   B

Complexity

Conditions 7
Paths 4

Size

Total Lines 17
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 17
rs 8.2222
cc 7
eloc 11
nc 4
nop 3
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
     * @param string|null $email
36
     * @param string|null $phone
37
     *
38
     * @return Builder
39
     */
40 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...
41
        $sequenceId,
42
        $userId,
43
        $timestamp = null,
44
        $isEmailConfirmed = null,
45
        $idPhoneConfirmed = null,
46
        $email = null,
47
        $phone = null
48
    ) {
49
        $builder = new self('confirmation', $sequenceId);
50
        if ($timestamp === null) {
51
            $timestamp = time();
52
        }
53
54
        return $builder->addUserData(
55
            $email,
56
            $userId,
57
            $phone,
58
            null,
59
            null,
60
            null,
61
            null,
62
            null,
63
            null,
64
            null,
65
            null,
66
            null,
67
            $timestamp,
68
            $isEmailConfirmed,
69
            $idPhoneConfirmed,
70
            null
71
        );
72
    }
73
74
    /**
75
     * Returns builder for login event
76
     *
77
     * @param string $sequenceId
78
     * @param string $userId
79
     * @param int|null $timestamp
80
     * @param string|null $email
81
     * @param bool|null $failed
82
     *
83
     * @return Builder
84
     */
85 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...
86
        $sequenceId,
87
        $userId,
88
        $timestamp = null,
89
        $email = null,
90
        $failed = null
91
    ) {
92
        $builder = new self('login', $sequenceId);
93
        if ($timestamp === null) {
94
            $timestamp = time();
95
        }
96
97
        return $builder->addUserData(
98
            $email,
99
            $userId,
100
            null,
101
            null,
102
            null,
103
            null,
104
            null,
105
            null,
106
            null,
107
            null,
108
            null,
109
            $timestamp,
110
            null,
111
            null,
112
            null,
113
            $failed
114
        );
115
    }
116
117
    /**
118
     * Returns builder for registration event
119
     *
120
     * @param string $sequenceId
121
     * @param string $userId
122
     * @param int|null $timestamp
123
     * @param string|null $email
124
     * @param string|null $userName
125
     * @param string|null $firstName
126
     * @param string|null $lastName
127
     * @param int|null $age
128
     * @param string|null $gender
129
     * @param string|null $phone
130
     * @param string|null $country
131
     * @param string|null $socialType
132
     * @param string|null $websiteUrl
133
     * @param string|null $trafficSource
134
     * @param string|null $affiliateId
135
     *
136
     * @return Builder
137
     */
138
    public static function registrationEvent(
139
        $sequenceId,
140
        $userId,
141
        $timestamp = null,
142
        $email = null,
143
        $userName = null,
144
        $firstName = null,
145
        $lastName = null,
146
        $age = null,
147
        $gender = null,
148
        $phone = null,
149
        $country = null,
150
        $socialType = null,
151
        $websiteUrl = null,
152
        $trafficSource = null,
153
        $affiliateId = null
154
    ) {
155
        $builder = new self('registration', $sequenceId);
156
        if ($timestamp === null) {
157
            $timestamp = time();
158
        }
159
160
        return $builder->addWebsiteData(
161
            $websiteUrl,
162
            $trafficSource,
163
            $affiliateId
164
        )->addUserData(
165
            $email,
166
            $userId,
167
            $phone,
168
            $userName,
169
            $firstName,
170
            $lastName,
171
            $gender,
172
            $age,
173
            $country,
174
            $socialType,
175
            $timestamp,
176
            null,
177
            null,
178
            null,
179
            null,
180
            null
181
        );
182
    }
183
184
    /**
185
     * Returns builder for payout request
186
     *
187
     * @param string $sequenceId
188
     * @param string $userId
189
     * @param string $payoutId
190
     * @param string $cardId
191
     * @param string $currency
192
     * @param int|float $amount
193
     * @param int|float $amountConverted
194
     * @param int|null $payoutTimestamp
195
     * @param string|null $method
196
     * @param string|null $system
197
     * @param string|null $mid
198
     * @param string|null $firstname
199
     * @param string|null $lastname
200
     * @param string|null $country
201
     * @param string|null $email
202
     * @param string|null $phone
203
     * @param int|null $cardBin
204
     * @param string|null $cardLast4
205
     * @param int|null $cardExpirationMonth
206
     * @param int|null $cardExpirationYear
207
     *
208
     * @return $this
209
     */
210
    public static function payoutEvent(
211
        $sequenceId,
212
        $userId,
213
        $payoutId,
214
        $cardId,
215
        $currency,
216
        $amount,
217
        $amountConverted,
218
        $payoutTimestamp = null,
219
        $method = null,
220
        $system = null,
221
        $mid = null,
222
        $firstname = null,
223
        $lastname = null,
224
        $country = null,
225
        $email = null,
226
        $phone = null,
227
        $cardBin = null,
228
        $cardLast4 = null,
229
        $cardExpirationMonth = null,
230
        $cardExpirationYear = null
231
    ) {
232
        $builder = new self('payout', $sequenceId);
233
        if ($payoutTimestamp === null) {
234
            $payoutTimestamp = time();
235
        }
236
        return $builder->addPayoutData(
237
            $payoutId,
238
            $payoutTimestamp,
239
            $cardId,
240
            $amount,
241
            $currency,
242
            $method,
243
            $system,
244
            $mid,
245
            $amountConverted,
246
            $cardBin,
247
            $cardLast4,
248
            $cardExpirationMonth,
249
            $cardExpirationYear
250
        )->addShortUserData($email, $userId, $phone, $firstname, $lastname, $country);
251
    }
252
253
    /**
254
     * Builder constructor.
255
     *
256
     * @param string $envelopeType
257
     * @param string $sequenceId
258
     */
259
    public function __construct($envelopeType, $sequenceId)
260
    {
261
        if (!is_string($envelopeType)) {
262
            throw new \InvalidArgumentException('Envelope type must be string');
263
        }
264
        if (!is_string($sequenceId)) {
265
            throw new \InvalidArgumentException('Sequence ID must be string');
266
        }
267
268
        $this->type = $envelopeType;
269
        $this->sequenceId = $sequenceId;
270
    }
271
272
    /**
273
     * Returns built envelope
274
     *
275
     * @return EnvelopeInterface
276
     */
277
    public function build()
278
    {
279
        return new Envelope(
280
            $this->type,
281
            $this->sequenceId,
282
            $this->identities,
283
            array_filter($this->data, function ($data) {
284
                return $data !== null;
285
            })
286
        );
287
    }
288
289
    /**
290
     * Replaces value in internal array if provided value not empty
291
     *
292
     * @param string $key
293
     * @param string|int|float|bool|null $value
294
     */
295
    private function replace($key, $value)
296
    {
297
        if (!array_key_exists($key, $this->data) || !empty($value)) {
298
            $this->data[$key] = $value;
299
        }
300
    }
301
302
    /**
303
     * Adds identity node
304
     *
305
     * @param IdentityNodeInterface $identity
306
     *
307
     * @return $this
308
     */
309
    public function addIdentity(IdentityNodeInterface $identity)
310
    {
311
        $this->identities[] = $identity;
312
        return $this;
313
    }
314
315
    /**
316
     * Provides website URL to envelope
317
     *
318
     * @param string|null $websiteUrl
319
     * @param string|null $traffic_source
320
     * @param string|null $affiliate_id
321
     *
322
     * @return $this
323
     */
324
    public function addWebsiteData($websiteUrl = null, $traffic_source = null, $affiliate_id = null)
325
    {
326
        if ($websiteUrl !== null && !is_string($websiteUrl)) {
327
            throw new \InvalidArgumentException('Website URL must be string');
328
        }
329
        if ($traffic_source !== null && !is_string($traffic_source)) {
330
            throw new \InvalidArgumentException('Traffic source must be string');
331
        }
332
        if ($affiliate_id !== null && !is_string($affiliate_id)) {
333
            throw new \InvalidArgumentException('Affiliate ID must be string');
334
        }
335
336
        $this->replace('website_url', $websiteUrl);
337
        $this->replace('traffic_source', $traffic_source);
338
        $this->replace('affiliate_id', $affiliate_id);
339
        return $this;
340
    }
341
342
    /**
343
     * Provides IP information for envelope
344
     *
345
     * @param string|null $ip User's IP address
346
     * @param string|null $realIp User's real IP address, if available
347
     * @param string|null $merchantIp Your website's IP address
348
     *
349
     * @return $this
350
     */
351 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...
352
    {
353
        if ($ip !== null && !is_string($ip)) {
354
            throw new \InvalidArgumentException('IP must be string');
355
        }
356
        if ($realIp !== null && !is_string($realIp)) {
357
            throw new \InvalidArgumentException('Real IP must be string');
358
        }
359
        if ($merchantIp !== null && !is_string($merchantIp)) {
360
            throw new \InvalidArgumentException('Merchant IP must be string');
361
        }
362
363
        $this->replace('ip', $ip);
364
        $this->replace('real_ip', $realIp);
365
        $this->replace('merchant_ip', $merchantIp);
366
367
        return $this;
368
    }
369
370
    /**
371
     * Provides browser information for envelope
372
     *
373
     * @param string|null $deviceFingerprint
374
     * @param string|null $userAgent
375
     * @param string|null $cpuClass
376
     * @param string|null $screenOrientation
377
     * @param string|null $screenResolution
378
     * @param string|null $os
379
     * @param int|null $timezoneOffset
380
     * @param string|null $languages
381
     * @param string|null $language
382
     * @param string|null $languageBrowser
383
     * @param string|null $languageUser
384
     * @param string|null $languageSystem
385
     * @param bool|null $cookieEnabled
386
     * @param bool|null $doNotTrack
387
     * @param bool|null $ajaxValidation
388
     *
389
     * @return $this
390
     */
391
    public function addBrowserData(
392
        $deviceFingerprint = '',
393
        $userAgent = '',
394
        $cpuClass = '',
395
        $screenOrientation = '',
396
        $screenResolution = '',
397
        $os = '',
398
        $timezoneOffset = null,
399
        $languages = '',
400
        $language = '',
401
        $languageBrowser = '',
402
        $languageUser = '',
403
        $languageSystem = '',
404
        $cookieEnabled = false,
405
        $doNotTrack = false,
406
        $ajaxValidation = false
407
    ) {
408
        if ($deviceFingerprint !== null && !is_string($deviceFingerprint)) {
409
            throw new \InvalidArgumentException('Device fingerprint must be string');
410
        }
411
        if ($userAgent !== null && !is_string($userAgent)) {
412
            throw new \InvalidArgumentException('User agent must be string');
413
        }
414
        if ($cpuClass !== null && !is_string($cpuClass)) {
415
            throw new \InvalidArgumentException('CPU class must be string');
416
        }
417
        if ($screenOrientation !== null && !is_string($screenOrientation)) {
418
            throw new \InvalidArgumentException('Screen orientation must be string');
419
        }
420
        if ($screenResolution !== null && !is_string($screenResolution)) {
421
            throw new \InvalidArgumentException('Screen resolution must be string');
422
        }
423
        if ($os !== null && !is_string($os)) {
424
            throw new \InvalidArgumentException('OS must be string');
425
        }
426
        if ($timezoneOffset !== null && $timezoneOffset !== null && !is_int($timezoneOffset)) {
427
            throw new \InvalidArgumentException('Timezone offset must be integer or null');
428
        }
429
        if ($languages !== null && !is_string($languages)) {
430
            throw new \InvalidArgumentException('Languages must be string');
431
        }
432
        if ($language !== null && !is_string($language)) {
433
            throw new \InvalidArgumentException('Language must be string');
434
        }
435
        if ($languageBrowser !== null && !is_string($languageBrowser)) {
436
            throw new \InvalidArgumentException('Browser language must be string');
437
        }
438
        if ($languageUser !== null && !is_string($languageUser)) {
439
            throw new \InvalidArgumentException('User language must be string');
440
        }
441
        if ($languageSystem !== null && !is_string($languageSystem)) {
442
            throw new \InvalidArgumentException('System language must be string');
443
        }
444
        if ($cookieEnabled !== null && !is_bool($cookieEnabled)) {
445
            throw new \InvalidArgumentException('Cookie enabled flag must be boolean');
446
        }
447
        if ($doNotTrack !== null && !is_bool($doNotTrack)) {
448
            throw new \InvalidArgumentException('DNT flag must be boolean');
449
        }
450
        if ($ajaxValidation !== null && !is_bool($ajaxValidation)) {
451
            throw new \InvalidArgumentException('AJAX validation flag must be boolean');
452
        }
453
454
        $this->replace('device_fingerprint', $deviceFingerprint);
455
        $this->replace('user_agent', $userAgent);
456
        $this->replace('cpu_class', $cpuClass);
457
        $this->replace('screen_orientation', $screenOrientation);
458
        $this->replace('screen_resolution', $screenResolution);
459
        $this->replace('os', $os);
460
        $this->replace('timezone_offset', $timezoneOffset);
461
        $this->replace('languages', $languages);
462
        $this->replace('language', $language);
463
        $this->replace('language_browser', $languageBrowser);
464
        $this->replace('language_system', $languageSystem);
465
        $this->replace('language_user', $languageUser);
466
        $this->replace('cookie_enabled', $cookieEnabled);
467
        $this->replace('do_not_track', $doNotTrack);
468
        $this->replace('ajax_validation', $ajaxValidation);
469
470
        return $this;
471
    }
472
473
    /**
474
     * Provides user data for envelope
475
     *
476
     * @param string|null $email
477
     * @param string|null $userId
478
     * @param string|null $phone
479
     * @param string|null $userName
480
     * @param string|null $firstName
481
     * @param string|null $lastName
482
     * @param string|null $gender
483
     * @param int|null $age
484
     * @param string|null $country
485
     * @param string|null $socialType
486
     * @param int|null $registrationTimestamp
487
     * @param int|null $loginTimeStamp
488
     * @param int|null $confirmationTimeStamp
489
     * @param bool|null $emailConfirmed
490
     * @param bool|null $phoneConfirmed
491
     * @param bool|null $loginFailed
492
     *
493
     * @return $this
494
     */
495
    public function addUserData(
496
        $email = '',
497
        $userId = '',
498
        $phone = '',
499
        $userName = '',
500
        $firstName = '',
501
        $lastName = '',
502
        $gender = '',
503
        $age = 0,
504
        $country = '',
505
        $socialType = '',
506
        $registrationTimestamp = 0,
507
        $loginTimeStamp = 0,
508
        $confirmationTimeStamp = 0,
509
        $emailConfirmed = false,
510
        $phoneConfirmed = false,
511
        $loginFailed = false
512
    ) {
513
        if ($userName !== null && !is_string($userName)) {
514
            throw new \InvalidArgumentException('User name must be string');
515
        }
516
        if ($gender !== null && !is_string($gender)) {
517
            throw new \InvalidArgumentException('Gender must be string');
518
        }
519
        if ($age !== null && !is_int($age)) {
520
            throw new \InvalidArgumentException('Age must be integer');
521
        }
522
        if ($socialType !== null && !is_string($socialType)) {
523
            throw new \InvalidArgumentException('Social type must be string');
524
        }
525
        if ($registrationTimestamp !== null && !is_int($registrationTimestamp)) {
526
            throw new \InvalidArgumentException('Registration timestamp must be integer');
527
        }
528
        if ($loginTimeStamp !== null && !is_int($loginTimeStamp)) {
529
            throw new \InvalidArgumentException('Login timestamp must be integer');
530
        }
531
        if ($confirmationTimeStamp !== null && !is_int($confirmationTimeStamp)) {
532
            throw new \InvalidArgumentException('Confirmation timestamp must be integer');
533
        }
534
        if ($emailConfirmed !== null && !is_bool($emailConfirmed)) {
535
            throw new \InvalidArgumentException('Email confirmed flag must be boolean');
536
        }
537
        if ($phoneConfirmed !== null && !is_bool($phoneConfirmed)) {
538
            throw new \InvalidArgumentException('Phone confirmed flag must be boolean');
539
        }
540
        if ($loginFailed !== null && !is_bool($loginFailed)) {
541
            throw new \InvalidArgumentException('Login failed flag must be boolean');
542
        }
543
544
        $this->addShortUserData($email, $userId, $phone, $firstName, $lastName, $country);
545
546
        $this->replace('user_name', $userName);
547
        $this->replace('gender', $gender);
548
        $this->replace('age', $age);
549
        $this->replace('social_type', $socialType);
550
        $this->replace('registration_timestamp', $registrationTimestamp);
551
        $this->replace('login_timestamp', $loginTimeStamp);
552
        $this->replace('confirmation_timestamp', $confirmationTimeStamp);
553
        $this->replace('email_confirmed', $emailConfirmed);
554
        $this->replace('phone_confirmed', $phoneConfirmed);
555
        $this->replace('login_failed', $loginFailed);
556
557
        return $this;
558
    }
559
560
    /**
561
     * Provides user data for envelope
562
     *
563
     * @param string|null $email
564
     * @param string|null $userId
565
     * @param string|null $phone
566
     * @param string|null $firstName
567
     * @param string|null $lastName
568
     * @param string|null $country
569
     *
570
     * @return $this
571
     */
572
    public function addShortUserData(
573
        $email = '',
574
        $userId = '',
575
        $phone = '',
576
        $firstName = '',
577
        $lastName = '',
578
        $country = ''
579
    ) {
580
        if ($email !== null && !is_string($email)) {
581
            throw new \InvalidArgumentException('Email must be string');
582
        }
583
        if (is_int($userId)) {
584
            $userId = strval($userId);
585
        }
586
        if ($userId !== null && !is_string($userId)) {
587
            throw new \InvalidArgumentException('UserId must be string or integer');
588
        }
589
        if ($phone !== null && !is_string($phone)) {
590
            throw new \InvalidArgumentException('Phone must be string');
591
        }
592
        if ($firstName !== null && !is_string($firstName)) {
593
            throw new \InvalidArgumentException('First name must be string');
594
        }
595
        if ($lastName !== null && !is_string($lastName)) {
596
            throw new \InvalidArgumentException('Last name must be string');
597
        }
598
        if ($country !== null && !is_string($country)) {
599
            throw new \InvalidArgumentException('Country must be string');
600
        }
601
602
        $this->replace('email', $email);
603
        $this->replace('user_merchant_id', $userId);
604
        $this->replace('phone', $phone);
605
        $this->replace('firstname', $firstName);
606
        $this->replace('lastname', $lastName);
607
        $this->replace('country', $country);
608
609
        return $this;
610
    }
611
612
    /**
613
     * Provides credit card data to envelope
614
     *
615
     * @param string|null $transactionId
616
     * @param string|null $transactionSource
617
     * @param string|null $transactionType
618
     * @param string|null $transactionMode
619
     * @param string|null $transactionTimestamp
620
     * @param string|null $transactionCurrency
621
     * @param string|null $transactionAmount
622
     * @param float|null $amountConverted
623
     * @param string|null $paymentMethod
624
     * @param string|null $paymentSystem
625
     * @param string|null $paymentMidName
626
     *
627
     * @return $this
628
     */
629
    public function addCCTransactionData(
630
        $transactionId,
631
        $transactionSource,
632
        $transactionType,
633
        $transactionMode,
634
        $transactionTimestamp,
635
        $transactionCurrency,
636
        $transactionAmount,
637
        $amountConverted = 0.0,
638
        $paymentMethod = '',
639
        $paymentSystem = '',
640
        $paymentMidName = ''
641
    ) {
642
        if ($transactionId !== null && !is_string($transactionId)) {
643
            throw new \InvalidArgumentException('Transaction ID must be string');
644
        }
645
        if ($transactionSource !== null && !is_string($transactionSource)) {
646
            throw new \InvalidArgumentException('Transaction source must be string');
647
        }
648
        if ($transactionType !== null && !is_string($transactionType)) {
649
            throw new \InvalidArgumentException('Transaction type must be string');
650
        }
651
        if ($transactionMode !== null && !is_string($transactionMode)) {
652
            throw new \InvalidArgumentException('Transaction mode must be string');
653
        }
654
        if ($transactionTimestamp !== null && !is_int($transactionTimestamp)) {
655
            throw new \InvalidArgumentException('Transaction timestamp must be integer');
656
        }
657
        if ($transactionAmount !== null && !is_int($transactionAmount) && !is_float($transactionAmount)) {
658
            throw new \InvalidArgumentException('Transaction amount must be float');
659
        }
660
        if ($transactionCurrency !== null && !is_string($transactionCurrency)) {
661
            throw new \InvalidArgumentException('Transaction currency must be string');
662
        }
663
        if ($paymentMethod !== null && !is_string($paymentMethod)) {
664
            throw new \InvalidArgumentException('Payment method must be string');
665
        }
666
        if ($paymentSystem !== null && !is_string($paymentSystem)) {
667
            throw new \InvalidArgumentException('Payment system must be string');
668
        }
669
        if ($paymentMidName !== null && !is_string($paymentMidName)) {
670
            throw new \InvalidArgumentException('Payment MID name must be string');
671
        }
672
        if ($amountConverted !== null && !is_int($amountConverted) && !is_float($amountConverted)) {
673
            throw new \InvalidArgumentException('Transaction amount converted must be float');
674
        }
675
676
        $this->replace('transaction_id', $transactionId);
677
        $this->replace('transaction_source', $transactionSource);
678
        $this->replace('transaction_type', $transactionType);
679
        $this->replace('transaction_mode', $transactionMode);
680
        $this->replace('transaction_timestamp', $transactionTimestamp);
681
        $this->replace('transaction_amount', floatval($transactionAmount));
682
        $this->replace('transaction_amount_converted', floatval($amountConverted));
683
        $this->replace('transaction_currency', $transactionCurrency);
684
        $this->replace('payment_method', $paymentMethod);
685
        $this->replace('payment_system', $paymentSystem);
686
        $this->replace('payment_mid', $paymentMidName);
687
688
        return $this;
689
    }
690
691
    /**
692
     * Provides Card data to envelope
693
     *
694
     * @param int|null $cardBin
695
     * @param string|null $cardLast4
696
     * @param int|null $expirationMonth
697
     * @param int|null $expirationYear
698
     * @param string|null $cardId
699
     *
700
     * @return $this
701
     */
702
    public function addCardData(
703
        $cardBin,
704
        $cardLast4,
705
        $expirationMonth,
706
        $expirationYear,
707
        $cardId = ''
708
    ) {
709
        if ($cardId !== null && !is_string($cardId)) {
710
            throw new \InvalidArgumentException('Card ID must be string');
711
        }
712
        if ($cardBin !== null && !is_int($cardBin)) {
713
            throw new \InvalidArgumentException('Card BIN must be integer');
714
        }
715
        if ($cardLast4 !== null && !is_string($cardLast4)) {
716
            throw new \InvalidArgumentException('Card last4  must be string');
717
        }
718
        if ($expirationMonth !== null && !is_int($expirationMonth)) {
719
            throw new \InvalidArgumentException('Expiration month must be integer');
720
        }
721
        if ($expirationYear !== null && !is_int($expirationYear)) {
722
            throw new \InvalidArgumentException('Expiration year must be integer');
723
        }
724
725
        $this->replace('card_id', $cardId);
726
        $this->replace('card_bin', $cardBin);
727
        $this->replace('card_last4', $cardLast4);
728
        $this->replace('expiration_month', $expirationMonth);
729
        $this->replace('expiration_year', $expirationYear);
730
731
        return $this;
732
    }
733
734
    /**
735
     * Provides billing data to envelope
736
     *
737
     * @param string|null $billingFirstName
738
     * @param string|null $billingLastName
739
     * @param string|null $billingFullName
740
     * @param string|null $billingCountry
741
     * @param string|null $billingState
742
     * @param string|null $billingCity
743
     * @param string|null $billingAddress
744
     * @param string|null $billingZip
745
     *
746
     * @return $this
747
     */
748
    public function addBillingData(
749
        $billingFirstName = '',
750
        $billingLastName = '',
751
        $billingFullName = '',
752
        $billingCountry = '',
753
        $billingState = '',
754
        $billingCity = '',
755
        $billingAddress = '',
756
        $billingZip = ''
757
    ) {
758
        if ($billingFirstName !== null && !is_string($billingFirstName)) {
759
            throw new \InvalidArgumentException('Billing first name must be string');
760
        }
761
        if ($billingLastName !== null && !is_string($billingLastName)) {
762
            throw new \InvalidArgumentException('Billing last name must be string');
763
        }
764
        if ($billingFullName !== null && !is_string($billingFullName)) {
765
            throw new \InvalidArgumentException('Billing full name must be string');
766
        }
767
        if ($billingCountry !== null && !is_string($billingCountry)) {
768
            throw new \InvalidArgumentException('Billing country name must be string');
769
        }
770
        if ($billingState !== null && !is_string($billingState)) {
771
            throw new \InvalidArgumentException('Billing state must be string');
772
        }
773
        if ($billingCity !== null && !is_string($billingCity)) {
774
            throw new \InvalidArgumentException('Billing city must be string');
775
        }
776
        if ($billingAddress !== null && !is_string($billingAddress)) {
777
            throw new \InvalidArgumentException('Billing address must be string');
778
        }
779
        if ($billingZip !== null && !is_string($billingZip)) {
780
            throw new \InvalidArgumentException('Billing zip must be string');
781
        }
782
783
        $this->replace('billing_firstname', $billingFirstName);
784
        $this->replace('billing_lastname', $billingLastName);
785
        $this->replace('billing_fullname', $billingFullName);
786
        $this->replace('billing_country', $billingCountry);
787
        $this->replace('billing_state', $billingState);
788
        $this->replace('billing_city', $billingCity);
789
        $this->replace('billing_address', $billingAddress);
790
        $this->replace('billing_zip', $billingZip);
791
792
        return $this;
793
    }
794
795
    /**
796
     * Provides product information to envelope
797
     *
798
     * @param float|null $productQuantity
799
     * @param string|null $productName
800
     * @param string|null $productDescription
801
     *
802
     * @return $this
803
     */
804 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...
805
        $productQuantity = 0.0,
806
        $productName = '',
807
        $productDescription = ''
808
    ) {
809
        if ($productQuantity !== null && !is_int($productQuantity) && !is_float($productQuantity)) {
810
            throw new \InvalidArgumentException('Product quantity must be int or float');
811
        }
812
        if ($productName !== null && !is_string($productName)) {
813
            throw new \InvalidArgumentException('Product name must be string');
814
        }
815
        if ($productDescription !== null && !is_string($productDescription)) {
816
            throw new \InvalidArgumentException('Product description must be string');
817
        }
818
819
        $this->replace('product_quantity', $productQuantity);
820
        $this->replace('product_name', $productName);
821
        $this->replace('product_description', $productDescription);
822
823
        return $this;
824
    }
825
826
    /**
827
     * Provides payout information to envelope
828
     *
829
     * @param string $payout_id
830
     * @param int $payout_timestamp
831
     * @param string $payout_card_id
832
     * @param int|float $payout_amount
833
     * @param string $payout_currency
834
     * @param string|null $payout_method
835
     * @param string|null $payout_system
836
     * @param string|null $payout_mid
837
     * @param int|float|null $amount_converted
838
     * @param int|null $payout_card_bin
839
     * @param string|null $payout_card_last4
840
     * @param int|null $payout_expiration_month
841
     * @param int|null $payout_expiration_year
842
     *
843
     * @return $this
844
     */
845
    public function addPayoutData(
846
        $payout_id,
847
        $payout_timestamp,
848
        $payout_card_id,
849
        $payout_amount,
850
        $payout_currency,
851
        $payout_method = null,
852
        $payout_system = null,
853
        $payout_mid = null,
854
        $amount_converted = null,
855
        $payout_card_bin = null,
856
        $payout_card_last4 = null,
857
        $payout_expiration_month = null,
858
        $payout_expiration_year = null
859
    ) {
860
        if (!is_string($payout_id)) {
861
            throw new \InvalidArgumentException('Payout ID must be string');
862
        }
863
        if (!is_int($payout_timestamp)) {
864
            throw new \InvalidArgumentException('Payout timestamp must be int');
865
        }
866
        if (!is_string($payout_card_id)) {
867
            throw new \InvalidArgumentException('Card ID must be string');
868
        }
869
        if (!is_float($payout_amount) && !is_int($payout_amount)) {
870
            throw new \InvalidArgumentException('Amount must be number');
871
        }
872
        if (!is_string($payout_currency)) {
873
            throw new \InvalidArgumentException('Payout currency must be string');
874
        }
875
        if ($payout_method !== null && !is_string($payout_method)) {
876
            throw new \InvalidArgumentException('Payout method must be string');
877
        }
878
        if ($payout_system !== null && !is_string($payout_system)) {
879
            throw new \InvalidArgumentException('Payout system must be string');
880
        }
881
        if ($payout_mid !== null && !is_string($payout_mid)) {
882
            throw new \InvalidArgumentException('Payout MID must be string');
883
        }
884
        if ($amount_converted !== null && !is_float($amount_converted) && !is_int($amount_converted)) {
885
            throw new \InvalidArgumentException('Payout converted amount must be number');
886
        }
887
        if ($payout_card_bin !== null && !is_int($payout_card_bin)) {
888
            throw new \InvalidArgumentException('Payout card BIN must be integer');
889
        }
890
        if ($payout_card_last4 !== null && !is_string($payout_card_last4)) {
891
            throw new \InvalidArgumentException('Payout last 4 must be string');
892
        }
893
        if ($payout_expiration_month !== null && !is_int($payout_expiration_month)) {
894
            throw new \InvalidArgumentException('Payout card expiration month must be integer');
895
        }
896
        if ($payout_expiration_year !== null && !is_int($payout_expiration_year)) {
897
            throw new \InvalidArgumentException('Payout card expiration year must be integer');
898
        }
899
900
        $this->replace('payout_id', $payout_id);
901
        $this->replace('payout_timestamp', $payout_timestamp);
902
        $this->replace('payout_card_id', $payout_card_id);
903
        $this->replace('payout_amount', (float) $payout_amount);
904
        $this->replace('payout_currency', $payout_currency);
905
        $this->replace('payout_method', $payout_method);
906
        $this->replace('payout_system', $payout_system);
907
        $this->replace('payout_mid', $payout_mid);
908
        $this->replace('payout_amount_converted', (float) $amount_converted);
909
        $this->replace('payout_card_bin', $payout_card_bin);
910
        $this->replace('payout_card_last4', $payout_card_last4);
911
        $this->replace('payout_expiration_month', $payout_expiration_month);
912
        $this->replace('payout_expiration_year', $payout_expiration_year);
913
914
        return $this;
915
    }
916
917
    /**
918
     * Adds custom data field to envelope
919
     *
920
     * @param string $name
921
     * @param string $value
922
     *
923
     * @return $this
924
     */
925
    public function addCustomField($name, $value)
926
    {
927
        if (!is_string($name)) {
928
            throw new \InvalidArgumentException('Custom field name must be string');
929
        }
930
        if (!is_string($value)) {
931
            throw new \InvalidArgumentException('Custom field value must be string');
932
        }
933
934
        if (strlen($name) < 8 || substr($name, 0, 7) !== 'custom_') {
935
            $name = 'custom_' . $name;
936
        }
937
938
        $this->replace($name, $value);
939
        return $this;
940
    }
941
}
942