1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace JDecool\Clubhouse; |
6
|
|
|
|
7
|
|
|
use JDecool\Clubhouse\{ |
8
|
|
|
Exception\ClubhouseException, |
9
|
|
|
Exception\ResourceNotExist, |
10
|
|
|
Exception\SchemaMismatch, |
11
|
|
|
Exception\TooManyRequest, |
12
|
|
|
Exception\Unprocessable, |
13
|
|
|
}; |
14
|
|
|
use Http\Client\Common\HttpMethodsClientInterface; |
15
|
|
|
use Psr\Http\Message\ResponseInterface; |
16
|
|
|
|
17
|
|
|
class Client implements HttpClient |
18
|
|
|
{ |
19
|
|
|
private $http; |
20
|
|
|
|
21
|
|
|
public function __construct(HttpMethodsClientInterface $http) |
22
|
|
|
{ |
23
|
|
|
$this->http = $http; |
24
|
|
|
} |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* @throws ClubhouseException |
28
|
|
|
*/ |
29
|
|
|
public function get(string $uri): array |
30
|
|
|
{ |
31
|
|
|
$response = $this->http->get($uri); |
32
|
|
|
|
33
|
|
|
if (200 !== $response->getStatusCode()) { |
34
|
|
|
throw $this->createExceptionFromResponse($response); |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
return json_decode($response->getBody()->getContents(), true); |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* @throws ClubhouseException |
42
|
|
|
* @throws \JsonException |
43
|
|
|
*/ |
44
|
|
|
public function post(string $uri, array $data): array |
45
|
|
|
{ |
46
|
|
|
$response = $this->http->post($uri, [], json_encode($data, JSON_THROW_ON_ERROR)); |
47
|
|
|
|
48
|
|
|
if (201 !== $response->getStatusCode()) { |
49
|
|
|
throw $this->createExceptionFromResponse($response); |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
return json_decode($response->getBody()->getContents(), true); |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* @throws ClubhouseException |
57
|
|
|
* @throws \JsonException |
58
|
|
|
*/ |
59
|
|
|
public function put(string $uri, array $data): array |
60
|
|
|
{ |
61
|
|
|
$response = $this->http->put($uri, [], json_encode($data, JSON_THROW_ON_ERROR)); |
62
|
|
|
|
63
|
|
|
if (200 !== $response->getStatusCode()) { |
64
|
|
|
throw $this->createExceptionFromResponse($response); |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
return json_decode($response->getBody()->getContents(), true); |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* @throws ClubhouseException |
72
|
|
|
*/ |
73
|
|
|
public function delete(string $uri): void |
74
|
|
|
{ |
75
|
|
|
$response = $this->http->delete($uri); |
76
|
|
|
|
77
|
|
|
if (204 !== $response->getStatusCode()) { |
78
|
|
|
throw $this->createExceptionFromResponse($response); |
79
|
|
|
} |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
private function createExceptionFromResponse(ResponseInterface $response): ClubhouseException |
83
|
|
|
{ |
84
|
|
|
$content = json_decode($response->getBody()->getContents(), true); |
85
|
|
|
$message = $content['message'] ?? 'An error occured.'; |
86
|
|
|
|
87
|
|
|
switch ($response->getStatusCode()) { |
88
|
|
|
case 400: |
89
|
|
|
return new SchemaMismatch($message); |
90
|
|
|
|
91
|
|
|
case 404: |
92
|
|
|
return new ResourceNotExist($message); |
93
|
|
|
|
94
|
|
|
case 422: |
95
|
|
|
return new Unprocessable($message); |
96
|
|
|
|
97
|
|
|
case 429: |
98
|
|
|
return new TooManyRequest($message); |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
return new ClubhouseException($message); |
102
|
|
|
} |
103
|
|
|
} |
104
|
|
|
|