Passed
Push — clockify ( d6a10a...c16d28 )
by Guillaume
31:35
created

Clockify::buildPastTimerObject()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 18
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 11
CRAP Score 3

Importance

Changes 0
Metric Value
cc 3
eloc 10
nc 4
nop 3
dl 0
loc 18
ccs 11
cts 11
cp 1
crap 3
rs 9.9332
c 0
b 0
f 0
1
<?php
2
3
namespace Godbout\Alfred\Time\Services;
4
5
use Carbon\Carbon;
6
use GuzzleHttp\Client;
7
8
class Clockify extends TimerService
9
{
10
    private $client;
11
12
    private $data = null;
0 ignored issues
show
introduced by
The private property $data is not used, and could be removed.
Loading history...
13
14
15 72
    public function __construct($apiToken)
16
    {
17 72
        $this->client = new Client([
18 72
            'base_uri' => 'https://api.clockify.me/api/v1/',
19
            'headers' => [
20 72
                'content-type' => 'application/json',
21 72
                'X-Api-Key' => $apiToken
22
            ]
23
        ]);
24 72
    }
25
26 12
    public function workspaces()
27
    {
28
        try {
29 12
            $response = $this->client->get('workspaces');
30 6
            $workspaces = json_decode($response->getBody()->getContents());
31
32 6
            return array_column($workspaces, 'name', 'id');
33 6
        } catch (\Exception $e) {
34 6
            return [];
35
        }
36
    }
37
38 18
    public function projects()
39
    {
40
        try {
41 18
            $workspaceId = getenv('timer_workspace_id');
42
43 18
            $response = $this->client->get("workspaces/$workspaceId/projects");
44
45 12
            $projects = json_decode($response->getBody()->getContents());
46
47 12
            return array_column($projects, 'name', 'id');
48 6
        } catch (\Exception $e) {
49 6
            return [];
50
        }
51
    }
52
53 18
    public function tags()
54
    {
55
        try {
56 18
            $workspaceId = getenv('timer_workspace_id');
57
58 18
            $response = $this->client->get("workspaces/$workspaceId/tags");
59 12
            $tags = json_decode($response->getBody()->getContents());
60
61 12
            return array_column($tags, 'name', 'id');
62 6
        } catch (\Exception $e) {
63 6
            return [];
64
        }
65
    }
66
67 24
    public function startTimer()
68
    {
69
        try {
70 24
            $workspaceId = getenv('timer_workspace_id');
71
72 24
            $response = $this->client->post("workspaces/$workspaceId/time-entries", [
73 8
                'json' => [
74 24
                    'start' => (new \DateTime())->format('Y-m-d\TH:i:s\Z'),
75 24
                    'description' => getenv('timer_description'),
76 24
                    'projectId' => getenv('timer_project_id'),
77 24
                    'tagIds' => getenv('timer_tag_id') ? [getenv('timer_tag_id')] : [''],
78
                ]
79
            ]);
80
81 24
            $timer = json_decode($response->getBody()->getContents());
82
83 24
            if (! isset($timer->id)) {
84 24
                return false;
85
            }
86
        } catch (Exception $e) {
0 ignored issues
show
Bug introduced by
The type Godbout\Alfred\Time\Services\Exception was not found. Did you mean Exception? If so, make sure to prefix the type with \.
Loading history...
87
            return false;
88
        }
89
90 24
        return $timer->id;
91
    }
92
93 24
    public function stopCurrentTimer()
94
    {
95 24
        $workspaceId = getenv('timer_workspace_id');
96 24
        $userId = getenv('timer_user_id');
97
98 24
        if ($timerId = $this->runningTimer()) {
0 ignored issues
show
Unused Code introduced by
The assignment to $timerId is dead and can be removed.
Loading history...
99 24
            $response = $this->client->patch("workspaces/$workspaceId/user/$userId/time-entries", [
100
                'json' => [
101 24
                    'end' => (new \DateTime())->format('Y-m-d\TH:i:s\Z'),
102
                ]
103
            ]) ;
104
105 24
            $timer = json_decode($response->getBody()->getContents());
106
107 24
            if (! isset($timer->timeInterval->end)) {
108
                throw new Exception("Can't stop current running timer.", 1);
109
110
                return false;
0 ignored issues
show
Unused Code introduced by
return false is not reachable.

This check looks for unreachable code. It uses sophisticated control flow analysis techniques to find statements which will never be executed.

Unreachable code is most often the result of return, die or exit statements that have been added for debug purposes.

function fx() {
    try {
        doSomething();
        return true;
    }
    catch (\Exception $e) {
        return false;
    }

    return false;
}

In the above example, the last return false will never be executed, because a return statement has already been met in every possible execution path.

Loading history...
111
            }
112
113 24
            return true;
114
        }
115
116 6
        return false;
117
    }
118
119 72
    public function runningTimer()
120
    {
121
        try {
122 72
            $workspaceId = getenv('timer_workspace_id');
123 72
            $userId = getenv('timer_user_id');
124
125 72
            $response = $this->client->get("workspaces/$workspaceId/user/$userId/time-entries?in-progress=true");
126
127 72
            $timer = json_decode($response->getBody()->getContents());
128
129 72
            return $timer[0]->id ?? false;
130
        } catch (\Exception $e) {
131
            return false;
132
        }
133
134
        return true;
0 ignored issues
show
Unused Code introduced by
return true is not reachable.

This check looks for unreachable code. It uses sophisticated control flow analysis techniques to find statements which will never be executed.

Unreachable code is most often the result of return, die or exit statements that have been added for debug purposes.

function fx() {
    try {
        doSomething();
        return true;
    }
    catch (\Exception $e) {
        return false;
    }

    return false;
}

In the above example, the last return false will never be executed, because a return statement has already been met in every possible execution path.

Loading history...
135
    }
136
137 6
    public function pastTimers()
138
    {
139
        try {
140 6
            $pastTimers = [];
0 ignored issues
show
Unused Code introduced by
The assignment to $pastTimers is dead and can be removed.
Loading history...
141
142 6
            $workspaceId = getenv('timer_workspace_id');
143 6
            $userId = getenv('timer_user_id');
144
145 6
            $response = $this->client->get("workspaces/$workspaceId/user/$userId/time-entries", [
146 6
                'start' => Carbon::today(),
147 6
                'end' => Carbon::today()->subDays(30),
148
            ]);
149 6
            $clockifyTimers = json_decode($response->getBody()->getContents());
150
151 6
            return $this->convertToPastTimers($clockifyTimers);
152
        } catch (Exception $e) {
153
            return [];
154
        }
155
    }
156
157
    public function continueTimer($timerId = null)
158
    {
159
        return false;
160
    }
161
162
    public function deleteTimer($timerId)
163
    {
164
        return false;
165
    }
166
167 6
    protected function convertToPastTimers($clockifyTimers)
168
    {
169 6
        $projects = $this->projects();
170 6
        $tags = $this->tags();
171
172
        return array_map(function ($clockifyTimer) use ($projects, $tags) {
173 6
            return $this->buildPastTimerObject($clockifyTimer, $projects, $tags);
174 6
        }, $clockifyTimers);
175
    }
176
177 6
    protected function buildPastTimerObject($clockifyTimer, $projects, $tags)
178
    {
179 6
        $pastTimer['id'] = $clockifyTimer->id;
0 ignored issues
show
Comprehensibility Best Practice introduced by
$pastTimer was never initialized. Although not strictly required by PHP, it is generally a good practice to add $pastTimer = array(); before regardless.
Loading history...
180 6
        $pastTimer['description'] = $clockifyTimer->description;
181
182 6
        if (isset($clockifyTimer->projectId)) {
183 6
            $pastTimer['project_id'] = $clockifyTimer->projectId;
184 6
            $pastTimer['project_name'] = $projects[$clockifyTimer->projectId];
185
        }
186
187 6
        if (isset($clockifyTimer->tagIds[0])) {
188 6
            $pastTimer['tag_id'] = $clockifyTimer->tagIds[0];
189 6
            $pastTimer['tags'] = $tags[$clockifyTimer->tagIds[0]];
190
        }
191
192 6
        $pastTimer['duration'] = $clockifyTimer->timeInterval->duration;
193
194 6
        return (object) $pastTimer;
195
    }
196
}
197