1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace JDecool\Clockify; |
6
|
|
|
|
7
|
|
|
use Http\Client\Common\HttpMethodsClient; |
8
|
|
|
use JDecool\Clockify\{ |
9
|
|
|
Api\Tag\Tag, |
10
|
|
|
Api\Workspace\Workspace, |
11
|
|
|
Exception\ClockifyException, |
12
|
|
|
Exception\BadRequest, |
13
|
|
|
Exception\Forbidden, |
14
|
|
|
Exception\NotFound, |
15
|
|
|
Exception\Unauthorized}; |
16
|
|
|
use Psr\Http\Message\ResponseInterface; |
17
|
|
|
use RuntimeException; |
18
|
|
|
|
19
|
|
|
class Client |
20
|
|
|
{ |
21
|
|
|
private $http; |
22
|
|
|
private $baseUri; |
23
|
|
|
private $apiKey; |
24
|
|
|
|
25
|
|
|
public function __construct(HttpMethodsClient $http, string $baseUri, string $apiKey) |
26
|
|
|
{ |
27
|
|
|
if (false === filter_var($baseUri, FILTER_VALIDATE_URL)) { |
28
|
|
|
throw new RuntimeException('Invalid Clockify endpoint.'); |
29
|
|
|
} |
30
|
|
|
|
31
|
|
|
$this->http = $http; |
32
|
|
|
$this->baseUri = $baseUri; |
33
|
|
|
$this->apiKey = $apiKey; |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
public function tagApi(): Tag |
37
|
|
|
{ |
38
|
|
|
return new Tag($this); |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
public function workspaceApi(): Workspace |
42
|
|
|
{ |
43
|
|
|
return new Workspace($this); |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
public function get(string $uri, array $params = []): array |
47
|
|
|
{ |
48
|
|
|
$response = $this->http->get( |
49
|
|
|
$this->endpoint($uri, $params), |
50
|
|
|
['X-Api-Key' => $this->apiKey] |
51
|
|
|
); |
52
|
|
|
|
53
|
|
|
if (200 !== $response->getStatusCode()) { |
54
|
|
|
throw $this->createExceptionFromResponse($response); |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
return json_decode($response->getBody()->getContents(), true); |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
public function post(string $uri, array $data): array |
61
|
|
|
{ |
62
|
|
|
$response = $this->http->post( |
63
|
|
|
$this->endpoint($uri), |
64
|
|
|
[ |
65
|
|
|
'Content-Type' => 'application/json', |
66
|
|
|
'X-Api-Key' => $this->apiKey, |
67
|
|
|
], |
68
|
|
|
json_encode($data) |
69
|
|
|
); |
70
|
|
|
|
71
|
|
|
if (201 !== $response->getStatusCode()) { |
72
|
|
|
throw $this->createExceptionFromResponse($response); |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
return json_decode($response->getBody()->getContents(), true); |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
public function put(string $uri, array $data): array |
79
|
|
|
{ |
80
|
|
|
$response = $this->http->put( |
81
|
|
|
$this->endpoint($uri), |
82
|
|
|
[ |
83
|
|
|
'Content-Type' => 'application/json', |
84
|
|
|
'X-Api-Key' => $this->apiKey, |
85
|
|
|
], |
86
|
|
|
json_encode($data) |
87
|
|
|
); |
88
|
|
|
|
89
|
|
|
if (!in_array($response->getStatusCode(), [200, 201], true)) { |
90
|
|
|
throw $this->createExceptionFromResponse($response); |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
return json_decode($response->getBody()->getContents(), true); |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
public function patch(string $uri, array $data): array |
97
|
|
|
{ |
98
|
|
|
$response = $this->http->patch( |
99
|
|
|
$this->endpoint($uri), |
100
|
|
|
[ |
101
|
|
|
'Content-Type' => 'application/json', |
102
|
|
|
'X-Api-Key' => $this->apiKey, |
103
|
|
|
], |
104
|
|
|
json_encode($data) |
105
|
|
|
); |
106
|
|
|
|
107
|
|
|
if (!in_array($response->getStatusCode(), [200, 204], true)) { |
108
|
|
|
throw $this->createExceptionFromResponse($response); |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
return json_decode($response->getBody()->getContents(), true); |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
public function delete(string $uri): void |
115
|
|
|
{ |
116
|
|
|
$response = $this->http->delete( |
117
|
|
|
$this->endpoint($uri) |
118
|
|
|
); |
119
|
|
|
|
120
|
|
|
if (204 !== $response->getStatusCode()) { |
121
|
|
|
throw $this->createExceptionFromResponse($response); |
122
|
|
|
} |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
private function endpoint(string $uri, array $params = []): string |
126
|
|
|
{ |
127
|
|
|
$endpoint = sprintf( |
128
|
|
|
'%s/%s', |
129
|
|
|
rtrim($this->baseUri, '/'), |
130
|
|
|
ltrim($uri, '/') |
131
|
|
|
); |
132
|
|
|
|
133
|
|
|
if (!empty($params)) { |
134
|
|
|
$endpoint .= http_build_query($params); |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
return $endpoint; |
138
|
|
|
} |
139
|
|
|
|
140
|
|
|
private function createExceptionFromResponse(ResponseInterface $response): ClockifyException |
141
|
|
|
{ |
142
|
|
|
switch ($response->getStatusCode()) { |
143
|
|
|
case 400: |
144
|
|
|
return new BadRequest(); |
145
|
|
|
|
146
|
|
|
case 401: |
147
|
|
|
return new Unauthorized(); |
148
|
|
|
|
149
|
|
|
case 403: |
150
|
|
|
return new Forbidden(); |
151
|
|
|
|
152
|
|
|
case 404: |
153
|
|
|
return new NotFound(); |
154
|
|
|
} |
155
|
|
|
|
156
|
|
|
return new ClockifyException(); |
157
|
|
|
} |
158
|
|
|
} |
159
|
|
|
|