Client   A
last analyzed

Complexity

Total Complexity 16

Size/Duplication

Total Lines 127
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 16
dl 0
loc 127
c 0
b 0
f 0
rs 10

6 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 10 3
A clear() 0 4 1
A newClient() 0 3 1
B call() 0 22 4
B parse() 0 19 6
A endpoint() 0 5 1
1
<?php
2
3
namespace Amelia\Monzo;
4
5
use GuzzleHttp\Psr7\Response;
6
use GuzzleHttp\Client as Guzzle;
7
use Amelia\Monzo\Exceptions\MonzoException;
8
use Amelia\Monzo\Contracts\Client as ClientContract;
9
use Amelia\Monzo\Exceptions\UnexpectedValueException;
10
11
class Client implements ClientContract
12
{
13
    use UsesTokens, UsesQueryParams;
14
15
    /**
16
     * The guzzle client.
17
     *
18
     * @var \GuzzleHttp\Client
19
     */
20
    protected $guzzle;
21
22
    /**
23
     * Create a new client instance.
24
     *
25
     * @param \GuzzleHttp\Client $guzzle
26
     * @param string|null $id
27
     * @param string|null $secret
28
     */
29
    public function __construct(Guzzle $guzzle, ?string $id = null, ?string $secret = null)
30
    {
31
        $this->guzzle = $guzzle;
32
33
        if ($id !== null) {
34
            $this->setClientId($id);
35
        }
36
37
        if ($secret !== null) {
38
            $this->setClientSecret($secret);
39
        }
40
    }
41
42
    /**
43
     * Do an API call.
44
     *
45
     * @param string $method
46
     * @param string $endpoint
47
     * @param string|null $key
48
     * @param array $query
49
     * @param array $data
50
     * @param bool $raw
51
     * @return mixed|\Psr\Http\Message\ResponseInterface
52
     */
53
    public function call(string $method, string $endpoint, array $query = [], array $data = [], ?string $key = null, bool $raw = false)
54
    {
55
        $endpoint = $this->endpoint($endpoint, $query);
56
57
        $options = [
58
            'headers' => [
59
                'Accept' => 'application/json',
60
            ],
61
            'http_errors' => false,
62
        ];
63
64
        if ($this->token) {
65
            $options['headers']['Authorization'] = 'Bearer ' . $this->token;
66
        }
67
68
        if ($data) {
69
            $options['form_params'] = $data;
70
        }
71
72
        $response = $this->guzzle->request($method, $endpoint, $options);
73
74
        return $raw ? $response : $this->parse($response, $key);
75
    }
76
77
    /**
78
     * Get an endpoint (including query params).
79
     *
80
     * @param string $endpoint
81
     * @param array $query
82
     * @return string
83
     */
84
    protected function endpoint(string $endpoint, array $query)
85
    {
86
        $params = $this->buildQueryParams($query);
87
88
        return static::API_ENDPOINT . '/' . trim($endpoint, '/') . '?' . $params;
89
    }
90
91
    /**
92
     * Clear all auth tokens/etc on this client instance.
93
     *
94
     * @return void
95
     */
96
    protected function clear()
97
    {
98
        $this->token = null;
99
        $this->params = [];
100
    }
101
102
    /**
103
     * Parse out data from the API that we want.
104
     *
105
     * @param \GuzzleHttp\Psr7\Response $response
106
     * @param string $key
107
     * @return array
108
     */
109
    protected function parse(Response $response, string $key = null)
110
    {
111
        $code = $response->getStatusCode();
112
113
        $body = $response->getBody()->getContents();
114
115
        $json = json_decode_response($response, $body);
116
117
        if ($code >= 400 || $code < 200) {
118
            $errorCode = array_get($json, 'error');
119
120
            throw MonzoException::fromResponse($response, $body, $errorCode);
121
        }
122
123
        if ($key !== null && ! array_key_exists($key, $json)) {
124
            throw new UnexpectedValueException("Expected to find a [$key] key within the response; none found.");
125
        }
126
127
        return $key === null ? $json : $json[$key];
128
    }
129
130
    /**
131
     * Get a new client instance.
132
     *
133
     * @return \Amelia\Monzo\Client|\Amelia\Monzo\Contracts\Client
134
     */
135
    public function newClient()
136
    {
137
        return new static($this->guzzle, $this->id, $this->secret);
138
    }
139
}
140