Completed
Push — master ( f18e18...2f846d )
by Anton
11s
created

Builder::addBrowserData()   D

Complexity

Conditions 32
Paths 16

Size

Total Lines 81
Code Lines 62

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 81
c 0
b 0
f 0
rs 4.8939
cc 32
eloc 62
nc 16
nop 15

How to fix   Long Method    Complexity    Many Parameters   

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:

Many Parameters

Methods with many parameters are not only hard to understand, but their parameters also often become inconsistent when you need more, or different data.

There are several approaches to avoid long parameter lists:

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 Builder
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
     * Returns builder for transaction request
255
     *
256
     * @param string $sequenceId
257
     * @param string $userId
258
     * @param string $transactionId
259
     * @param int|float $transactionAmount
260
     * @param string $transactionCurrency
261
     * @param int|null $transactionTimestamp
262
     * @param string|null $transactionMode
263
     * @param string|null $transactionType
264
     * @param int|null $cardBin
265
     * @param string|null $cardId
266
     * @param string|null $cardLast4
267
     * @param int|null $expirationMonth
268
     * @param int|null $expirationYear
269
     * @param int|null $age
270
     * @param string|null $country
271
     * @param string|null $email
272
     * @param string|null $gender
273
     * @param string|null $firstName
274
     * @param string|null $lastName
275
     * @param string|null $phone
276
     * @param string|null $userName
277
     * @param string|null $paymentAccountId
278
     * @param string|null $paymentMethod
279
     * @param string|null $paymentMidName
280
     * @param string|null $paymentSystem
281
     * @param int|float|null $transactionAmountConverted
282
     * @param string|null $transactionSource
283
     * @param string|null $billingAddress
284
     * @param string|null $billingCity
285
     * @param string|null $billingCountry
286
     * @param string|null $billingFirstName
287
     * @param string|null $billingLastName
288
     * @param string|null $billingFullName
289
     * @param string|null $billingState
290
     * @param string|null $billingZip
291
     * @param string|null $productDescription
292
     * @param string|null $productName
293
     * @param int|float|null $productQuantity
294
     * @param string|null $websiteUrl
295
     * @param string|null $merchantIp
296
     *
297
     * @return Builder
298
     */
299
    public static function transactionEvent(
300
        $sequenceId,
301
        $userId,
302
        $transactionId,
303
        $transactionAmount,
304
        $transactionCurrency,
305
        $transactionTimestamp = null,
306
        $transactionMode = null,
307
        $transactionType = null,
308
        $cardBin = null,
309
        $cardId = null,
310
        $cardLast4 = null,
311
        $expirationMonth = null,
312
        $expirationYear = null,
313
        $age = null,
314
        $country = null,
315
        $email = null,
316
        $gender = null,
317
        $firstName = null,
318
        $lastName = null,
319
        $phone = null,
320
        $userName = null,
321
        $paymentAccountId = null,
322
        $paymentMethod = null,
323
        $paymentMidName = null,
324
        $paymentSystem = null,
325
        $transactionAmountConverted = null,
326
        $transactionSource = null,
327
        $billingAddress = null,
328
        $billingCity = null,
329
        $billingCountry = null,
330
        $billingFirstName = null,
331
        $billingLastName = null,
332
        $billingFullName = null,
333
        $billingState = null,
334
        $billingZip = null,
335
        $productDescription = null,
336
        $productName = null,
337
        $productQuantity = null,
338
        $websiteUrl = null,
339
        $merchantIp = null
340
    ) {
341
        $builder = new self('transaction', $sequenceId);
342
        if ($transactionTimestamp === null) {
343
            $transactionTimestamp = time();
344
        }
345
346
        return $builder
347
            ->addCCTransactionData(
348
                $transactionId,
349
                $transactionSource,
350
                $transactionType,
351
                $transactionMode,
352
                $transactionTimestamp,
353
                $transactionCurrency,
354
                $transactionAmount,
355
                $transactionAmountConverted,
356
                $paymentMethod,
357
                $paymentSystem,
358
                $paymentMidName,
359
                $paymentAccountId
360
            )
361
            ->addBillingData(
362
                $billingFirstName,
363
                $billingLastName,
364
                $billingFullName,
365
                $billingCountry,
366
                $billingState,
367
                $billingCity,
368
                $billingAddress,
369
                $billingZip
370
            )
371
            ->addCardData($cardBin, $cardLast4, $expirationMonth, $expirationYear, $cardId)
372
            ->addUserData(
373
                $email,
374
                $userId,
375
                $phone,
376
                $userName,
377
                $firstName,
378
                $lastName,
379
                $gender,
380
                $age,
381
                $country
382
            )
383
            ->addProductData($productQuantity, $productName, $productDescription)
384
            ->addWebsiteData($websiteUrl)
385
            ->addIpData(null, null, $merchantIp);
386
387
    }
388
389
    /**
390
     * Returns builder for install request
391
     *
392
     * @param string $sequenceId
393
     * @param string|null $userId
394
     * @param int|null $installTimestamp
395
     * @param string|null $country
396
     * @param string|null $websiteUrl
397
     * @param string|null $trafficSource
398
     * @param string|null $affiliateId
399
     *
400
     * @return Builder
401
     */
402
    public static function installEvent(
403
        $sequenceId,
404
        $userId = null,
405
        $installTimestamp = null,
406
        $country = null,
407
        $websiteUrl = null,
408
        $trafficSource = null,
409
        $affiliateId = null
410
    ) {
411
        $builder = new self('install', $sequenceId);
412
        if ($installTimestamp === null) {
413
            $installTimestamp = time();
414
        }
415
416
        return $builder->addInstallData(
417
            $installTimestamp
418
        )->addWebsiteData($websiteUrl, $trafficSource, $affiliateId)
419
        ->addShortUserData(null, $userId, null, null, null, $country);
420
    }
421
422
    /**
423
     * Returns builder for refund request
424
     *
425
     * @param string $sequenceId
426
     * @param string $refundId
427
     * @param int|float $refundAmount
428
     * @param string $refundCurrency
429
     * @param int|null $refundTimestamp
430
     * @param int|float|null $refundAmountConverted
431
     * @param string|null $refundSource
432
     * @param string|null $refundType
433
     * @param string|null $refundCode
434
     * @param string|null $refundReason
435
     * @param string|null $agentId
436
     *
437
     * @return Builder
438
     */
439
    public static function refundEvent(
440
        $sequenceId,
441
        $refundId,
442
        $refundAmount,
443
        $refundCurrency,
444
        $refundTimestamp = null,
445
        $refundAmountConverted = null,
446
        $refundSource = null,
447
        $refundType = null,
448
        $refundCode = null,
449
        $refundReason = null,
450
        $agentId = null
451
    ) {
452
        $builder = new self('refund', $sequenceId);
453
        if ($refundTimestamp === null) {
454
            $refundTimestamp = time();
455
        }
456
457
        return $builder->addRefundData(
458
            $refundId,
459
            $refundTimestamp,
460
            $refundAmount,
461
            $refundCurrency,
462
            $refundAmountConverted,
463
            $refundSource,
464
            $refundType,
465
            $refundCode,
466
            $refundReason,
467
            $agentId
468
        );
469
    }
470
471
    /**
472
     * Builder constructor.
473
     *
474
     * @param string $envelopeType
475
     * @param string $sequenceId
476
     */
477
    public function __construct($envelopeType, $sequenceId)
478
    {
479
        if (!is_string($envelopeType)) {
480
            throw new \InvalidArgumentException('Envelope type must be string');
481
        }
482
        if (!is_string($sequenceId)) {
483
            throw new \InvalidArgumentException('Sequence ID must be string');
484
        }
485
486
        $this->type = $envelopeType;
487
        $this->sequenceId = $sequenceId;
488
    }
489
490
    /**
491
     * Returns built envelope
492
     *
493
     * @return EnvelopeInterface
494
     */
495
    public function build()
496
    {
497
        return new Envelope(
498
            $this->type,
499
            $this->sequenceId,
500
            $this->identities,
501
            array_filter($this->data, function ($data) {
502
                return $data !== null;
503
            })
504
        );
505
    }
506
507
    /**
508
     * Replaces value in internal array if provided value not empty
509
     *
510
     * @param string $key
511
     * @param string|int|float|bool|null $value
512
     */
513
    private function replace($key, $value)
514
    {
515
        if ($value !== null && $value !== '' && $value !== 0 && $value !== 0.0) {
516
            $this->data[$key] = $value;
517
        }
518
    }
519
520
    /**
521
     * Adds identity node
522
     *
523
     * @param IdentityNodeInterface $identity
524
     *
525
     * @return $this
526
     */
527
    public function addIdentity(IdentityNodeInterface $identity)
528
    {
529
        $this->identities[] = $identity;
530
        return $this;
531
    }
532
533
    /**
534
     * Provides website URL to envelope
535
     *
536
     * @param string|null $websiteUrl
537
     * @param string|null $traffic_source
538
     * @param string|null $affiliate_id
539
     *
540
     * @return $this
541
     */
542 View Code Duplication
    public function addWebsiteData($websiteUrl = null, $traffic_source = null, $affiliate_id = null)
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...
543
    {
544
        if ($websiteUrl !== null && !is_string($websiteUrl)) {
545
            throw new \InvalidArgumentException('Website URL must be string');
546
        }
547
        if ($traffic_source !== null && !is_string($traffic_source)) {
548
            throw new \InvalidArgumentException('Traffic source must be string');
549
        }
550
        if ($affiliate_id !== null && !is_string($affiliate_id)) {
551
            throw new \InvalidArgumentException('Affiliate ID must be string');
552
        }
553
554
        $this->replace('website_url', $websiteUrl);
555
        $this->replace('traffic_source', $traffic_source);
556
        $this->replace('affiliate_id', $affiliate_id);
557
        return $this;
558
    }
559
560
    /**
561
     * Provides IP information for envelope
562
     *
563
     * @param string|null $ip User's IP address
564
     * @param string|null $realIp User's real IP address, if available
565
     * @param string|null $merchantIp Your website's IP address
566
     *
567
     * @return $this
568
     */
569
    public function addIpData($ip = '', $realIp = '', $merchantIp = '')
570
    {
571
        if ($ip !== null && !is_string($ip)) {
572
            throw new \InvalidArgumentException('IP must be string');
573
        }
574
        if ($realIp !== null && !is_string($realIp)) {
575
            throw new \InvalidArgumentException('Real IP must be string');
576
        }
577
        if ($merchantIp !== null && !is_string($merchantIp)) {
578
            throw new \InvalidArgumentException('Merchant IP must be string');
579
        }
580
581
        $this->replace('ip', $ip);
582
        $this->replace('real_ip', $realIp);
583
        $this->replace('merchant_ip', $merchantIp);
584
585
        return $this;
586
    }
587
588
    /**
589
     * Provides browser information for envelope
590
     *
591
     * @param string|null $deviceFingerprint
592
     * @param string|null $userAgent
593
     * @param string|null $cpuClass
594
     * @param string|null $screenOrientation
595
     * @param string|null $screenResolution
596
     * @param string|null $os
597
     * @param int|null $timezoneOffset
598
     * @param string|null $languages
599
     * @param string|null $language
600
     * @param string|null $languageBrowser
601
     * @param string|null $languageUser
602
     * @param string|null $languageSystem
603
     * @param bool|null $cookieEnabled
604
     * @param bool|null $doNotTrack
605
     * @param bool|null $ajaxValidation
606
     *
607
     * @return $this
608
     */
609
    public function addBrowserData(
610
        $deviceFingerprint = '',
611
        $userAgent = '',
612
        $cpuClass = '',
613
        $screenOrientation = '',
614
        $screenResolution = '',
615
        $os = '',
616
        $timezoneOffset = null,
617
        $languages = '',
618
        $language = '',
619
        $languageBrowser = '',
620
        $languageUser = '',
621
        $languageSystem = '',
622
        $cookieEnabled = null,
623
        $doNotTrack = null,
624
        $ajaxValidation = null
625
    ) {
626
        if ($deviceFingerprint !== null && !is_string($deviceFingerprint)) {
627
            throw new \InvalidArgumentException('Device fingerprint must be string');
628
        }
629
        if ($userAgent !== null && !is_string($userAgent)) {
630
            throw new \InvalidArgumentException('User agent must be string');
631
        }
632
        if ($cpuClass !== null && !is_string($cpuClass)) {
633
            throw new \InvalidArgumentException('CPU class must be string');
634
        }
635
        if ($screenOrientation !== null && !is_string($screenOrientation)) {
636
            throw new \InvalidArgumentException('Screen orientation must be string');
637
        }
638
        if ($screenResolution !== null && !is_string($screenResolution)) {
639
            throw new \InvalidArgumentException('Screen resolution must be string');
640
        }
641
        if ($os !== null && !is_string($os)) {
642
            throw new \InvalidArgumentException('OS must be string');
643
        }
644
        if ($timezoneOffset !== null && $timezoneOffset !== null && !is_int($timezoneOffset)) {
645
            throw new \InvalidArgumentException('Timezone offset must be integer or null');
646
        }
647
        if ($languages !== null && !is_string($languages)) {
648
            throw new \InvalidArgumentException('Languages must be string');
649
        }
650
        if ($language !== null && !is_string($language)) {
651
            throw new \InvalidArgumentException('Language must be string');
652
        }
653
        if ($languageBrowser !== null && !is_string($languageBrowser)) {
654
            throw new \InvalidArgumentException('Browser language must be string');
655
        }
656
        if ($languageUser !== null && !is_string($languageUser)) {
657
            throw new \InvalidArgumentException('User language must be string');
658
        }
659
        if ($languageSystem !== null && !is_string($languageSystem)) {
660
            throw new \InvalidArgumentException('System language must be string');
661
        }
662
        if ($cookieEnabled !== null && !is_bool($cookieEnabled)) {
663
            throw new \InvalidArgumentException('Cookie enabled flag must be boolean');
664
        }
665
        if ($doNotTrack !== null && !is_bool($doNotTrack)) {
666
            throw new \InvalidArgumentException('DNT flag must be boolean');
667
        }
668
        if ($ajaxValidation !== null && !is_bool($ajaxValidation)) {
669
            throw new \InvalidArgumentException('AJAX validation flag must be boolean');
670
        }
671
672
        $this->replace('device_fingerprint', $deviceFingerprint);
673
        $this->replace('user_agent', $userAgent);
674
        $this->replace('cpu_class', $cpuClass);
675
        $this->replace('screen_orientation', $screenOrientation);
676
        $this->replace('screen_resolution', $screenResolution);
677
        $this->replace('os', $os);
678
        $this->replace('timezone_offset', $timezoneOffset);
679
        $this->replace('languages', $languages);
680
        $this->replace('language', $language);
681
        $this->replace('language_browser', $languageBrowser);
682
        $this->replace('language_system', $languageSystem);
683
        $this->replace('language_user', $languageUser);
684
        $this->replace('cookie_enabled', $cookieEnabled);
685
        $this->replace('do_not_track', $doNotTrack);
686
        $this->replace('ajax_validation', $ajaxValidation);
687
688
        return $this;
689
    }
690
691
    /**
692
     * Provides user data for envelope
693
     *
694
     * @param string|null $email
695
     * @param string|null $userId
696
     * @param string|null $phone
697
     * @param string|null $userName
698
     * @param string|null $firstName
699
     * @param string|null $lastName
700
     * @param string|null $gender
701
     * @param int|null $age
702
     * @param string|null $country
703
     * @param string|null $socialType
704
     * @param int|null $registrationTimestamp
705
     * @param int|null $loginTimeStamp
706
     * @param int|null $confirmationTimeStamp
707
     * @param bool|null $emailConfirmed
708
     * @param bool|null $phoneConfirmed
709
     * @param bool|null $loginFailed
710
     *
711
     * @return $this
712
     */
713
    public function addUserData(
714
        $email = '',
715
        $userId = '',
716
        $phone = '',
717
        $userName = '',
718
        $firstName = '',
719
        $lastName = '',
720
        $gender = '',
721
        $age = 0,
722
        $country = '',
723
        $socialType = '',
724
        $registrationTimestamp = 0,
725
        $loginTimeStamp = 0,
726
        $confirmationTimeStamp = 0,
727
        $emailConfirmed = null,
728
        $phoneConfirmed = null,
729
        $loginFailed = null
730
    ) {
731
        if ($userName !== null && !is_string($userName)) {
732
            throw new \InvalidArgumentException('User name must be string');
733
        }
734
        if ($gender !== null && !is_string($gender)) {
735
            throw new \InvalidArgumentException('Gender must be string');
736
        }
737
        if ($age !== null && !is_int($age)) {
738
            throw new \InvalidArgumentException('Age must be integer');
739
        }
740
        if ($socialType !== null && !is_string($socialType)) {
741
            throw new \InvalidArgumentException('Social type must be string');
742
        }
743
        if ($registrationTimestamp !== null && !is_int($registrationTimestamp)) {
744
            throw new \InvalidArgumentException('Registration timestamp must be integer');
745
        }
746
        if ($loginTimeStamp !== null && !is_int($loginTimeStamp)) {
747
            throw new \InvalidArgumentException('Login timestamp must be integer');
748
        }
749
        if ($confirmationTimeStamp !== null && !is_int($confirmationTimeStamp)) {
750
            throw new \InvalidArgumentException('Confirmation timestamp must be integer');
751
        }
752
        if ($emailConfirmed !== null && !is_bool($emailConfirmed)) {
753
            throw new \InvalidArgumentException('Email confirmed flag must be boolean');
754
        }
755
        if ($phoneConfirmed !== null && !is_bool($phoneConfirmed)) {
756
            throw new \InvalidArgumentException('Phone confirmed flag must be boolean');
757
        }
758
        if ($loginFailed !== null && !is_bool($loginFailed)) {
759
            throw new \InvalidArgumentException('Login failed flag must be boolean');
760
        }
761
762
        $this->addShortUserData($email, $userId, $phone, $firstName, $lastName, $country);
763
764
        $this->replace('user_name', $userName);
765
        $this->replace('gender', $gender);
766
        $this->replace('age', $age);
767
        $this->replace('social_type', $socialType);
768
        $this->replace('registration_timestamp', $registrationTimestamp);
769
        $this->replace('login_timestamp', $loginTimeStamp);
770
        $this->replace('confirmation_timestamp', $confirmationTimeStamp);
771
        $this->replace('email_confirmed', $emailConfirmed);
772
        $this->replace('phone_confirmed', $phoneConfirmed);
773
        $this->replace('login_failed', $loginFailed);
774
775
        return $this;
776
    }
777
778
    /**
779
     * Provides user data for envelope
780
     *
781
     * @param string|null $email
782
     * @param string|null $userId
783
     * @param string|null $phone
784
     * @param string|null $firstName
785
     * @param string|null $lastName
786
     * @param string|null $country
787
     *
788
     * @return $this
789
     */
790
    public function addShortUserData(
791
        $email = '',
792
        $userId = '',
793
        $phone = '',
794
        $firstName = '',
795
        $lastName = '',
796
        $country = ''
797
    ) {
798
        if ($email !== null && !is_string($email)) {
799
            throw new \InvalidArgumentException('Email must be string');
800
        }
801
        if (is_int($userId)) {
802
            $userId = strval($userId);
803
        }
804
        if ($userId !== null && !is_string($userId)) {
805
            throw new \InvalidArgumentException('UserId must be string or integer');
806
        }
807
        if ($phone !== null && !is_string($phone)) {
808
            throw new \InvalidArgumentException('Phone must be string');
809
        }
810
        if ($firstName !== null && !is_string($firstName)) {
811
            throw new \InvalidArgumentException('First name must be string');
812
        }
813
        if ($lastName !== null && !is_string($lastName)) {
814
            throw new \InvalidArgumentException('Last name must be string');
815
        }
816
        if ($country !== null && !is_string($country)) {
817
            throw new \InvalidArgumentException('Country must be string');
818
        }
819
820
        $this->replace('email', $email);
821
        $this->replace('user_merchant_id', $userId);
822
        $this->replace('phone', $phone);
823
        $this->replace('firstname', $firstName);
824
        $this->replace('lastname', $lastName);
825
        $this->replace('country', $country);
826
827
        return $this;
828
    }
829
830
    /**
831
     * Provides credit card data to envelope
832
     *
833
     * @param string|null $transactionId
834
     * @param string|null $transactionSource
835
     * @param string|null $transactionType
836
     * @param string|null $transactionMode
837
     * @param string|null $transactionTimestamp
838
     * @param string|null $transactionCurrency
839
     * @param string|null $transactionAmount
840
     * @param float|null $amountConverted
841
     * @param string|null $paymentMethod
842
     * @param string|null $paymentSystem
843
     * @param string|null $paymentMidName
844
     * @param string|null $paymentAccountId
845
     *
846
     * @return $this
847
     */
848
    public function addCCTransactionData(
849
        $transactionId,
850
        $transactionSource,
851
        $transactionType,
852
        $transactionMode,
853
        $transactionTimestamp,
854
        $transactionCurrency,
855
        $transactionAmount,
856
        $amountConverted = null,
857
        $paymentMethod = null,
858
        $paymentSystem = null,
859
        $paymentMidName = null,
860
        $paymentAccountId = null
861
    ) {
862
        if ($transactionId !== null && !is_string($transactionId)) {
863
            throw new \InvalidArgumentException('Transaction ID must be string');
864
        }
865
        if ($transactionSource !== null && !is_string($transactionSource)) {
866
            throw new \InvalidArgumentException('Transaction source must be string');
867
        }
868
        if ($transactionType !== null && !is_string($transactionType)) {
869
            throw new \InvalidArgumentException('Transaction type must be string');
870
        }
871
        if ($transactionMode !== null && !is_string($transactionMode)) {
872
            throw new \InvalidArgumentException('Transaction mode must be string');
873
        }
874
        if ($transactionTimestamp !== null && !is_int($transactionTimestamp)) {
875
            throw new \InvalidArgumentException('Transaction timestamp must be integer');
876
        }
877
        if ($transactionAmount !== null && !is_int($transactionAmount) && !is_float($transactionAmount)) {
878
            throw new \InvalidArgumentException('Transaction amount must be float');
879
        }
880
        if ($transactionCurrency !== null && !is_string($transactionCurrency)) {
881
            throw new \InvalidArgumentException('Transaction currency must be string');
882
        }
883
        if ($paymentMethod !== null && !is_string($paymentMethod)) {
884
            throw new \InvalidArgumentException('Payment method must be string');
885
        }
886
        if ($paymentSystem !== null && !is_string($paymentSystem)) {
887
            throw new \InvalidArgumentException('Payment system must be string');
888
        }
889
        if ($paymentMidName !== null && !is_string($paymentMidName)) {
890
            throw new \InvalidArgumentException('Payment MID name must be string');
891
        }
892
        if ($paymentAccountId !== null && !is_string($paymentAccountId)) {
893
            throw new \InvalidArgumentException('Payment account id must be string');
894
        }
895 View Code Duplication
        if ($amountConverted !== null && !is_int($amountConverted) && !is_float($amountConverted)) {
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...
896
            throw new \InvalidArgumentException('Transaction amount converted must be float');
897
        }
898
899
        $this->replace('transaction_id', $transactionId);
900
        $this->replace('transaction_source', $transactionSource);
901
        $this->replace('transaction_type', $transactionType);
902
        $this->replace('transaction_mode', $transactionMode);
903
        $this->replace('transaction_timestamp', $transactionTimestamp);
904
        $this->replace('transaction_amount', floatval($transactionAmount));
905
        $this->replace('transaction_amount_converted', floatval($amountConverted));
906
        $this->replace('transaction_currency', $transactionCurrency);
907
        $this->replace('payment_method', $paymentMethod);
908
        $this->replace('payment_system', $paymentSystem);
909
        $this->replace('payment_mid', $paymentMidName);
910
        $this->replace('payment_account_id', $paymentAccountId);
911
912
        return $this;
913
    }
914
915
    /**
916
     * Provides Card data to envelope
917
     *
918
     * @param string|null $cardId
919
     * @param int|null $cardBin
920
     * @param string|null $cardLast4
921
     * @param int|null $expirationMonth
922
     * @param int|null $expirationYear
923
     *
924
     * @return $this
925
     */
926
    public function addCardData(
927
        $cardBin,
928
        $cardLast4,
929
        $expirationMonth,
930
        $expirationYear,
931
        $cardId = null
932
    ) {
933
        if ($cardId !== null && !is_string($cardId)) {
934
            throw new \InvalidArgumentException('Card ID must be string');
935
        }
936
        if ($cardBin !== null && !is_int($cardBin)) {
937
            throw new \InvalidArgumentException('Card BIN must be integer');
938
        }
939
        if ($cardLast4 !== null && !is_string($cardLast4)) {
940
            throw new \InvalidArgumentException('Card last4  must be string');
941
        }
942
        if ($expirationMonth !== null && !is_int($expirationMonth)) {
943
            throw new \InvalidArgumentException('Expiration month must be integer');
944
        }
945
        if ($expirationYear !== null && !is_int($expirationYear)) {
946
            throw new \InvalidArgumentException('Expiration year must be integer');
947
        }
948
949
        $this->replace('card_id', $cardId);
950
        $this->replace('card_bin', $cardBin);
951
        $this->replace('card_last4', $cardLast4);
952
        $this->replace('expiration_month', $expirationMonth);
953
        $this->replace('expiration_year', $expirationYear);
954
955
        return $this;
956
    }
957
958
    /**
959
     * Provides billing data to envelope
960
     *
961
     * @param string|null $billingFirstName
962
     * @param string|null $billingLastName
963
     * @param string|null $billingFullName
964
     * @param string|null $billingCountry
965
     * @param string|null $billingState
966
     * @param string|null $billingCity
967
     * @param string|null $billingAddress
968
     * @param string|null $billingZip
969
     *
970
     * @return $this
971
     */
972
    public function addBillingData(
973
        $billingFirstName = null,
974
        $billingLastName = null,
975
        $billingFullName = null,
976
        $billingCountry = null,
977
        $billingState = null,
978
        $billingCity = null,
979
        $billingAddress = null,
980
        $billingZip = null
981
    ) {
982
        if ($billingFirstName !== null && !is_string($billingFirstName)) {
983
            throw new \InvalidArgumentException('Billing first name must be string');
984
        }
985
        if ($billingLastName !== null && !is_string($billingLastName)) {
986
            throw new \InvalidArgumentException('Billing last name must be string');
987
        }
988
        if ($billingFullName !== null && !is_string($billingFullName)) {
989
            throw new \InvalidArgumentException('Billing full name must be string');
990
        }
991
        if ($billingCountry !== null && !is_string($billingCountry)) {
992
            throw new \InvalidArgumentException('Billing country name must be string');
993
        }
994
        if ($billingState !== null && !is_string($billingState)) {
995
            throw new \InvalidArgumentException('Billing state must be string');
996
        }
997
        if ($billingCity !== null && !is_string($billingCity)) {
998
            throw new \InvalidArgumentException('Billing city must be string');
999
        }
1000
        if ($billingAddress !== null && !is_string($billingAddress)) {
1001
            throw new \InvalidArgumentException('Billing address must be string');
1002
        }
1003
        if ($billingZip !== null && !is_string($billingZip)) {
1004
            throw new \InvalidArgumentException('Billing zip must be string');
1005
        }
1006
1007
        $this->replace('billing_firstname', $billingFirstName);
1008
        $this->replace('billing_lastname', $billingLastName);
1009
        $this->replace('billing_fullname', $billingFullName);
1010
        $this->replace('billing_country', $billingCountry);
1011
        $this->replace('billing_state', $billingState);
1012
        $this->replace('billing_city', $billingCity);
1013
        $this->replace('billing_address', $billingAddress);
1014
        $this->replace('billing_zip', $billingZip);
1015
1016
        return $this;
1017
    }
1018
1019
    /**
1020
     * Provides product information to envelope
1021
     *
1022
     * @param float|null $productQuantity
1023
     * @param string|null $productName
1024
     * @param string|null $productDescription
1025
     *
1026
     * @return $this
1027
     */
1028 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...
1029
        $productQuantity = null,
1030
        $productName = null,
1031
        $productDescription = null
1032
    ) {
1033
        if ($productQuantity !== null && !is_int($productQuantity) && !is_float($productQuantity)) {
1034
            throw new \InvalidArgumentException('Product quantity must be int or float');
1035
        }
1036
        if ($productName !== null && !is_string($productName)) {
1037
            throw new \InvalidArgumentException('Product name must be string');
1038
        }
1039
        if ($productDescription !== null && !is_string($productDescription)) {
1040
            throw new \InvalidArgumentException('Product description must be string');
1041
        }
1042
1043
        $this->replace('product_quantity', $productQuantity);
1044
        $this->replace('product_name', $productName);
1045
        $this->replace('product_description', $productDescription);
1046
1047
        return $this;
1048
    }
1049
1050
    /**
1051
     * Provides payout information to envelope
1052
     *
1053
     * @param string $payoutId
1054
     * @param int $payoutTimestamp
1055
     * @param string $payoutCardId
1056
     * @param int|float $payoutAmount
1057
     * @param string $payoutCurrency
1058
     * @param string|null $payoutMethod
1059
     * @param string|null $payoutSystem
1060
     * @param string|null $payoutMid
1061
     * @param int|float|null $amountConverted
1062
     * @param int|null $payoutCardBin
1063
     * @param string|null $payoutCardLast4
1064
     * @param int|null $payoutExpirationMonth
1065
     * @param int|null $payoutExpirationYear
1066
     *
1067
     * @return $this
1068
     */
1069
    public function addPayoutData(
1070
        $payoutId,
1071
        $payoutTimestamp,
1072
        $payoutCardId,
1073
        $payoutAmount,
1074
        $payoutCurrency,
1075
        $payoutMethod = null,
1076
        $payoutSystem = null,
1077
        $payoutMid = null,
1078
        $amountConverted = null,
1079
        $payoutCardBin = null,
1080
        $payoutCardLast4 = null,
1081
        $payoutExpirationMonth = null,
1082
        $payoutExpirationYear = null
1083
    ) {
1084
        if (!is_string($payoutId)) {
1085
            throw new \InvalidArgumentException('Payout ID must be string');
1086
        }
1087
        if (!is_int($payoutTimestamp)) {
1088
            throw new \InvalidArgumentException('Payout timestamp must be int');
1089
        }
1090
        if (!is_string($payoutCardId)) {
1091
            throw new \InvalidArgumentException('Card ID must be string');
1092
        }
1093
        if (!is_float($payoutAmount) && !is_int($payoutAmount)) {
1094
            throw new \InvalidArgumentException('Amount must be number');
1095
        }
1096
        if (!is_string($payoutCurrency)) {
1097
            throw new \InvalidArgumentException('Payout currency must be string');
1098
        }
1099
        if ($payoutMethod !== null && !is_string($payoutMethod)) {
1100
            throw new \InvalidArgumentException('Payout method must be string');
1101
        }
1102
        if ($payoutSystem !== null && !is_string($payoutSystem)) {
1103
            throw new \InvalidArgumentException('Payout system must be string');
1104
        }
1105
        if ($payoutMid !== null && !is_string($payoutMid)) {
1106
            throw new \InvalidArgumentException('Payout MID must be string');
1107
        }
1108 View Code Duplication
        if ($amountConverted !== null && !is_float($amountConverted) && !is_int($amountConverted)) {
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...
1109
            throw new \InvalidArgumentException('Payout converted amount must be number');
1110
        }
1111
        if ($payoutCardBin !== null && !is_int($payoutCardBin)) {
1112
            throw new \InvalidArgumentException('Payout card BIN must be integer');
1113
        }
1114
        if ($payoutCardLast4 !== null && !is_string($payoutCardLast4)) {
1115
            throw new \InvalidArgumentException('Payout last 4 must be string');
1116
        }
1117
        if ($payoutExpirationMonth !== null && !is_int($payoutExpirationMonth)) {
1118
            throw new \InvalidArgumentException('Payout card expiration month must be integer');
1119
        }
1120
        if ($payoutExpirationYear !== null && !is_int($payoutExpirationYear)) {
1121
            throw new \InvalidArgumentException('Payout card expiration year must be integer');
1122
        }
1123
1124
        $this->replace('payout_id', $payoutId);
1125
        $this->replace('payout_timestamp', $payoutTimestamp);
1126
        $this->replace('payout_card_id', $payoutCardId);
1127
        $this->replace('payout_amount', (float) $payoutAmount);
1128
        $this->replace('payout_currency', $payoutCurrency);
1129
        $this->replace('payout_method', $payoutMethod);
1130
        $this->replace('payout_system', $payoutSystem);
1131
        $this->replace('payout_mid', $payoutMid);
1132
        $this->replace('payout_amount_converted', (float) $amountConverted);
1133
        $this->replace('payout_card_bin', $payoutCardBin);
1134
        $this->replace('payout_card_last4', $payoutCardLast4);
1135
        $this->replace('payout_expiration_month', $payoutExpirationMonth);
1136
        $this->replace('payout_expiration_year', $payoutExpirationYear);
1137
1138
        return $this;
1139
    }
1140
1141
    /**
1142
     * Provides install information to envelope
1143
     *
1144
     * @param int $installTimestamp
1145
     *
1146
     * @return $this
1147
     */
1148
    public function addInstallData($installTimestamp)
1149
    {
1150
        if (!is_int($installTimestamp)) {
1151
            throw new \InvalidArgumentException('Install timestamp must be int');
1152
        }
1153
1154
        $this->replace('install_timestamp', $installTimestamp);
1155
1156
        return $this;
1157
    }
1158
1159
    /**
1160
     * Provides refund information to envelope
1161
     *
1162
     * @param string $refundId
1163
     * @param int|float $refundAmount
1164
     * @param string $refundCurrency
1165
     * @param int|null $refundTimestamp
1166
     * @param int|float|null $refundAmountConverted
1167
     * @param string|null $refundSource
1168
     * @param string|null $refundType
1169
     * @param string|null $refundCode
1170
     * @param string|null $refundReason
1171
     * @param string|null $agentId
1172
     *
1173
     * @return $this
1174
     */
1175
    public function addRefundData(
1176
        $refundId,
1177
        $refundTimestamp,
1178
        $refundAmount,
1179
        $refundCurrency,
1180
        $refundAmountConverted = null,
1181
        $refundSource = null,
1182
        $refundType = null,
1183
        $refundCode = null,
1184
        $refundReason = null,
1185
        $agentId = null
1186
    ) {
1187
        if (!is_string($refundId)) {
1188
            throw new \InvalidArgumentException('Refund ID must be string');
1189
        }
1190
        if (!is_int($refundTimestamp)) {
1191
            throw new \InvalidArgumentException('Refund timestamp must be int');
1192
        }
1193
        if (!is_float($refundAmount) && !is_int($refundAmount)) {
1194
            throw new \InvalidArgumentException('Amount must be number');
1195
        }
1196
        if (!is_string($refundCurrency)) {
1197
            throw new \InvalidArgumentException('Refund currency must be string');
1198
        }
1199
        if ($refundAmountConverted !== null && !is_float($refundAmountConverted) && !is_int($refundAmountConverted)) {
1200
            throw new \InvalidArgumentException('Refund converted amount must be number');
1201
        }
1202
        if ($refundSource !== null && !is_string($refundSource)) {
1203
            throw new \InvalidArgumentException('Refund source must be string');
1204
        }
1205
        if ($refundType !== null && !is_string($refundType)) {
1206
            throw new \InvalidArgumentException('Refund type must be string');
1207
        }
1208
        if ($refundCode !== null && !is_string($refundCode)) {
1209
            throw new \InvalidArgumentException('Refund code must be string');
1210
        }
1211
        if ($refundReason !== null && !is_string($refundReason)) {
1212
            throw new \InvalidArgumentException('Refund reason must be string');
1213
        }
1214
        if ($agentId !== null && !is_string($agentId)) {
1215
            throw new \InvalidArgumentException('Agent id must be string');
1216
        }
1217
1218
        $this->replace('refund_id', $refundId);
1219
        $this->replace('refund_timestamp', $refundTimestamp);
1220
        $this->replace('refund_amount', $refundAmount);
1221
        $this->replace('refund_currency', $refundCurrency);
1222
        $this->replace('refund_amount_converted', $refundAmountConverted);
1223
        $this->replace('refund_source', $refundSource);
1224
        $this->replace('refund_type', $refundType);
1225
        $this->replace('refund_code', $refundCode);
1226
        $this->replace('refund_reason', $refundReason);
1227
        $this->replace('agent_id', $agentId);
1228
1229
        return $this;
1230
    }
1231
1232
    /**
1233
     * Adds custom data field to envelope
1234
     *
1235
     * @param string $name
1236
     * @param string $value
1237
     *
1238
     * @return $this
1239
     */
1240
    public function addCustomField($name, $value)
1241
    {
1242
        if (!is_string($name)) {
1243
            throw new \InvalidArgumentException('Custom field name must be string');
1244
        }
1245
        if (!is_string($value)) {
1246
            throw new \InvalidArgumentException('Custom field value must be string');
1247
        }
1248
1249
        if (strlen($name) < 8 || substr($name, 0, 7) !== 'custom_') {
1250
            $name = 'custom_' . $name;
1251
        }
1252
1253
        $this->replace($name, $value);
1254
        return $this;
1255
    }
1256
}
1257