Passed
Push — master ( c1f9a4...4b0053 )
by Rimas
02:42 queued 32s
created

GuzzleClientAdapter::requestBody()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 2
dl 0
loc 8
c 0
b 0
f 0
rs 10
cc 1
nc 1
nop 3
1
<?php
2
namespace Dokobit\Gateway\Http;
3
4
use GuzzleHttp;
5
use GuzzleHttp\Exception\BadResponseException;
6
use Dokobit\Gateway\Exception;
7
use Psr\Http\Message\ResponseInterface;
8
9
/**
10
 * Adapter for GuzzleHttp client
11
 */
12
class GuzzleClientAdapter implements ClientInterface
13
{
14
    /** @var GuzzleHttp\ClientInterface */
15
    protected $client;
16
17
    /**
18
     * @param GuzzleHttp\ClientInterface $client
19
     * @return self
20
     */
21
    public function __construct(GuzzleHttp\ClientInterface $client)
22
    {
23
        $this->client = $client;
24
    }
25
26
    /**
27
     * Send HTTP request and return response body
28
     * @param string $method POST|GET
29
     * @param string $url http URL
30
     * @param array $options query options. Query values goes under 'body' key.
31
     *         Example:
32
     *         $options = [
33
     *             'query' => [
34
     *                 'access_token' => 'foobar',
35
     *             ],
36
     *             'body' => [
37
     *                 'param1' => 'value1',
38
     *                 'param2' => 'value2',
39
     *             ]
40
     *         ]
41
     * @return string
42
     */
43
    public function requestBody(
44
        string $method,
45
        string $url,
46
        array $options = []
47
    ): string {
48
        $response = $this->sendRequest($method, $url, $options);
49
50
        return (string)$response->getBody();
51
    }
52
53
    /**
54
     * Send HTTP request and return response JSON parsed into array
55
     * @param string $method POST|GET
56
     * @param string $url http URL
57
     * @param array $options query options. Query values goes under 'body' key.
58
     *         Example:
59
     *         $options = [
60
     *             'query' => [
61
     *                 'access_token' => 'foobar',
62
     *             ],
63
     *             'body' => [
64
     *                 'param1' => 'value1',
65
     *                 'param2' => 'value2',
66
     *             ]
67
     *         ]
68
     * @return array
69
     */
70
    public function requestJson(
71
        string $method,
72
        string $url,
73
        array $options = []
74
    ): ?array {
75
        $response = $this->sendRequest($method, $url, $options);
76
        $response = json_decode($response->getBody()->getContents(), true);
77
78
        return $response;
79
    }
80
81
    /**
82
     * Actually send the HTTP request and return its response object
83
     * @param string $method POST|GET
84
     * @param string $url http URL
85
     * @param array $options query options. Query values goes under 'body' key.
86
     *         Example:
87
     *         $options = [
88
     *             'query' => [
89
     *                 'access_token' => 'foobar',
90
     *             ],
91
     *             'body' => [
92
     *                 'param1' => 'value1',
93
     *                 'param2' => 'value2',
94
     *             ]
95
     *         ]
96
     * @return ResponseInterface
97
     */
98
    protected function sendRequest(
99
        string $method,
100
        string $url,
101
        array $options = []
102
    ): ResponseInterface {
103
        try {
104
            $response = $this->client->request($method, $url, $options);
105
106
            return $response;
107
        } catch (BadResponseException $e) {
108
            if ($e->getCode() == 400) {
109
                throw new Exception\InvalidData(
110
                    'Data validation failed',
111
                    400,
112
                    $e,
113
                    json_decode($e->getResponse()->getBody()->getContents(), true)
114
                );
115
            } elseif ($e->getCode() == 403) {
116
                throw new Exception\InvalidApiKey(
117
                    'Access forbidden. Invalid API key.',
118
                    403,
119
                    $e,
120
                    $e->getResponse()->getBody()
121
                );
122
            } elseif ($e->getCode() == 404) {
123
                throw new Exception\NotFound(
124
                    'Requested URL was not found.',
125
                    404,
126
                    $e,
127
                    $e->getResponse()->getBody()
128
                );
129
            } elseif ($e->getCode() == 500) {
130
                throw new Exception\ServerError(
131
                    'Error occurred on server side while handling request',
132
                    500,
133
                    $e,
134
                    $e->getResponse()->getBody()
135
                );
136
            } elseif ($e->getCode() == 504) {
137
                throw new Exception\Timeout(
138
                    'Request timeout',
139
                    504,
140
                    $e,
141
                    $e->getResponse()->getBody()
142
                );
143
            } else {
144
                throw new Exception\UnexpectedResponse(
145
                    'Unexpected error occurred',
146
                    $e->getCode(),
147
                    $e,
148
                    $e->getResponse()->getBody()
149
                );
150
            }
151
        } catch (\Exception $e) {
152
            throw new Exception\UnexpectedError('Unexpected error occurred', 0, $e);
153
        }
154
    }
155
}
156