FirstDataGuzzleHttpClientAdapter::post()   A
last analyzed

Complexity

Conditions 2
Paths 3

Size

Total Lines 19
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 13
c 1
b 0
f 0
dl 0
loc 19
rs 9.8333
cc 2
nc 3
nop 3
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\FirstData\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\FirstData\Dependency\External\Guzzle\Exception\FirstDataGuzzleRequestException;
15
use SprykerEco\Zed\FirstData\Dependency\External\Guzzle\Response\FirstDataGuzzleResponse;
16
use SprykerEco\Zed\FirstData\Dependency\External\Guzzle\Response\FirstDataGuzzleResponseInterface;
17
18
class FirstDataGuzzleHttpClientAdapter implements FirstDataGuzzleHttpClientAdapterInterface
19
{
20
    protected const DEFAULT_TIMEOUT = 45;
21
    protected const HEADER_CONTENT_TYPE_KEY = 'Content-Type';
22
    protected const HEADER_CONTENT_TYPE_VALUE = 'application/json';
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
        ]);
34
    }
35
36
    /**
37
     * @param string $url
38
     * @param array $headers
39
     * @param string $body
40
     *
41
     * @throws \SprykerEco\Zed\FirstData\Dependency\External\Guzzle\Exception\FirstDataGuzzleRequestException
42
     *
43
     * @return \SprykerEco\Zed\FirstData\Dependency\External\Guzzle\Response\FirstDataGuzzleResponseInterface
44
     */
45
    public function post(string $url, array $headers = [], string $body = ''): FirstDataGuzzleResponseInterface
46
    {
47
        try {
48
            $headers[static::HEADER_CONTENT_TYPE_KEY] = static::HEADER_CONTENT_TYPE_VALUE;
49
            $options = [
50
                RequestOptions::BODY => $body,
51
                RequestOptions::HEADERS => $headers,
52
            ];
53
            $response = $this->guzzleHttpClient->post($url, $options);
54
        } catch (RequestException $requestException) {
55
            throw new FirstDataGuzzleRequestException(
56
                $this->createFirstDataGuzzleResponse($requestException->getResponse()),
57
                $requestException->getMessage(),
58
                $requestException->getCode(),
59
                $requestException
60
            );
61
        }
62
63
        return $this->createFirstDataGuzzleResponse($response);
64
    }
65
66
    /**
67
     * @param \Psr\Http\Message\ResponseInterface|null $response
68
     *
69
     * @return \SprykerEco\Zed\FirstData\Dependency\External\Guzzle\Response\FirstDataGuzzleResponseInterface
70
     */
71
    protected function createFirstDataGuzzleResponse(?ResponseInterface $response): FirstDataGuzzleResponseInterface
72
    {
73
        if ($response === null) {
74
            return new FirstDataGuzzleResponse();
75
        }
76
77
        return new FirstDataGuzzleResponse(
78
            $response->getBody(),
79
            $response->getHeaders()
80
        );
81
    }
82
}
83