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
|
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) { |
|
|
|
|
87
|
|
|
return false; |
88
|
|
|
} |
89
|
|
|
|
90
|
24 |
|
return $timer->id; |
91
|
|
|
} |
92
|
|
|
|
93
|
25 |
|
public function stopCurrentTimer() |
94
|
|
|
{ |
95
|
25 |
|
$workspaceId = getenv('timer_workspace_id'); |
96
|
25 |
|
$userId = getenv('timer_user_id'); |
97
|
|
|
|
98
|
25 |
|
if ($timerId = $this->runningTimer()) { |
|
|
|
|
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; |
|
|
|
|
111
|
|
|
} |
112
|
|
|
|
113
|
24 |
|
return true; |
114
|
|
|
} |
115
|
|
|
|
116
|
7 |
|
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; |
|
|
|
|
135
|
|
|
} |
136
|
|
|
|
137
|
6 |
|
public function pastTimers() |
138
|
|
|
{ |
139
|
|
|
try { |
140
|
6 |
|
$pastTimers = []; |
|
|
|
|
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
|
1 |
|
public function deleteTimer($timerId) |
163
|
|
|
{ |
164
|
1 |
|
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; |
|
|
|
|
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
|
|
|
|