|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace AlfredTime; |
|
4
|
|
|
|
|
5
|
|
|
use AlfredTime\Toggl; |
|
6
|
|
|
use AlfredTime\Config; |
|
7
|
|
|
use AlfredTime\Harvest; |
|
8
|
|
|
|
|
9
|
|
|
class Time |
|
10
|
|
|
{ |
|
11
|
|
|
/** |
|
12
|
|
|
* @var mixed |
|
13
|
|
|
*/ |
|
14
|
|
|
private $config; |
|
15
|
|
|
|
|
16
|
|
|
/** |
|
17
|
|
|
* @var mixed |
|
18
|
|
|
*/ |
|
19
|
|
|
private $harvest; |
|
20
|
|
|
|
|
21
|
|
|
/** |
|
22
|
|
|
* @var mixed |
|
23
|
|
|
*/ |
|
24
|
|
|
private $toggl; |
|
25
|
|
|
|
|
26
|
|
|
/** |
|
27
|
|
|
* @param Config $config |
|
28
|
|
|
*/ |
|
29
|
|
|
public function __construct(Config $config = null) |
|
30
|
|
|
{ |
|
31
|
|
|
$this->config = $config; |
|
32
|
|
|
$this->harvest = new Harvest($this->config->get('harvest', 'domain'), $this->config->get('harvest', 'api_token')); |
|
33
|
|
|
$this->toggl = new Toggl($this->config->get('toggl', 'api_token')); |
|
34
|
|
|
} |
|
35
|
|
|
|
|
36
|
|
|
/** |
|
37
|
|
|
* @param $service |
|
38
|
|
|
* @param $timerId |
|
39
|
|
|
* @return boolean |
|
40
|
|
|
*/ |
|
41
|
|
|
public function deleteServiceTimer($service, $timerId) |
|
42
|
|
|
{ |
|
43
|
|
|
if ($this->$service->deleteTimer($timerId) === false) { |
|
44
|
|
|
return false; |
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
|
|
if ($timerId === $this->config->get('workflow', 'timer_' . $service . '_id')) { |
|
48
|
|
|
$this->config->update('workflow', 'timer_' . $service . '_id', null); |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
|
|
return true; |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
|
/** |
|
55
|
|
|
* @param $timerId |
|
56
|
|
|
* @return string |
|
57
|
|
|
*/ |
|
58
|
|
|
public function deleteTimer($timerId) |
|
59
|
|
|
{ |
|
60
|
|
|
$message = ''; |
|
61
|
|
|
|
|
62
|
|
|
foreach ($this->config->implementedServicesForFeature('delete') as $service) { |
|
63
|
|
|
$res = $this->deleteServiceTimer($service, $timerId); |
|
64
|
|
|
$message .= $this->setNotificationForService($service, 'delete', $res); |
|
|
|
|
|
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
return $message; |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
|
|
/** |
|
71
|
|
|
* @param $service |
|
72
|
|
|
* @param null $action |
|
73
|
|
|
* @param null $success |
|
74
|
|
|
*/ |
|
75
|
|
|
public function setNotificationForService($service = null, $action = null, $success = false) |
|
76
|
|
|
{ |
|
77
|
|
|
if ($success === false) { |
|
78
|
|
|
return '- ' . ucfirst($service) . ': cannot ' . $action . ' [' . $this->$service->getLastMessage() . ']' . "\r\n"; |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
|
|
return '- ' . ucfirst($service) . ': ' . $action . "\r\n"; |
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
|
|
/** |
|
85
|
|
|
* @param $projectId |
|
86
|
|
|
* @return mixed |
|
87
|
|
|
*/ |
|
88
|
|
|
public function getProjectName($projectId) |
|
89
|
|
|
{ |
|
90
|
|
|
$projectName = ''; |
|
91
|
|
|
|
|
92
|
|
|
$projects = $this->getProjects(); |
|
93
|
|
|
|
|
94
|
|
|
foreach ($projects as $project) { |
|
95
|
|
|
if ($project['id'] === $projectId) { |
|
96
|
|
|
$projectName = $project['name']; |
|
97
|
|
|
break; |
|
98
|
|
|
} |
|
99
|
|
|
} |
|
100
|
|
|
|
|
101
|
|
|
return $projectName; |
|
102
|
|
|
} |
|
103
|
|
|
|
|
104
|
|
|
/** |
|
105
|
|
|
* @return mixed |
|
106
|
|
|
*/ |
|
107
|
|
View Code Duplication |
public function getProjects() |
|
|
|
|
|
|
108
|
|
|
{ |
|
109
|
|
|
$projects = []; |
|
110
|
|
|
|
|
111
|
|
|
/* |
|
112
|
|
|
* Temporary, only get the projects of Toggl |
|
113
|
|
|
* Later, we will get Harvest ones too |
|
114
|
|
|
*/ |
|
115
|
|
|
foreach ($this->config->implementedServicesForFeature('get_projects') as $service) { |
|
116
|
|
|
if ($this->config->isServiceActive($service) === true) { |
|
117
|
|
|
$projects = $this->$service->getProjects($this->getServiceDataCache($service)); |
|
118
|
|
|
} |
|
119
|
|
|
} |
|
120
|
|
|
|
|
121
|
|
|
return $projects; |
|
122
|
|
|
} |
|
123
|
|
|
|
|
124
|
|
|
/** |
|
125
|
|
|
* @return mixed |
|
126
|
|
|
*/ |
|
127
|
|
View Code Duplication |
public function getRecentTimers() |
|
|
|
|
|
|
128
|
|
|
{ |
|
129
|
|
|
$timers = []; |
|
130
|
|
|
|
|
131
|
|
|
foreach ($this->config->implementedServicesForFeature('get_timers') as $service) { |
|
132
|
|
|
if ($this->config->isServiceActive($service) === true) { |
|
133
|
|
|
$timers = array_merge($timers, $this->getRecentServiceTimers($service)); |
|
134
|
|
|
} |
|
135
|
|
|
} |
|
136
|
|
|
|
|
137
|
|
|
return $timers; |
|
138
|
|
|
} |
|
139
|
|
|
|
|
140
|
|
|
/** |
|
141
|
|
|
* @param $service |
|
142
|
|
|
* @return mixed |
|
143
|
|
|
*/ |
|
144
|
|
|
public function getServiceDataCache($service) |
|
145
|
|
|
{ |
|
146
|
|
|
$data = []; |
|
147
|
|
|
$cacheFile = getenv('alfred_workflow_data') . '/' . $service . '_cache.json'; |
|
148
|
|
|
|
|
149
|
|
|
if (file_exists($cacheFile)) { |
|
150
|
|
|
$data = json_decode(file_get_contents($cacheFile), true); |
|
151
|
|
|
} |
|
152
|
|
|
|
|
153
|
|
|
return $data; |
|
154
|
|
|
} |
|
155
|
|
|
|
|
156
|
|
|
/** |
|
157
|
|
|
* @return mixed |
|
158
|
|
|
*/ |
|
159
|
|
View Code Duplication |
public function getTags() |
|
|
|
|
|
|
160
|
|
|
{ |
|
161
|
|
|
$tags = []; |
|
162
|
|
|
|
|
163
|
|
|
/* |
|
164
|
|
|
* Temporary, only get the tags of Toggl |
|
165
|
|
|
* Later, we will get Harvest ones too |
|
166
|
|
|
*/ |
|
167
|
|
|
foreach ($this->config->implementedServicesForFeature('get_tags') as $service) { |
|
168
|
|
|
if ($this->config->isServiceActive($service) === true) { |
|
169
|
|
|
$tags = $this->$service->getTags($this->getServiceDataCache($service)); |
|
170
|
|
|
} |
|
171
|
|
|
} |
|
172
|
|
|
|
|
173
|
|
|
return $tags; |
|
174
|
|
|
} |
|
175
|
|
|
|
|
176
|
|
|
/** |
|
177
|
|
|
* @param $description |
|
178
|
|
|
* @param $projectsDefault |
|
179
|
|
|
* @param null $tagsDefault |
|
180
|
|
|
* @param string $startType |
|
181
|
|
|
* @return string |
|
182
|
|
|
*/ |
|
183
|
|
|
public function startTimer($description = '', array $projectsDefault = [], array $tagsDefault = [], $startType = 'start') |
|
184
|
|
|
{ |
|
185
|
|
|
$message = ''; |
|
186
|
|
|
$oneServiceStarted = false; |
|
187
|
|
|
$implementedServices = $this->config->implementedServicesForFeature($startType); |
|
188
|
|
|
|
|
189
|
|
|
/* |
|
190
|
|
|
* When starting a new timer, all the services timer IDs have to be put to null |
|
191
|
|
|
* so that when the user uses the UNDO feature, it doesn't delete old previous |
|
192
|
|
|
* other services timers. The timer IDs are used for the UNDO feature and |
|
193
|
|
|
* should then contain the IDs of the last starts through the workflow, not |
|
194
|
|
|
* through each individual sefrvice |
|
195
|
|
|
*/ |
|
196
|
|
|
if (empty($implementedServices) === true) { |
|
197
|
|
|
return ''; |
|
198
|
|
|
} |
|
199
|
|
|
|
|
200
|
|
|
foreach ($this->config->activatedServices() as $service) { |
|
201
|
|
|
$this->config->update('workflow', 'timer_' . $service . '_id', null); |
|
202
|
|
|
} |
|
203
|
|
|
|
|
204
|
|
|
foreach ($implementedServices as $service) { |
|
205
|
|
|
$defaultProjectId = isset($projectsDefault[$service]) ? $projectsDefault[$service] : null; |
|
206
|
|
|
$defaultTags = isset($tagsDefault[$service]) ? $tagsDefault[$service] : null; |
|
207
|
|
|
|
|
208
|
|
|
$timerId = $this->$service->startTimer($description, $defaultProjectId, $defaultTags); |
|
209
|
|
|
$this->config->update('workflow', 'timer_' . $service . '_id', $timerId); |
|
210
|
|
|
|
|
211
|
|
|
if ($timerId !== null) { |
|
212
|
|
|
$oneServiceStarted = true; |
|
213
|
|
|
$message .= '- ' . ucfirst($service) . ': started' . "\r\n"; |
|
214
|
|
|
} else { |
|
215
|
|
|
$message .= '- ' . ucfirst($service) . ': cannot start [' . $this->$service->getLastMessage() . ']' . "\r\n"; |
|
216
|
|
|
} |
|
217
|
|
|
} |
|
218
|
|
|
|
|
219
|
|
|
if ($oneServiceStarted === true) { |
|
220
|
|
|
$this->config->update('workflow', 'timer_description', $description); |
|
221
|
|
|
$this->config->update('workflow', 'is_timer_running', true); |
|
222
|
|
|
} |
|
223
|
|
|
|
|
224
|
|
|
return $message; |
|
225
|
|
|
} |
|
226
|
|
|
|
|
227
|
|
|
/** |
|
228
|
|
|
* @param $description |
|
229
|
|
|
* @return string |
|
230
|
|
|
*/ |
|
231
|
|
|
public function startTimerWithDefaultOptions($description) |
|
232
|
|
|
{ |
|
233
|
|
|
$projectsDefault = [ |
|
234
|
|
|
'toggl' => $this->config->get('toggl', 'default_project_id'), |
|
235
|
|
|
'harvest' => $this->config->get('harvest', 'default_project_id'), |
|
236
|
|
|
]; |
|
237
|
|
|
|
|
238
|
|
|
$tagsDefault = [ |
|
239
|
|
|
'toggl' => $this->config->get('toggl', 'default_tags'), |
|
240
|
|
|
'harvest' => $this->config->get('harvest', 'default_task_id'), |
|
241
|
|
|
]; |
|
242
|
|
|
|
|
243
|
|
|
return $this->startTimer($description, $projectsDefault, $tagsDefault, 'start_default'); |
|
244
|
|
|
} |
|
245
|
|
|
|
|
246
|
|
|
/** |
|
247
|
|
|
* @return string |
|
248
|
|
|
*/ |
|
249
|
|
|
public function stopRunningTimer() |
|
250
|
|
|
{ |
|
251
|
|
|
$message = ''; |
|
252
|
|
|
$oneServiceStopped = false; |
|
253
|
|
|
|
|
254
|
|
View Code Duplication |
foreach ($this->config->activatedServices() as $service) { |
|
|
|
|
|
|
255
|
|
|
$timerId = $this->config->get('workflow', 'timer_' . $service . '_id'); |
|
256
|
|
|
|
|
257
|
|
|
$res = $this->$service->stopTimer($timerId); |
|
258
|
|
|
$message .= $this->setNotificationForService($service, 'stop', $res); |
|
259
|
|
|
|
|
260
|
|
|
if ($res === true) { |
|
261
|
|
|
$oneServiceStopped = true; |
|
262
|
|
|
} |
|
263
|
|
|
} |
|
264
|
|
|
|
|
265
|
|
|
if ($oneServiceStopped === true) { |
|
266
|
|
|
$this->config->update('workflow', 'is_timer_running', false); |
|
267
|
|
|
} |
|
268
|
|
|
|
|
269
|
|
|
return $message; |
|
270
|
|
|
} |
|
271
|
|
|
|
|
272
|
|
|
/** |
|
273
|
|
|
* @return string |
|
274
|
|
|
*/ |
|
275
|
|
View Code Duplication |
public function syncOnlineDataToLocalCache() |
|
|
|
|
|
|
276
|
|
|
{ |
|
277
|
|
|
$message = ''; |
|
278
|
|
|
|
|
279
|
|
|
foreach ($this->config->implementedServicesForFeature('sync_data') as $service) { |
|
280
|
|
|
if ($this->config->isServiceActive($service) === true) { |
|
281
|
|
|
$message .= $this->syncServiceOnlineDataToLocalCache($service); |
|
282
|
|
|
} |
|
283
|
|
|
} |
|
284
|
|
|
|
|
285
|
|
|
return $message; |
|
286
|
|
|
} |
|
287
|
|
|
|
|
288
|
|
|
/** |
|
289
|
|
|
* @return string |
|
290
|
|
|
*/ |
|
291
|
|
|
public function undoTimer() |
|
292
|
|
|
{ |
|
293
|
|
|
$message = ''; |
|
294
|
|
|
|
|
295
|
|
|
if ($this->config->hasTimerRunning() === true) { |
|
296
|
|
|
$this->stopRunningTimer(); |
|
297
|
|
|
} |
|
298
|
|
|
|
|
299
|
|
|
$oneTimerDeleted = false; |
|
300
|
|
|
|
|
301
|
|
View Code Duplication |
foreach ($this->config->servicesToUndo() as $service) { |
|
|
|
|
|
|
302
|
|
|
$res = $this->deleteServiceTimer($service, $this->config->get('workflow', 'timer_' . $service . '_id')); |
|
303
|
|
|
$message .= $this->setNotificationForService($service, 'undo', $res); |
|
|
|
|
|
|
304
|
|
|
|
|
305
|
|
|
if ($res === true) { |
|
306
|
|
|
$oneTimerDeleted = true; |
|
307
|
|
|
} |
|
308
|
|
|
} |
|
309
|
|
|
|
|
310
|
|
|
if ($oneTimerDeleted === true) { |
|
311
|
|
|
$this->config->update('workflow', 'is_timer_running', false); |
|
312
|
|
|
} |
|
313
|
|
|
|
|
314
|
|
|
return $message; |
|
315
|
|
|
} |
|
316
|
|
|
|
|
317
|
|
|
/** |
|
318
|
|
|
* @return mixed |
|
319
|
|
|
*/ |
|
320
|
|
|
private function getRecentServiceTimers($service) |
|
321
|
|
|
{ |
|
322
|
|
|
return $this->$service->getRecentTimers(); |
|
323
|
|
|
} |
|
324
|
|
|
|
|
325
|
|
|
/** |
|
326
|
|
|
* @param $data |
|
327
|
|
|
* @param string $service |
|
328
|
|
|
*/ |
|
329
|
|
|
private function saveServiceDataCache($service, $data) |
|
330
|
|
|
{ |
|
331
|
|
|
$cacheFile = getenv('alfred_workflow_data') . '/' . $service . '_cache.json'; |
|
332
|
|
|
file_put_contents($cacheFile, json_encode($data)); |
|
333
|
|
|
} |
|
334
|
|
|
|
|
335
|
|
|
/** |
|
336
|
|
|
* @param string $service |
|
337
|
|
|
* @return string |
|
338
|
|
|
*/ |
|
339
|
|
|
private function syncServiceOnlineDataToLocalCache($service) |
|
340
|
|
|
{ |
|
341
|
|
|
$data = $this->$service->getOnlineData(); |
|
342
|
|
|
|
|
343
|
|
|
if (empty($data) === true) { |
|
344
|
|
|
return $this->setNotificationForService($service, 'data', false); |
|
345
|
|
|
} |
|
346
|
|
|
|
|
347
|
|
|
$this->saveServiceDataCache($service, $data); |
|
348
|
|
|
|
|
349
|
|
|
return $this->setNotificationForService($service, 'data', true); |
|
|
|
|
|
|
350
|
|
|
} |
|
351
|
|
|
} |
|
352
|
|
|
|
It seems like the type of the argument is not accepted by the function/method which you are calling.
In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.
We suggest to add an explicit type cast like in the following example: