Passed
Push — master ( c97f61...125742 )
by Jérémy
01:58
created

Task::tasks()   B

Complexity

Conditions 11
Paths 5

Size

Total Lines 25
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 11
eloc 13
c 1
b 0
f 0
nc 5
nop 3
dl 0
loc 25
rs 7.3166

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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