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; |
|
|
|
|
13
|
|
|
|
14
|
|
|
|
15
|
78 |
|
public function __construct($apiToken) |
16
|
|
|
{ |
17
|
78 |
|
$this->client = new Client([ |
18
|
78 |
|
'base_uri' => 'https://api.clockify.me/api/v1/', |
19
|
|
|
'headers' => [ |
20
|
78 |
|
'content-type' => 'application/json', |
21
|
78 |
|
'X-Api-Key' => $apiToken |
22
|
|
|
] |
23
|
|
|
]); |
24
|
78 |
|
} |
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 |
|
$user = json_decode( |
42
|
18 |
|
$this->client->get("user")->getBody()->getContents() |
43
|
|
|
); |
44
|
|
|
|
45
|
12 |
|
$response = $this->client->get("workspaces/{$user->activeWorkspace}/projects"); |
46
|
|
|
|
47
|
12 |
|
$projects = json_decode($response->getBody()->getContents()); |
48
|
|
|
|
49
|
12 |
|
return array_column($projects, 'name', 'id'); |
50
|
6 |
|
} catch (\Exception $e) { |
51
|
6 |
|
return []; |
52
|
|
|
} |
53
|
|
|
} |
54
|
|
|
|
55
|
18 |
|
public function tags() |
56
|
|
|
{ |
57
|
|
|
try { |
58
|
18 |
|
$workspaceId = getenv('timer_workspace_id'); |
59
|
|
|
|
60
|
18 |
|
$response = $this->client->get("workspaces/$workspaceId/tags"); |
61
|
12 |
|
$tags = json_decode($response->getBody()->getContents()); |
62
|
|
|
|
63
|
12 |
|
return array_column($tags, 'name', 'id'); |
64
|
6 |
|
} catch (\Exception $e) { |
65
|
6 |
|
return []; |
66
|
|
|
} |
67
|
|
|
} |
68
|
|
|
|
69
|
30 |
|
public function startTimer() |
70
|
|
|
{ |
71
|
|
|
try { |
72
|
30 |
|
$workspaceId = getenv('timer_workspace_id'); |
73
|
|
|
|
74
|
30 |
|
$response = $this->client->post("workspaces/$workspaceId/time-entries", [ |
75
|
10 |
|
'json' => [ |
76
|
30 |
|
'start' => (new \DateTime())->format('Y-m-d\TH:i:s\Z'), |
77
|
30 |
|
'description' => getenv('timer_description'), |
78
|
30 |
|
'projectId' => getenv('timer_project_id'), |
79
|
30 |
|
'tagIds' => getenv('timer_tag_id') ? [getenv('timer_tag_id')] : [''], |
80
|
|
|
] |
81
|
|
|
]); |
82
|
|
|
|
83
|
30 |
|
$timer = json_decode($response->getBody()->getContents()); |
84
|
|
|
|
85
|
30 |
|
if (! isset($timer->id)) { |
86
|
30 |
|
return false; |
87
|
|
|
} |
88
|
|
|
} catch (Exception $e) { |
|
|
|
|
89
|
|
|
return false; |
90
|
|
|
} |
91
|
|
|
|
92
|
30 |
|
return $timer->id; |
93
|
|
|
} |
94
|
|
|
|
95
|
30 |
|
public function stopCurrentTimer() |
96
|
|
|
{ |
97
|
30 |
|
$workspaceId = getenv('timer_workspace_id'); |
98
|
30 |
|
$userId = getenv('timer_user_id'); |
99
|
|
|
|
100
|
30 |
|
if ($timerId = $this->runningTimer()) { |
|
|
|
|
101
|
30 |
|
$response = $this->client->patch("workspaces/$workspaceId/user/$userId/time-entries", [ |
102
|
|
|
'json' => [ |
103
|
30 |
|
'end' => (new \DateTime())->format('Y-m-d\TH:i:s\Z'), |
104
|
|
|
] |
105
|
|
|
]) ; |
106
|
|
|
|
107
|
30 |
|
$timer = json_decode($response->getBody()->getContents()); |
108
|
|
|
|
109
|
30 |
|
if (! isset($timer->timeInterval->end)) { |
110
|
|
|
throw new Exception("Can't stop current running timer.", 1); |
111
|
|
|
|
112
|
|
|
return false; |
|
|
|
|
113
|
|
|
} |
114
|
|
|
|
115
|
30 |
|
return true; |
116
|
|
|
} |
117
|
|
|
|
118
|
6 |
|
return false; |
119
|
|
|
} |
120
|
|
|
|
121
|
78 |
|
public function runningTimer() |
122
|
|
|
{ |
123
|
|
|
try { |
124
|
78 |
|
$workspaceId = getenv('timer_workspace_id'); |
125
|
78 |
|
$userId = getenv('timer_user_id'); |
126
|
|
|
|
127
|
78 |
|
$response = $this->client->get("workspaces/$workspaceId/user/$userId/time-entries?in-progress=true"); |
128
|
|
|
|
129
|
78 |
|
$timer = json_decode($response->getBody()->getContents()); |
130
|
|
|
|
131
|
78 |
|
return $timer[0]->id ?? false; |
132
|
|
|
} catch (\Exception $e) { |
133
|
|
|
return false; |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
return true; |
|
|
|
|
137
|
|
|
} |
138
|
|
|
|
139
|
6 |
|
public function pastTimers() |
140
|
|
|
{ |
141
|
|
|
try { |
142
|
6 |
|
$pastTimers = []; |
|
|
|
|
143
|
|
|
|
144
|
6 |
|
$workspaceId = getenv('timer_workspace_id'); |
145
|
6 |
|
$userId = getenv('timer_user_id'); |
146
|
|
|
|
147
|
6 |
|
$response = $this->client->get("workspaces/$workspaceId/user/$userId/time-entries", [ |
148
|
6 |
|
'start' => Carbon::today(), |
149
|
6 |
|
'end' => Carbon::today()->subDays(30), |
150
|
|
|
]); |
151
|
6 |
|
$clockifyTimers = json_decode($response->getBody()->getContents()); |
152
|
|
|
|
153
|
6 |
|
return $this->convertToPastTimers($clockifyTimers); |
154
|
|
|
} catch (Exception $e) { |
155
|
|
|
return []; |
156
|
|
|
} |
157
|
|
|
} |
158
|
|
|
|
159
|
6 |
|
public function continueTimer($timerId = null) |
160
|
|
|
{ |
161
|
|
|
/** |
162
|
|
|
* Timer attributes are stored in env variables |
163
|
|
|
* gathered in startTimer. |
164
|
|
|
*/ |
165
|
6 |
|
return $this->startTimer(); |
166
|
|
|
} |
167
|
|
|
|
168
|
6 |
|
public function deleteTimer($timerId) |
169
|
|
|
{ |
170
|
6 |
|
return false; |
171
|
|
|
} |
172
|
|
|
|
173
|
6 |
|
protected function convertToPastTimers($clockifyTimers) |
174
|
|
|
{ |
175
|
6 |
|
$projects = $this->projects(); |
176
|
6 |
|
$tags = $this->tags(); |
177
|
|
|
|
178
|
|
|
return array_map(function ($clockifyTimer) use ($projects, $tags) { |
179
|
6 |
|
return $this->buildPastTimerObject($clockifyTimer, $projects, $tags); |
180
|
6 |
|
}, $clockifyTimers); |
181
|
|
|
} |
182
|
|
|
|
183
|
6 |
|
protected function buildPastTimerObject($clockifyTimer, $projects, $tags) |
184
|
|
|
{ |
185
|
6 |
|
$pastTimer['id'] = $clockifyTimer->id; |
|
|
|
|
186
|
6 |
|
$pastTimer['description'] = $clockifyTimer->description; |
187
|
|
|
|
188
|
6 |
|
if (isset($clockifyTimer->projectId)) { |
189
|
6 |
|
$pastTimer['project_id'] = $clockifyTimer->projectId; |
190
|
6 |
|
$pastTimer['project_name'] = $projects[$clockifyTimer->projectId]; |
191
|
|
|
} |
192
|
|
|
|
193
|
6 |
|
if (isset($clockifyTimer->tagIds[0])) { |
194
|
6 |
|
$pastTimer['tag_id'] = $clockifyTimer->tagIds[0]; |
195
|
6 |
|
$pastTimer['tags'] = $tags[$clockifyTimer->tagIds[0]]; |
196
|
|
|
} |
197
|
|
|
|
198
|
6 |
|
$pastTimer['duration'] = $clockifyTimer->timeInterval->duration; |
199
|
|
|
|
200
|
6 |
|
return (object) $pastTimer; |
201
|
|
|
} |
202
|
|
|
} |
203
|
|
|
|