Client::createExceptionFromResponse()   A
last analyzed

Complexity

Conditions 5
Paths 5

Size

Total Lines 21
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 13
c 2
b 0
f 0
dl 0
loc 21
rs 9.5222
cc 5
nc 5
nop 1
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
17
class Client
18
{
19
    private $http;
20
21
    public function __construct(HttpMethodsClient $http)
22
    {
23
        $this->http = $http;
24
    }
25
26
    public function get(string $uri, array $params = []): array
27
    {
28
        if (!empty($params)) {
29
            $uri .= '?'.http_build_query($params);
30
        }
31
32
        $response = $this->http->get($uri);
33
34
        if (200 !== $response->getStatusCode()) {
35
            throw $this->createExceptionFromResponse($response);
36
        }
37
38
        return json_decode($response->getBody()->getContents(), true, 512, JSON_THROW_ON_ERROR);
39
    }
40
41
    public function post(string $uri, array $data): array
42
    {
43
        $response = $this->http->post($uri, [], json_encode($data, JSON_THROW_ON_ERROR));
44
45
        if (201 !== $response->getStatusCode()) {
46
            throw $this->createExceptionFromResponse($response);
47
        }
48
49
        return json_decode($response->getBody()->getContents(), true, 512, JSON_THROW_ON_ERROR);
50
    }
51
52
    public function put(string $uri, array $data): array
53
    {
54
        $response = $this->http->put($uri, [], json_encode($data, JSON_THROW_ON_ERROR));
55
56
        if (!in_array($response->getStatusCode(), [200, 201], true)) {
57
            throw $this->createExceptionFromResponse($response);
58
        }
59
60
        return json_decode($response->getBody()->getContents(), true, 512, JSON_THROW_ON_ERROR);
61
    }
62
63
    public function patch(string $uri, array $data): array
64
    {
65
        $response = $this->http->patch($uri, [], json_encode($data, JSON_THROW_ON_ERROR));
66
67
        if (!in_array($response->getStatusCode(), [200, 204], true)) {
68
            throw $this->createExceptionFromResponse($response);
69
        }
70
71
        return json_decode($response->getBody()->getContents(), true, 512, JSON_THROW_ON_ERROR);
72
    }
73
74
    public function delete(string $uri): void
75
    {
76
        $response = $this->http->delete($uri);
77
78
        if (204 !== $response->getStatusCode()) {
79
            throw $this->createExceptionFromResponse($response);
80
        }
81
    }
82
83
    private function createExceptionFromResponse(ResponseInterface $response): ClockifyException
84
    {
85
        $data = json_decode((string) $response->getBody(), true);
86
        $message = $data['message'] ?? '';
87
        $code = $data['code'] ?? 0;
88
89
        switch ($response->getStatusCode()) {
90
            case 400:
91
                return new BadRequest($message, $code);
92
93
            case 401:
94
                return new Unauthorized($message, $code);
95
96
            case 403:
97
                return new Forbidden($message, $code);
98
99
            case 404:
100
                return new NotFound($message, $code);
101
        }
102
103
        return new ClockifyException($message, $code);
104
    }
105
}
106