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

Task::create()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 2
c 0
b 0
f 0
dl 0
loc 5
rs 10
cc 1
nc 1
nop 3
1
<?php
2
3
declare(strict_types=1);
4
5
namespace JDecool\Clockify\Api\Task;
6
7
use JDecool\Clockify\{
8
    Client,
9
    Exception\ClockifyException,
10
    Model\TaskDto
11
};
12
13
class Task
14
{
15
    private $http;
16
17
    public function __construct(Client $http)
18
    {
19
        $this->http = $http;
20
    }
21
22
    /**
23
     * @return TaskDto[]
24
     */
25
    public function tasks(string $workspaceId, string $projectId, array $params = []): array
26
    {
27
        if (isset($params['is-active']) && !is_bool($params['is-active'])) {
28
            throw new ClockifyException('Invalid "is-active" parameter (should be a boolean value)');
29
        }
30
31
        if (isset($params['name']) && empty($params['name'])) {
32
            throw new ClockifyException('Invalid "name" parameter');
33
        }
34
35
        if (isset($params['page']) && (!is_int($params['page']) || $params['page'] < 1)) {
36
            throw new ClockifyException('Invalid "page" parameter');
37
        }
38
39
        if (isset($params['page-size']) && (!is_int($params['page-size']) || $params['page-size'] < 1)) {
40
            throw new ClockifyException('Invalid "page-size" parameter');
41
        }
42
43
        $data = $this->http->get("/workspaces/$workspaceId/projects/$projectId/tasks", $params);
44
45
        return array_map(
46
            static function(array $task): TaskDto {
47
                return TaskDto::fromArray($task);
48
            },
49
            $data
50
        );
51
    }
52
53
    public function create(string $workspaceId, string $projectId, TaskRequest $request): TaskDto
54
    {
55
        $data = $this->http->post("/workspaces/$workspaceId/projects/$projectId/tasks", $request->toArray());
56
57
        return TaskDto::fromArray($data);
58
    }
59
}
60