Completed
Pull Request — master (#3)
by Oleksandr
32:39 queued 22:11
created

GuzzleAdapter   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 44
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 3
eloc 14
dl 0
loc 44
c 0
b 0
f 0
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A sendGetRequest() 0 13 2
A __construct() 0 6 1
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\Episerver\Business\Api\Adapter\Http;
9
10
use GuzzleHttp\Client;
11
use GuzzleHttp\Exception\RequestException;
12
use GuzzleHttp\RequestOptions;
13
use Psr\Http\Message\ResponseInterface;
14
use SprykerEco\Zed\Episerver\Business\Exception\ApiHttpRequestException;
15
use SprykerEco\Zed\Episerver\EpiserverConfig;
16
17
class GuzzleAdapter implements HttpAdapterInterface
18
{
19
    /**
20
     * @var \SprykerEco\Zed\Episerver\EpiserverConfig
21
     */
22
    protected $config;
23
24
    /**
25
     * @var \GuzzleHttp\Client
26
     */
27
    protected $client;
28
29
    /**
30
     * @param \SprykerEco\Zed\Episerver\EpiserverConfig $config
31
     */
32
    public function __construct(EpiserverConfig $config)
33
    {
34
        $this->config = $config;
35
36
        $this->client = new Client([
37
            RequestOptions::TIMEOUT => $this->config->getRequestTimeout(),
38
        ]);
39
    }
40
41
    /**
42
     * @param string $gatewayUrl
43
     *
44
     * @throws \SprykerEco\Zed\Episerver\Business\Exception\ApiHttpRequestException
45
     *
46
     * @return \Psr\Http\Message\ResponseInterface
47
     */
48
    public function sendGetRequest(string $gatewayUrl): ResponseInterface
49
    {
50
        try {
51
            $response = $this->client->get($gatewayUrl);
52
        } catch (RequestException $requestException) {
53
            throw new ApiHttpRequestException(
54
                $requestException->getMessage(),
55
                $requestException->getCode(),
56
                $requestException->getPrevious()
57
            );
58
        }
59
60
        return $response;
61
    }
62
}
63