ForwardApiService::directTokenization()   B
last analyzed

Complexity

Conditions 3
Paths 13

Size

Total Lines 86
Code Lines 61

Duplication

Lines 0
Ratio 0 %

Importance

Changes 4
Bugs 0 Features 0
Metric Value
cc 3
eloc 61
c 4
b 0
f 0
nc 13
nop 2
dl 0
loc 86
rs 8.8509

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
            $body = [
27
                'debug_transformations' => true,
28
                'name' => 'default',
29
                'merchant_id' => $this->gateway->config->getMerchantId(),
30
                'payment_method_nonce' => $paymentNonce,
31
                'device_data' => $deviceData,
32
                'method' => 'POST',
33
                'data' => [
34
                    'card-id' => 'custom_card_id_1',
35
                    'open-id-token' => sha1(time()),
36
                ],
37
                'url' => 'https://test.com',
38
                'config' => [
39
                    'name' => 'default',
40
                    'url' => '^https://test.com',
41
                    'methods' => ['POST'],
42
                    'types' => ['NetworkTokenizedCard'],
43
                    'request_format' => [
44
                        'body' => 'json'
45
                    ],
46
                    'transformations' => [
47
                        [
48
                            'path' => '/body/card/pan',
49
                            'value' => '$number'
50
                        ],
51
                        [
52
                            'path' => '/body/card/card-cvv',
53
                            'value' => '$cvv'
54
                        ],
55
                        [
56
                            'path' => '/body/card/expiration-month',
57
                            'value' => '$expiration_month'
58
                        ],
59
                        [
60
                            'path' => '/body/card/expiration-year',
61
                            'value' => '$expiration_year'
62
                        ],
63
                        [
64
                            'path' => '/body/custom-fields/card-id',
65
                            'value' => '$card-id'
66
                        ],
67
                        [
68
                            'path' => '/header/Authorization',
69
                            'value' => '$open-id-token'
70
                        ]
71
                    ]
72
73
                ]
74
            ];
75
            $ch = curl_init();
76
            curl_setopt($ch, CURLOPT_URL, $this->ENDPOINT);
77
            curl_setopt($ch, CURLOPT_HTTPHEADER, ['Content-Type:application/json']);
78
            curl_setopt(
79
                $ch,
80
                CURLOPT_USERPWD,
81
                $this->gateway->config->getPublicKey() . ':' . $this->gateway->config->getPrivateKey()
82
            );
83
            curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'POST');
84
            curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($body, 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

84
            curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($body, /** @scrutinizer ignore-type */ true));
Loading history...
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
            'request' => $body,
105
            'response' => [
106
                'headers' => (object) $headers,
107
                '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

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