createCrefoPayApiGuzzleResponse()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 5
nc 2
nop 1
dl 0
loc 9
rs 10
c 0
b 0
f 0
1
<?php
2
3
/**
4
 * MIT License
5
 * For full license information, please view the LICENSE file that was distributed with this source code.
6
 */
7
8
namespace SprykerEco\Zed\CrefoPayApi\Dependency\External\Guzzle;
9
10
use GuzzleHttp\Client;
11
use GuzzleHttp\Exception\RequestException;
12
use GuzzleHttp\RequestOptions;
13
use Psr\Http\Message\ResponseInterface;
14
use SprykerEco\Zed\CrefoPayApi\Dependency\External\Guzzle\Exception\CrefoPayApiGuzzleRequestException;
15
use SprykerEco\Zed\CrefoPayApi\Dependency\External\Guzzle\Response\CrefoPayApiGuzzleResponse;
16
use SprykerEco\Zed\CrefoPayApi\Dependency\External\Guzzle\Response\CrefoPayApiGuzzleResponseInterface;
17
18
class CrefoPayApiGuzzleHttpClientAdapter implements CrefoPayApiGuzzleHttpClientAdapterInterface
19
{
20
    /**
21
     * @var int
22
     */
23
    protected const DEFAULT_TIMEOUT = 45;
24
25
    /**
26
     * @var string
27
     */
28
    protected const HEADER_CONTENT_TYPE_KEY = 'Content-Type';
29
30
    /**
31
     * @var string
32
     */
33
    protected const HEADER_CONTENT_TYPE_VALUE = 'application/x-www-form-urlencoded';
34
35
    /**
36
     * @var \GuzzleHttp\Client
37
     */
38
    protected $guzzleHttpClient;
39
40
    public function __construct()
41
    {
42
        $this->guzzleHttpClient = new Client([
43
            RequestOptions::TIMEOUT => static::DEFAULT_TIMEOUT,
44
            RequestOptions::HEADERS => [
45
                static::HEADER_CONTENT_TYPE_KEY => static::HEADER_CONTENT_TYPE_VALUE,
46
            ],
47
        ]);
48
    }
49
50
    /**
51
     * @param string $url
52
     * @param array $formParams
53
     *
54
     * @throws \SprykerEco\Zed\CrefoPayApi\Dependency\External\Guzzle\Exception\CrefoPayApiGuzzleRequestException
55
     *
56
     * @return \SprykerEco\Zed\CrefoPayApi\Dependency\External\Guzzle\Response\CrefoPayApiGuzzleResponseInterface
57
     */
58
    public function post(string $url, array $formParams = []): CrefoPayApiGuzzleResponseInterface
59
    {
60
        try {
61
            $options = [
62
                RequestOptions::FORM_PARAMS => $formParams,
63
            ];
64
            $response = $this->guzzleHttpClient->post($url, $options);
65
        } catch (RequestException $requestException) {
66
            throw new CrefoPayApiGuzzleRequestException(
67
                $this->createCrefoPayApiGuzzleResponse($requestException->getResponse()),
68
                $requestException->getMessage(),
69
                $requestException->getCode(),
70
                $requestException,
71
            );
72
        }
73
74
        return $this->createCrefoPayApiGuzzleResponse($response);
75
    }
76
77
    /**
78
     * @param \Psr\Http\Message\ResponseInterface|null $response
79
     *
80
     * @return \SprykerEco\Zed\CrefoPayApi\Dependency\External\Guzzle\Response\CrefoPayApiGuzzleResponse
81
     */
82
    protected function createCrefoPayApiGuzzleResponse(?ResponseInterface $response): CrefoPayApiGuzzleResponse
83
    {
84
        if ($response === null) {
85
            return new CrefoPayApiGuzzleResponse();
86
        }
87
88
        return new CrefoPayApiGuzzleResponse(
89
            $response->getBody(),
90
            $response->getHeaders(),
91
        );
92
    }
93
}
94