Passed
Push — master ( 881b80...a7445e )
by Jérémy
01:38
created

Client   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 41
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 16
dl 0
loc 41
rs 10
c 1
b 0
f 0
wmc 11

3 Methods

Rating   Name   Duplication   Size   Complexity  
A createClient() 0 5 1
B clients() 0 21 9
A __construct() 0 3 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace JDecool\Clockify\Api\Client;
6
7
use JDecool\Clockify\Client as Http;
8
use JDecool\Clockify\Exception\ClockifyException;
9
use JDecool\Clockify\Model\ClientDto;
10
11
class Client
12
{
13
    private $http;
14
15
    public function __construct(Http $http)
16
    {
17
        $this->http = $http;
18
    }
19
20
    /**
21
     * @return ClientDto[]
22
     */
23
    public function clients(string $workspaceId, array $params = []): array
24
    {
25
        if (isset($params['name']) && empty($params['name'])) {
26
            throw new ClockifyException('Invalid "name" parameter');
27
        }
28
29
        if (isset($params['page']) && (!is_int($params['page']) || $params['page'] < 1)) {
30
            throw new ClockifyException('Invalid "page" parameter');
31
        }
32
33
        if (isset($params['page-size']) && (!is_int($params['page-size']) || $params['page-size'] < 1)) {
34
            throw new ClockifyException('Invalid "page-size" parameter');
35
        }
36
37
        $data = $this->http->get("/workspaces/$workspaceId/clients", $params);
38
39
        return array_map(
40
            static function(array $client): ClientDto {
41
                return ClientDto::fromArray($client);
42
            },
43
            $data
44
        );
45
    }
46
47
    public function createClient(string $workspaceId, ClientRequest $request): ClientDto
48
    {
49
        $data = $this->http->post("/workspaces/$workspaceId/clients", $request->toArray());
50
51
        return ClientDto::fromArray($data);
52
    }
53
}
54