Passed
Push — master ( 66ab3b...f2d26b )
by Cesar
12:58
created

ForwardApiService::directTokenization()   B

Complexity

Conditions 3
Paths 12

Size

Total Lines 84
Code Lines 59

Duplication

Lines 0
Ratio 0 %

Importance

Changes 3
Bugs 0 Features 0
Metric Value
cc 3
eloc 59
c 3
b 0
f 0
nc 12
nop 2
dl 0
loc 84
rs 8.8945

How to fix   Long Method   

Long Method

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

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

Commonly applied refactorings include:

1
<?php
2
3
namespace App\Service\Braintree;
4
5
use Exception;
6
7
/**
8
 * Class ForwardApiService
9
 * @package App\Service\Braintree
10
 */
11
class ForwardApiService extends AbstractBraintreeService
12
{
13
    /**
14
     * @var string
15
     */
16
    public string $ENDPOINT = "https://forwarding.sandbox.braintreegateway.com";
17
18
    /**
19
     * @param string $paymentNonce
20
     * @param string $deviceData
21
     * @return array|null
22
     */
23
    public function directTokenization(string $paymentNonce, string $deviceData): ?array
24
    {
25
        try {
26
            $ch = curl_init();
27
            curl_setopt($ch, CURLOPT_URL, $this->ENDPOINT);
28
            curl_setopt($ch, CURLOPT_HTTPHEADER, ['Content-Type:application/json']);
29
            curl_setopt(
30
                $ch,
31
                CURLOPT_USERPWD,
32
                $this->gateway->config->getPublicKey() . ':' . $this->gateway->config->getPrivateKey()
33
            );
34
            curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'POST');
35
            curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode([
36
                'debug_transformations' => true,
37
                'name' => 'default',
38
                'merchant_id' => $this->gateway->config->getMerchantId(),
39
                'payment_method_nonce' => $paymentNonce,
40
                'device_data' => $deviceData,
41
                'method' => 'POST',
42
                'data' => [
43
                    'card-id' => 'custom_card_id_1',
44
                    'open-id-token' => sha1(time()),
45
                ],
46
                'url' => 'https://test.com',
47
                'config' => [
48
                    'name' => 'default',
49
                    'url' => '^https://test.com',
50
                    'methods' => ['POST'],
51
                    'types' => ['NetworkTokenizedCard'],
52
                    'request_format' => [
53
                        'body' => 'json'
54
                    ],
55
                    'transformations' => [
56
                        [
57
                            'path' => '/body/card/pan',
58
                            'value' => '$number'
59
                        ],
60
                        [
61
                            'path' => '/body/card/card-cvv',
62
                            'value' => '$cvv'
63
                        ],
64
                        [
65
                            'path' => '/body/card/expiration-month',
66
                            'value' => '$expiration_month'
67
                        ],
68
                        [
69
                            'path' => '/body/card/expiration-year',
70
                            'value' => '$expiration_year'
71
                        ],
72
                        [
73
                            'path' => '/body/custom-fields/card-id',
74
                            'value' => '$card-id'
75
                        ],
76
                        [
77
                            'path' => '/header/Authorization',
78
                            'value' => '$open-id-token'
79
                        ]
80
                    ]
81
82
                ]
83
            ], true));
0 ignored issues
show
Bug introduced by
true of type true is incompatible with the type integer expected by parameter $flags of json_encode(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

83
            ], /** @scrutinizer ignore-type */ true));
Loading history...
84
85
            curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
86
            curl_setopt($ch, CURLOPT_HEADERFUNCTION, function ($curl, $header) use (&$headers) {
87
                $len = strlen($header);
88
                $header = explode(':', $header, 2);
89
                if (count($header) < 2) {
90
                    return $len;
91
                }
92
                $headers[(trim($header[0]))] = trim($header[1]);
93
                return $len;
94
            });
95
            $result = curl_exec($ch);
96
            $statusCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
97
98
            curl_close($ch);
99
        } catch (Exception $e) {
100
            $this->logger->error('Error on ForwardAPI::'.$this->ENDPOINT.' = ' . $e->getMessage());
101
            return null;
102
        }
103
        return ([
104
            'headers' => (object) $headers,
105
            'body' => (object) json_decode($result),
0 ignored issues
show
Bug introduced by
It seems like $result can also be of type true; however, parameter $json of json_decode() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

105
            'body' => (object) json_decode(/** @scrutinizer ignore-type */ $result),
Loading history...
106
            'statusCode' => $statusCode
107
        ]);
108
    }
109
}
110