Passed
Push — feature/eco-2295/eco-2344-crea... ( 425ba1...d28970 )
by Aleksey
04:38
created

createCrefoPayApiGuzzleResponse()   A

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
    protected const DEFAULT_TIMEOUT = 45;
21
    protected const HEADER_CONTENT_TYPE_KEY = 'Content-Type';
22
    protected const HEADER_CONTENT_TYPE_VALUE = 'application/x-www-form-urlencoded';
23
24
    /**
25
     * @var \GuzzleHttp\Client
26
     */
27
    protected $guzzleHttpClient;
28
29
    public function __construct()
30
    {
31
        $this->guzzleHttpClient = new Client([
32
            RequestOptions::TIMEOUT => static::DEFAULT_TIMEOUT,
33
            RequestOptions::HEADERS => [
34
                static::HEADER_CONTENT_TYPE_KEY => static::HEADER_CONTENT_TYPE_VALUE,
35
            ],
36
        ]);
37
    }
38
39
    /**
40
     * @param string $url
41
     * @param array $formParams
42
     *
43
     * @throws \SprykerEco\Zed\CrefoPayApi\Dependency\External\Guzzle\Exception\CrefoPayApiGuzzleRequestException
44
     *
45
     * @return \SprykerEco\Zed\CrefoPayApi\Dependency\External\Guzzle\Response\CrefoPayApiGuzzleResponseInterface
46
     */
47
    public function post(string $url, array $formParams = []): CrefoPayApiGuzzleResponseInterface
48
    {
49
        try {
50
            $options = [
51
                RequestOptions::FORM_PARAMS => $formParams,
52
            ];
53
            $response = $this->guzzleHttpClient->post($url, $options);
54
        } catch (RequestException $requestException) {
55
            throw new CrefoPayApiGuzzleRequestException(
56
                $this->createCrefoPayApiGuzzleResponse($requestException->getResponse()),
57
                $requestException->getMessage(),
58
                $requestException->getCode(),
59
                $requestException
60
            );
61
        }
62
63
        return $this->createCrefoPayApiGuzzleResponse($response);
64
    }
65
66
    /**
67
     * @param \Psr\Http\Message\ResponseInterface|null $response
68
     *
69
     * @return \SprykerEco\Zed\CrefoPayApi\Dependency\External\Guzzle\Response\CrefoPayApiGuzzleResponse
70
     */
71
    protected function createCrefoPayApiGuzzleResponse(?ResponseInterface $response): CrefoPayApiGuzzleResponse
72
    {
73
        if ($response === null) {
74
            return new CrefoPayApiGuzzleResponse();
75
        }
76
77
        return new CrefoPayApiGuzzleResponse(
78
            $response->getBody(),
79
            $response->getHeaders()
80
        );
81
    }
82
}
83