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

Client::clients()   B

Complexity

Conditions 9
Paths 4

Size

Total Lines 21
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 9
eloc 11
nc 4
nop 2
dl 0
loc 21
rs 8.0555
c 1
b 0
f 0
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