Completed
Push — master ( 4b86d5...24918a )
by steef
01:22
created

Client::delete()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 0
cts 2
cp 0
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 2
crap 2
1
<?php
2
3
namespace TestMonitor\Custify;
4
5
use Psr\Http\Message\ResponseInterface;
6
use TestMonitor\Custify\Exceptions\Exception;
7
use TestMonitor\Custify\Exceptions\NotFoundException;
8
use TestMonitor\Custify\Exceptions\ValidationException;
9
use TestMonitor\Custify\Exceptions\FailedActionException;
10
use TestMonitor\Custify\Exceptions\UnauthorizedException;
11
12
class Client
13
{
14
    use Actions\ManagesPeople,
15
        Actions\ManagesCompanies,
16
        Actions\ManagesEvents;
17
18
    /**
19
     * @var string
20
     */
21
    protected $token;
22
23
    /**
24
     * @var string
25
     */
26
    protected $baseUrl = 'https://api.custify.com';
27
28
    /**
29
     * @var \GuzzleHttp\Client
30
     */
31
    protected $client;
32
33
    /**
34
     * Create a new client instance.
35
     *
36
     * @param string $token
37
     */
38 23
    public function __construct(string $token)
39
    {
40 23
        $this->token = $token;
41 23
    }
42
43
    /**
44
     * Returns an Guzzle client instance.
45
     *
46
     * @throws \TestMonitor\Custify\Exceptions\UnauthorizedException
47
     * @return \GuzzleHttp\Client
48
     */
49 23
    protected function client()
50
    {
51 23
        if (empty($this->token)) {
52
            throw new UnauthorizedException();
53
        }
54
55 23
        return $this->client ?? new \GuzzleHttp\Client([
56
            'base_uri' => $this->baseUrl . '/',
57
            'http_errors' => false,
58
            'headers' => [
59
                'Authorization' => 'Bearer ' . $this->token,
60
                'Accept' => 'application/json',
61 23
                'Content-Type' => 'application/json',
62
            ],
63
        ]);
64
    }
65
66
    /**
67
     * @param \GuzzleHttp\Client $client
68
     */
69 23
    public function setClient(\GuzzleHttp\Client $client)
70
    {
71 23
        $this->client = $client;
72 23
    }
73
74
    /**
75
     * Make a GET request to Custify servers and return the response.
76
     *
77
     * @param string $uri
78
     *
79
     * @param array $payload
80
     * @throws \GuzzleHttp\Exception\GuzzleException
81
     * @throws \TestMonitor\Custify\Exceptions\FailedActionException
82
     * @throws \TestMonitor\Custify\Exceptions\NotFoundException
83
     * @throws \TestMonitor\Custify\Exceptions\UnauthorizedException
84
     * @throws \TestMonitor\Custify\Exceptions\ValidationException
85
     * @return mixed
86
     */
87 21
    protected function get($uri, array $payload = [])
88
    {
89 21
        return $this->request('GET', $uri, $payload);
90
    }
91
92
    /**
93
     * Make a POST request to Custify servers and return the response.
94
     *
95
     * @param string $uri
96
     * @param array $payload
97
     *
98
     * @throws \GuzzleHttp\Exception\GuzzleException
99
     * @throws \TestMonitor\Custify\Exceptions\FailedActionException
100
     * @throws \TestMonitor\Custify\Exceptions\NotFoundException
101
     * @throws \TestMonitor\Custify\Exceptions\UnauthorizedException
102
     * @throws \TestMonitor\Custify\Exceptions\ValidationException
103
     * @return mixed
104
     */
105 2
    protected function post($uri, array $payload = [])
106
    {
107 2
        return $this->request('POST', $uri, $payload);
108
    }
109
110
    /**
111
     * Make a DELETE request to Custify servers and return the response.
112
     *
113
     * @param string $uri
114
     * @param array $payload
115
     *
116
     * @throws \GuzzleHttp\Exception\GuzzleException
117
     * @throws \TestMonitor\Custify\Exceptions\FailedActionException
118
     * @throws \TestMonitor\Custify\Exceptions\NotFoundException
119
     * @throws \TestMonitor\Custify\Exceptions\UnauthorizedException
120
     * @throws \TestMonitor\Custify\Exceptions\ValidationException
121
     * @return mixed
122
     */
123
    protected function delete($uri, array $payload = [])
124
    {
125
        return $this->request('DELETE', $uri, $payload);
126
    }
127
128
    /**
129
     * Make a PUT request to Forge servers and return the response.
130
     *
131
     * @param string $uri
132
     * @param array $payload
133
     *
134
     * @throws \GuzzleHttp\Exception\GuzzleException
135
     * @throws \TestMonitor\Custify\Exceptions\FailedActionException
136
     * @throws \TestMonitor\Custify\Exceptions\NotFoundException
137
     * @throws \TestMonitor\Custify\Exceptions\UnauthorizedException
138
     * @throws \TestMonitor\Custify\Exceptions\ValidationException
139
     * @return mixed
140
     */
141
    protected function patch($uri, array $payload = [])
142
    {
143
        return $this->request('PATCH', $uri, $payload);
144
    }
145
146
    /**
147
     * Make request to Custify servers and return the response.
148
     *
149
     * @param string $verb
150
     * @param string $uri
151
     * @param array $payload
152
     *
153
     * @throws \GuzzleHttp\Exception\GuzzleException
154
     * @throws \TestMonitor\Custify\Exceptions\FailedActionException
155
     * @throws \TestMonitor\Custify\Exceptions\NotFoundException
156
     * @throws \TestMonitor\Custify\Exceptions\UnauthorizedException
157
     * @throws \TestMonitor\Custify\Exceptions\ValidationException
158
     * @return mixed
159
     */
160 23
    protected function request($verb, $uri, array $payload = [])
161
    {
162 23
        $response = $this->client()->request(
163 23
            $verb,
164
            $uri,
165
            $payload
166
        );
167
168 23
        if (! in_array($response->getStatusCode(), [200, 201, 202, 203, 204, 206])) {
169 13
            return $this->handleRequestError($response);
170
        }
171
172 10
        $responseBody = (string) $response->getBody();
173
174 10
        return json_decode($responseBody, true) ?: $responseBody;
175
    }
176
177
    /**
178
     * @param  \Psr\Http\Message\ResponseInterface $response
179
     *
180
     * @throws \TestMonitor\Custify\Exceptions\ValidationException
181
     * @throws \TestMonitor\Custify\Exceptions\NotFoundException
182
     * @throws \TestMonitor\Custify\Exceptions\FailedActionException
183
     * @throws \Exception
184
     * @return void
185
     */
186 13
    protected function handleRequestError(ResponseInterface $response)
187
    {
188 13
        if ($response->getStatusCode() == 422) {
189 4
            throw new ValidationException(json_decode((string) $response->getBody(), true));
190
        }
191
192 9
        if ($response->getStatusCode() == 404) {
193 3
            throw new NotFoundException();
194
        }
195
196 6
        if ($response->getStatusCode() == 401 || $response->getStatusCode() == 403) {
197 2
            throw new UnauthorizedException();
198
        }
199
200 4
        if ($response->getStatusCode() == 400) {
201 2
            throw new FailedActionException((string) $response->getBody());
202
        }
203
204 2
        throw new Exception((string) $response->getStatusCode());
205
    }
206
}
207