Passed
Branch [email protected] (3810d0)
by Bruno
11:09
created

CreateOrderPaymentTwoCcClient::placeRequest()   B

Complexity

Conditions 6
Paths 27

Size

Total Lines 68
Code Lines 38

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 6
eloc 38
c 1
b 0
f 0
nc 27
nop 1
dl 0
loc 68
rs 8.6897

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
 * Copyright © Getnet. All rights reserved.
4
 *
5
 * @author    Bruno Elisei <[email protected]>
6
 * See LICENSE for license details.
7
 */
8
9
declare(strict_types=1);
10
11
namespace Getnet\PaymentMagento\Gateway\Http\Client;
12
13
use Exception;
14
use Getnet\PaymentMagento\Gateway\Config\Config;
15
use InvalidArgumentException;
16
use Magento\Framework\HTTP\ZendClient;
17
use Magento\Framework\HTTP\ZendClientFactory;
0 ignored issues
show
Bug introduced by
The type Magento\Framework\HTTP\ZendClientFactory was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
18
use Magento\Framework\Serialize\Serializer\Json;
19
use Magento\Payment\Gateway\Http\ClientInterface;
20
use Magento\Payment\Gateway\Http\TransferInterface;
21
use Magento\Payment\Model\Method\Logger;
22
23
/**
24
 * Class Create Order Payment Two Cc Client - create authorization for payment by Cc.
25
 *
26
 * @SuppressWarnings(PHPCPD)
27
 */
28
class CreateOrderPaymentTwoCcClient implements ClientInterface
29
{
30
    /**
31
     * Result Code - Block name.
32
     */
33
    public const RESULT_CODE = 'RESULT_CODE';
34
35
    /**
36
     * External Order Id - Block name.
37
     */
38
    public const EXT_ORD_ID = 'EXT_ORD_ID';
39
40
    /**
41
     * @var Logger
42
     */
43
    private $logger;
44
45
    /**
46
     * @var ZendClientFactory
47
     */
48
    private $httpClientFactory;
49
50
    /**
51
     * @var Config
52
     */
53
    private $config;
54
55
    /**
56
     * @var Json
57
     */
58
    private $json;
59
60
    /**
61
     * @param Logger            $logger
62
     * @param ZendClientFactory $httpClientFactory
63
     * @param Config            $config
64
     * @param Json              $json
65
     */
66
    public function __construct(
67
        Logger $logger,
68
        ZendClientFactory $httpClientFactory,
69
        Config $config,
70
        Json $json
71
    ) {
72
        $this->config = $config;
73
        $this->httpClientFactory = $httpClientFactory;
74
        $this->logger = $logger;
75
        $this->json = $json;
76
    }
77
78
    /**
79
     * Places request to gateway.
80
     *
81
     * @param TransferInterface $transferObject
82
     *
83
     * @return array
84
     */
85
    public function placeRequest(TransferInterface $transferObject)
86
    {
87
        /** @var ZendClient $client */
88
        $isSuccess = false;
89
        $client = $this->httpClientFactory->create();
90
        $request = $transferObject->getBody();
91
        $url = $this->config->getApiUrl();
92
        $apiBearer = $this->config->getMerchantGatewayOauth();
93
94
        try {
95
            $client->setUri($url.'v1/payments/combined');
96
            $client->setConfig(['maxredirects' => 0, 'timeout' => 45000]);
97
            $client->setHeaders(
98
                [
99
                    'Authorization' => 'Bearer '.$apiBearer,
100
                ]
101
            );
102
            $client->setRawData($this->json->serialize($request), 'application/json');
103
            $client->setMethod(ZendClient::POST);
104
105
            $responseBody = $client->request()->getBody();
106
            $data = $this->json->unserialize($responseBody);
107
            $response = array_merge(
108
                [
109
                    self::RESULT_CODE  => 0,
110
                ],
111
                $data
112
            );
113
114
            if (isset($data['payments'])) {
115
                foreach ($data['payments'] as $payment) {
116
                    if (isset($payment['payment_id'])) {
117
                        $isSuccess = true;
118
                    }
119
                }
120
            }
121
122
            if ($isSuccess) {
123
                $response = array_merge(
124
                    [
125
                        self::RESULT_CODE => 1,
126
                        self::EXT_ORD_ID  => $data['combined_id'],
127
                    ],
128
                    $data
129
                );
130
            }
131
132
            $this->logger->debug(
133
                [
134
                    'url'      => $url.'v1/payments/combined',
135
                    'request'  => $this->json->serialize($transferObject->getBody()),
136
                    'response' => $responseBody,
137
                ]
138
            );
139
        } catch (InvalidArgumentException $e) {
140
            $this->logger->debug(
141
                [
142
                    'exception' => $e->getMessage(),
143
                    'url'       => $url.'v1/payments/combined',
144
                    'request'   => $this->json->serialize($transferObject->getBody()),
145
                    'response'  => $responseBody,
146
                ]
147
            );
148
            // phpcs:ignore Magento2.Exceptions.DirectThrow
149
            throw new Exception('Invalid JSON was returned by the gateway');
150
        }
151
152
        return $response;
153
    }
154
}
155