Completed
Push — master ( 8f1019...13ce36 )
by Žilvinas
04:20 queued 01:35
created

GuzzleClientAdapter::requestJson()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 8
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 3
1
<?php
2
namespace Isign\Gateway\Http;
3
4
use GuzzleHttp;
5
use GuzzleHttp\Message\ResponseInterface;
6
use GuzzleHttp\Exception\BadResponseException;
7
use Isign\Gateway\Exception;
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 type 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 $response->getBody();
0 ignored issues
show
Bug Best Practice introduced by
The expression return $response->getBody() could return the type GuzzleHttp\Stream\StreamInterface which is incompatible with the type-hinted return null|string. Consider adding an additional type-check to rule them out.
Loading history...
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
77
        return $response->json();
78
    }
79
80
    /**
81
     * Actually send the HTTP request and return its response object
82
     * @param string $method POST|GET
83
     * @param string $url http URL
84
     * @param array $options query options. Query values goes under 'body' key.
85
     *         Example:
86
     *         $options = [
87
     *             'query' => [
88
     *                 'access_token' => 'foobar',
89
     *             ],
90
     *             'body' => [
91
     *                 'param1' => 'value1',
92
     *                 'param2' => 'value2',
93
     *             ]
94
     *         ]
95
     * @return ResponseInterface
96
     */
97
    protected function sendRequest(
98
        string $method,
99
        string $url,
100
        array $options = [],
101
        bool $expectJson = true
0 ignored issues
show
Unused Code introduced by
The parameter $expectJson is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

101
        /** @scrutinizer ignore-unused */ bool $expectJson = true

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
102
    ): ResponseInterface {
103
        $result = [];
0 ignored issues
show
Unused Code introduced by
The assignment to $result is dead and can be removed.
Loading history...
104
105
        try {
106
            $response = $this->client->send(
107
                $this->client->createRequest($method, $url, $options)
108
            );
109
            return $response;
110
        } catch (BadResponseException $e) {
111
            if ($e->getCode() == 400) {
112
                throw new Exception\InvalidData(
113
                    'Data validation failed',
114
                    400,
115
                    $e,
116
                    $e->getResponse()->json()
117
                );
118
            } elseif ($e->getCode() == 403) {
119
                throw new Exception\InvalidApiKey(
120
                    'Access forbidden. Invalid API key.',
121
                    403,
122
                    $e,
123
                    $e->getResponse()->getBody()
124
                );
125
            } elseif ($e->getCode() == 404) {
126
                throw new Exception\NotFound(
127
                    'Requested URL was not found.',
128
                    404,
129
                    $e,
130
                    $e->getResponse()->getBody()
131
                );
132
            } elseif ($e->getCode() == 500) {
133
                throw new Exception\ServerError(
134
                    'Error occurred on server side while handling request',
135
                    500,
136
                    $e,
137
                    $e->getResponse()->getBody()
138
                );
139
            } elseif ($e->getCode() == 504) {
140
                throw new Exception\Timeout(
141
                    'Request timeout',
142
                    504,
143
                    $e,
144
                    $e->getResponse()->getBody()
145
                );
146
            } else {
147
                throw new Exception\UnexpectedResponse(
148
                    'Unexpected error occurred',
149
                    $e->getCode(),
150
                    $e,
151
                    $e->getResponse()->getBody()
152
                );
153
            }
154
        } catch (\Exception $e) {
155
            throw new Exception\UnexpectedError('Unexpected error occurred', 0, $e);
156
        }
157
    }
158
}
159