AbstractHttpAdapter   A
last analyzed

Complexity

Total Complexity 18

Size/Duplication

Total Lines 179
Duplicated Lines 0 %

Importance

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

12 Methods

Rating   Name   Duplication   Size   Complexity  
A getTimeout() 0 3 1
A sendRawRequest() 0 6 1
A __construct() 0 3 1
A setTimeout() 0 3 1
A sendRequest() 0 10 2
A generateUrlArray() 0 6 1
A getUrl() 0 3 1
A getRawResponse() 0 3 1
A parseResponse() 0 27 6
A getParams() 0 3 1
A setParams() 0 3 1
A setRawResponse() 0 3 1
1
<?php
2
3
/**
4
 * MIT License
5
 * Use of this software requires acceptance of the Evaluation License Agreement. See LICENSE file.
6
 */
7
8
namespace SprykerEco\Zed\Payone\Business\Api\Adapter\Http;
9
10
use SprykerEco\Zed\Payone\Business\Api\Adapter\AdapterInterface;
11
use SprykerEco\Zed\Payone\Business\Api\Request\Container\AbstractRequestContainer;
12
use SprykerEco\Zed\Payone\Business\Exception\TimeoutException;
13
14
abstract class AbstractHttpAdapter implements AdapterInterface
15
{
16
    public const DEFAULT_TIMEOUT = 45;
17
18
    /**
19
     * @var int
20
     */
21
    protected $timeout = self::DEFAULT_TIMEOUT;
22
23
    /**
24
     * @var string
25
     */
26
    protected $url;
27
28
    /**
29
     * @var array
30
     */
31
    protected $params = [];
32
33
    /**
34
     * @var string
35
     */
36
    protected $rawResponse;
37
38
    /**
39
     * @param string $paymentGatewayUrl
40
     */
41
    public function __construct($paymentGatewayUrl)
42
    {
43
        $this->url = $paymentGatewayUrl;
44
    }
45
46
    /**
47
     * @param int $timeout
48
     *
49
     * @return void
50
     */
51
    public function setTimeout($timeout)
52
    {
53
        $this->timeout = $timeout;
54
    }
55
56
    /**
57
     * @return int
58
     */
59
    public function getTimeout()
60
    {
61
        return $this->timeout;
62
    }
63
64
    /**
65
     * @param array $params
66
     *
67
     * @return array
68
     */
69
    public function sendRawRequest(array $params): array
70
    {
71
        $rawResponse = $this->performRequest($params);
72
        $result = $this->parseResponse($rawResponse);
73
74
        return $result;
75
    }
76
77
    /**
78
     * @param \SprykerEco\Zed\Payone\Business\Api\Request\Container\AbstractRequestContainer $container
79
     * @param array $additionalParams
80
     *
81
     * @return array
82
     */
83
    public function sendRequest(AbstractRequestContainer $container, array $additionalParams = []): array
84
    {
85
        try {
86
            $requestParams = array_merge($container->toArray(), $additionalParams);
87
            ksort($requestParams);
88
89
            return $this->sendRawRequest($requestParams);
90
        } catch (TimeoutException $exception) {
91
            return [
92
                'status' => 'TIMEOUT',
93
            ];
94
        }
95
    }
96
97
    /**
98
     * @param array $params
99
     *
100
     * @return array
101
     */
102
    abstract protected function performRequest(array $params);
103
104
    /**
105
     * @param array $params
106
     *
107
     * @return array
108
     */
109
    protected function generateUrlArray(array $params)
110
    {
111
        $urlRequest = $this->getUrl() . '?' . http_build_query($params, '', '&');
112
        $urlArray = parse_url($urlRequest);
113
114
        return $urlArray;
115
    }
116
117
    /**
118
     * @param array $responseRaw
119
     *
120
     * @return array
121
     */
122
    protected function parseResponse(array $responseRaw = [])
123
    {
124
        $result = [];
125
126
        if (count($responseRaw) === 0) {
127
            return $result;
128
        } elseif (strpos($responseRaw[0], '%PDF-') !== false) {
129
            return ['rawResponse' => base64_encode(implode("\n", array_values($responseRaw)))];
130
        }
131
132
        foreach ($responseRaw as $key => $line) {
133
            $pos = strpos($line, '=');
134
135
            if ($pos === false) {
136
                if (strlen($line) > 0) {
137
                    $result[$key] = $line;
138
                }
139
140
                continue;
141
            }
142
143
            $lineArray = explode('=', $line);
144
            $resultKey = array_shift($lineArray);
145
            $result[$resultKey] = implode('=', $lineArray);
146
        }
147
148
        return $result;
149
    }
150
151
    /**
152
     * @return string
153
     */
154
    public function getUrl(): string
155
    {
156
        return $this->url;
157
    }
158
159
    /**
160
     * @param array $params
161
     *
162
     * @return void
163
     */
164
    public function setParams(array $params)
165
    {
166
        $this->params = $params;
167
    }
168
169
    /**
170
     * @return array
171
     */
172
    public function getParams()
173
    {
174
        return $this->params;
175
    }
176
177
    /**
178
     * @return string
179
     */
180
    public function getRawResponse(): string
181
    {
182
        return $this->rawResponse;
183
    }
184
185
    /**
186
     * @param string $rawResponse
187
     *
188
     * @return void
189
     */
190
    protected function setRawResponse($rawResponse)
191
    {
192
        $this->rawResponse = $rawResponse;
193
    }
194
}
195