CreateOrderAuthorizeClient   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 100
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 42
c 1
b 0
f 0
dl 0
loc 100
rs 10
wmc 4

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 10 1
A placeRequest() 0 53 3
1
<?php
2
/**
3
 * Copyright © Wirecard Brasil. All rights reserved.
4
 *
5
 * @author    Bruno Elisei <[email protected]>
6
 * See COPYING.txt for license details.
7
 */
8
9
declare(strict_types=1);
10
11
namespace Moip\Magento2\Gateway\Http\Client;
12
13
use InvalidArgumentException;
14
use Magento\Framework\HTTP\ZendClient;
0 ignored issues
show
Bug introduced by
The type Magento\Framework\HTTP\ZendClient 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...
15
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...
16
use Magento\Framework\Serialize\Serializer\Json;
0 ignored issues
show
Bug introduced by
The type Magento\Framework\Serialize\Serializer\Json 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...
17
use Magento\Payment\Gateway\Http\ClientInterface;
0 ignored issues
show
Bug introduced by
The type Magento\Payment\Gateway\Http\ClientInterface 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\Payment\Gateway\Http\TransferInterface;
0 ignored issues
show
Bug introduced by
The type Magento\Payment\Gateway\Http\TransferInterface 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...
19
use Magento\Payment\Model\Method\Logger;
0 ignored issues
show
Bug introduced by
The type Magento\Payment\Model\Method\Logger 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...
20
use Moip\Magento2\Gateway\Config\Config;
21
22
/**
23
 * Class CreateOrderAuthorizeClient - Returns authorization for order creation.
24
 */
25
class CreateOrderAuthorizeClient implements ClientInterface
26
{
27
    /**
28
     * @var LoggerInterface
0 ignored issues
show
Bug introduced by
The type Moip\Magento2\Gateway\Http\Client\LoggerInterface 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...
29
     */
30
    private $logger;
31
32
    /**
33
     * @var ZendClientFactory
34
     */
35
    private $httpClientFactory;
36
37
    /**
38
     * @var Config
39
     */
40
    private $config;
41
42
    /**
43
     * @var Json
44
     */
45
    private $json;
46
47
    /**
48
     * @param Logger            $logger
49
     * @param ZendClientFactory $httpClientFactory
50
     * @param Config            $config
51
     * @param Json              $json
52
     */
53
    public function __construct(
54
        Logger $logger,
55
        ZendClientFactory $httpClientFactory,
56
        Config $config,
57
        Json $json
58
    ) {
59
        $this->config = $config;
60
        $this->httpClientFactory = $httpClientFactory;
61
        $this->logger = $logger;
62
        $this->json = $json;
63
    }
64
65
    /**
66
     * Places request to gateway.
67
     *
68
     * @param TransferInterface $transferObject
69
     *
70
     * @return array
71
     */
72
    public function placeRequest(TransferInterface $transferObject)
73
    {
74
        $client = $this->httpClientFactory->create();
75
        $request = $transferObject->getBody();
76
        $url = $this->config->getApiUrl();
77
        $apiBearer = $this->config->getMerchantGatewayOauth();
78
79
        try {
80
            $client->setUri($url.'orders');
81
            $client->setConfig(['maxredirects' => 0, 'timeout' => 120]);
82
            $client->setHeaders('Authorization', 'Bearer '.$apiBearer);
83
            $client->setRawData($this->json->serialize($request), 'application/json');
84
            $client->setMethod(ZendClient::POST);
85
86
            $responseBody = $client->request()->getBody();
87
            $data = $this->json->unserialize($responseBody);
88
            if (isset($data['id'])) {
89
                $response = array_merge(
90
                    [
91
                        'RESULT_CODE'     => 1,
92
                        'EXT_ORD_ID'      => $data['id'],
93
                    ],
94
                    $data
95
                );
96
            } else {
97
                $response = array_merge(
98
                    [
99
                        'RESULT_CODE' => 0,
100
                    ],
101
                    $data
102
                );
103
            }
104
            $this->logger->debug(
105
                [
106
                    'url'      => $url.'orders',
107
                    'request'  => $this->json->serialize($transferObject->getBody()),
108
                    'response' => $responseBody,
109
                ]
110
            );
111
        } catch (InvalidArgumentException $e) {
112
            $this->logger->debug(
113
                [
114
                    'exception' => $e->getMessage(),
115
                    'url'       => $url.'orders',
116
                    'request'   => $this->json->serialize($transferObject->getBody()),
117
                    'response'  => $responseBody,
118
                ]
119
            );
120
            // phpcs:ignore Magento2.Exceptions.DirectThrow
121
            throw new \Exception('Invalid JSON was returned by the gateway');
122
        }
123
124
        return $response;
125
    }
126
}
127