Completed
Pull Request — master (#4)
by
unknown
02:34
created

GuzzleClientAdapter::sendRequest()   C

Complexity

Conditions 9
Paths 23

Size

Total Lines 66
Code Lines 48

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 66
rs 6.4099
c 0
b 0
f 0
cc 9
eloc 48
nc 23
nop 4

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
namespace Isign\Gateway\Http;
3
4
use GuzzleHttp;
5
use GuzzleHttp\Exception\BadResponseException;
6
use Isign\Gateway\Exception;
7
8
/**
9
 * Adapter for GuzzleHttp client
10
 */
11
class GuzzleClientAdapter implements ClientInterface
12
{
13
    /** @var GuzzleHttp\ClientInterface */
14
    protected $client;
15
16
    /**
17
     * @param type GuzzleHttp\ClientInterface $client
18
     * @return self
19
     */
20
    public function __construct(GuzzleHttp\ClientInterface $client)
21
    {
22
        $this->client = $client;
23
    }
24
25
    /**
26
     * Send HTTP request
27
     * @param string $method POST|GET
28
     * @param string $url http URL
29
     * @param array $options query options. Query values goes under 'body' key.
30
     * Example:
31
     * $options = [
32
     *     'query' => [
33
     *         'access_token' => 'foobar',
34
     *     ],
35
     *     'body' => [
36
     *         'param1' => 'value1',
37
     *         'param2' => 'value2',
38
     *     ]
39
     * ]
40
     * @param bool $expectJson whether JSON response is expected
41
     * @return array
42
     */
43
    public function sendRequest(
44
        string $method,
45
        string $url,
46
        array $options = [],
47
        bool $expectJson = true
48
    ): array {
49
        $result = [];
50
51
        try {
52
            $response = $this->client->send(
53
                $this->client->createRequest($method, $url, $options)
54
            );
55
            if ($expectJson) {
56
                $result = $response->json();
57
            } else {
58
                $result = [ 'body' => $response->getBody() ];
59
            }
60
        } catch (BadResponseException $e) {
61
            if ($e->getCode() == 400) {
62
                throw new Exception\InvalidData(
63
                    'Data validation failed',
64
                    400,
65
                    $e,
66
                    $e->getResponse()->json()
67
                );
68
            } elseif ($e->getCode() == 403) {
69
                throw new Exception\InvalidApiKey(
70
                    'Access forbidden. Invalid API key.',
71
                    403,
72
                    $e,
73
                    $e->getResponse()->getBody()
74
                );
75
            } elseif ($e->getCode() == 404) {
76
                throw new Exception\NotFound(
77
                    'Requested URL was not found.',
78
                    404,
79
                    $e,
80
                    $e->getResponse()->getBody()
81
                );
82
            } elseif ($e->getCode() == 500) {
83
                throw new Exception\ServerError(
84
                    'Error occurred on server side while handling request',
85
                    500,
86
                    $e,
87
                    $e->getResponse()->getBody()
88
                );
89
            } elseif ($e->getCode() == 504) {
90
                throw new Exception\Timeout(
91
                    'Request timeout',
92
                    504,
93
                    $e,
94
                    $e->getResponse()->getBody()
95
                );
96
            } else {
97
                throw new Exception\UnexpectedResponse(
98
                    'Unexpected error occurred',
99
                    $e->getCode(),
100
                    $e,
101
                    $e->getResponse()->getBody()
102
                );
103
            }
104
        } catch (\Exception $e) {
105
            throw new Exception\UnexpectedError('Unexpected error occurred', 0, $e);
106
        }
107
108
        return $result;
109
    }
110
}
111