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