Passed
Push — master ( b6e1f4...929753 )
by Jérémy
01:51
created

Project   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 46
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 12
eloc 17
c 1
b 0
f 0
dl 0
loc 46
rs 10

4 Methods

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