Passed
Push — master ( a7445e...7a8a1d )
by Jérémy
01:47
created

Client::tagApi()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

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