TimeEntry   A
last analyzed

Complexity

Total Complexity 40

Size/Duplication

Total Lines 118
Duplicated Lines 0 %

Importance

Changes 6
Bugs 0 Features 0
Metric Value
wmc 40
eloc 49
c 6
b 0
f 0
dl 0
loc 118
rs 9.2

8 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 1
A create() 0 5 1
A update() 0 5 1
A createForUser() 0 5 1
A delete() 0 3 1
A entry() 0 13 5
D find() 0 61 29
A stopRunningTime() 0 5 1

How to fix   Complexity   

Complex Class

Complex classes like TimeEntry often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.

Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.

While breaking up the class, it is a good idea to analyze how other classes use TimeEntry, and based on these observations, apply Extract Interface, too.

1
<?php
2
3
declare(strict_types=1);
4
5
namespace JDecool\Clockify\Api\TimeEntry;
6
7
use JDecool\Clockify\{
8
    Client,
9
    Exception\ClockifyException,
10
    Model\TimeEntryDtoImpl
11
};
12
13
class TimeEntry
14
{
15
    private $http;
16
17
    public function __construct(Client $http)
18
    {
19
        $this->http = $http;
20
    }
21
22
    public function create(string $workspaceId, TimeEntryRequest $request): TimeEntryDtoImpl
23
    {
24
        $data = $this->http->post("/workspaces/$workspaceId/time-entries", $request->toArray());
25
26
        return TimeEntryDtoImpl::fromArray($data);
27
    }
28
29
    public function createForUser(string $workspaceId, string $userId, TimeEntryRequest $request): TimeEntryDtoImpl
30
    {
31
        $data = $this->http->post("/workspaces/$workspaceId/user/$userId/time-entries", $request->toArray());
32
33
        return TimeEntryDtoImpl::fromArray($data);
34
    }
35
36
    public function update(string $workspaceId, string $id, UpdateTimeEntryRequest $request): TimeEntryDtoImpl
37
    {
38
        $data = $this->http->put("/workspaces/$workspaceId/time-entries/$id", $request->toArray());
39
40
        return TimeEntryDtoImpl::fromArray($data);
41
    }
42
43
    public function entry(string $workspaceId, string $id, array $params = []): TimeEntryDtoImpl
44
    {
45
        if (isset($params['consider-duration-format']) && !is_bool($params['consider-duration-format'])) {
46
            throw new ClockifyException('Invalid "consider-duration-format" parameter (should be a boolean value)');
47
        }
48
49
        if (isset($params['hydrated']) && !is_bool($params['hydrated'])) {
50
            throw new ClockifyException('Invalid "hydrated" parameter (should be a boolean value)');
51
        }
52
53
        $data = $this->http->get("/workspaces/$workspaceId/time-entries/$id", $params);
54
55
        return TimeEntryDtoImpl::fromArray($data);
56
    }
57
58
    public function delete(string $workspaceId, string $id): void
59
    {
60
        $this->http->delete("/workspaces/$workspaceId/time-entries/$id");
61
    }
62
63
    public function stopRunningTime(string $workspaceId, string $userId, StopTimeEntryRequest $request): TimeEntryDtoImpl
64
    {
65
        $data = $this->http->patch("/workspaces/$workspaceId/user/$userId/time-entries", $request->toArray());
66
67
        return TimeEntryDtoImpl::fromArray($data);
68
    }
69
70
    public function find(string $workspaceId, string $userId, array $params = []): array
71
    {
72
        if (isset($params['description']) && !is_string($params['description'])) {
73
            throw new ClockifyException('Invalid "description" parameter');
74
        }
75
76
        if (isset($params['start']) && !is_string($params['start'])) {
77
            throw new ClockifyException('Invalid "start" parameter');
78
        }
79
80
        if (isset($params['end']) && !is_string($params['end'])) {
81
            throw new ClockifyException('Invalid "end" parameter');
82
        }
83
84
        if (isset($params['project']) && !is_string($params['project'])) {
85
            throw new ClockifyException('Invalid "project" parameter');
86
        }
87
88
        if (isset($params['task']) && !is_string($params['task'])) {
89
            throw new ClockifyException('Invalid "task" parameter');
90
        }
91
92
        if (isset($params['tags']) && !is_array($params['tags'])) {
93
            throw new ClockifyException('Invalid "tags" parameter');
94
        }
95
96
        if (isset($params['project-required']) && !is_bool($params['project-required'])) {
97
            throw new ClockifyException('Invalid "project-required" parameter');
98
        }
99
100
        if (isset($params['task-required']) && !is_bool($params['task-required'])) {
101
            throw new ClockifyException('Invalid "task-required" parameter');
102
        }
103
104
        if (isset($params['consider-duration-format']) && !is_bool($params['consider-duration-format'])) {
105
            throw new ClockifyException('Invalid "consider-duration-format" parameter');
106
        }
107
108
        if (isset($params['hydrated']) && !is_bool($params['hydrated'])) {
109
            throw new ClockifyException('Invalid "hydrated" parameter');
110
        }
111
112
        if (isset($params['in-progress']) && !is_bool($params['in-progress'])) {
113
            throw new ClockifyException('Invalid "in-progress" parameter');
114
        }
115
116
        if (isset($params['page']) && (!is_int($params['page']) || $params['page'] < 1)) {
117
            throw new ClockifyException('Invalid "page" parameter');
118
        }
119
120
        if (isset($params['page-size']) && (!is_int($params['page-size']) || $params['page-size'] < 1)) {
121
            throw new ClockifyException('Invalid "page-size" parameter');
122
        }
123
124
        $data = $this->http->get("/workspaces/$workspaceId/user/$userId/time-entries", $params);
125
126
        return array_map(
127
            static function(array $timeEntry): TimeEntryDtoImpl {
128
                return TimeEntryDtoImpl::fromArray($timeEntry);
129
            },
130
            $data
131
        );
132
    }
133
}
134