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
|
|
View Code Duplication |
public function deleteTimer($timerId) |
|
|
|
|
59
|
|
|
{ |
60
|
|
|
$message = ''; |
61
|
|
|
|
62
|
|
|
foreach ($this->config->implementedServicesForFeature('delete') as $service) { |
63
|
|
|
if ($this->deleteServiceTimer($service, $timerId) === true) { |
64
|
|
|
$message .= '- ' . ucfirst($service) . ': deleted' . "\r\n"; |
65
|
|
|
} else { |
66
|
|
|
$message .= '- ' . ucfirst($service) . ': cannot delete' . "\r\n"; |
67
|
|
|
} |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
return $message; |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* @param $projectId |
75
|
|
|
* @return mixed |
76
|
|
|
*/ |
77
|
|
|
public function getProjectName($projectId) |
78
|
|
|
{ |
79
|
|
|
$projectName = ''; |
80
|
|
|
|
81
|
|
|
$projects = $this->getProjects(); |
82
|
|
|
|
83
|
|
|
foreach ($projects as $project) { |
84
|
|
|
if ($project['id'] === $projectId) { |
85
|
|
|
$projectName = $project['name']; |
86
|
|
|
break; |
87
|
|
|
} |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
return $projectName; |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* @return mixed |
95
|
|
|
*/ |
96
|
|
View Code Duplication |
public function getProjects() |
|
|
|
|
97
|
|
|
{ |
98
|
|
|
$projects = []; |
99
|
|
|
|
100
|
|
|
/* |
101
|
|
|
* Temporary, only get the projects of Toggl |
102
|
|
|
* Later, we will get Harvest ones too |
103
|
|
|
*/ |
104
|
|
|
foreach ($this->config->implementedServicesForFeature('get_projects') as $service) { |
105
|
|
|
if ($this->config->isServiceActive($service) === true) { |
106
|
|
|
$projects = $this->$service->getProjects($this->getServiceDataCache($service)); |
107
|
|
|
} |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
return $projects; |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
/** |
114
|
|
|
* @return mixed |
115
|
|
|
*/ |
116
|
|
View Code Duplication |
public function getRecentTimers() |
|
|
|
|
117
|
|
|
{ |
118
|
|
|
$timers = []; |
119
|
|
|
|
120
|
|
|
foreach ($this->config->implementedServicesForFeature('get_timers') as $service) { |
121
|
|
|
if ($this->config->isServiceActive($service) === true) { |
122
|
|
|
$timers = array_merge($timers, $this->getRecentServiceTimers($service)); |
123
|
|
|
} |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
return $timers; |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
/** |
130
|
|
|
* @param $service |
131
|
|
|
* @return mixed |
132
|
|
|
*/ |
133
|
|
|
public function getServiceDataCache($service) |
134
|
|
|
{ |
135
|
|
|
$data = []; |
136
|
|
|
$cacheFile = getenv('alfred_workflow_data') . '/' . $service . '_cache.json'; |
137
|
|
|
|
138
|
|
|
if (file_exists($cacheFile)) { |
139
|
|
|
$data = json_decode(file_get_contents($cacheFile), true); |
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
return $data; |
143
|
|
|
} |
144
|
|
|
|
145
|
|
|
/** |
146
|
|
|
* @return mixed |
147
|
|
|
*/ |
148
|
|
View Code Duplication |
public function getTags() |
|
|
|
|
149
|
|
|
{ |
150
|
|
|
$tags = []; |
151
|
|
|
|
152
|
|
|
/* |
153
|
|
|
* Temporary, only get the tags of Toggl |
154
|
|
|
* Later, we will get Harvest ones too |
155
|
|
|
*/ |
156
|
|
|
foreach ($this->config->implementedServicesForFeature('get_tags') as $service) { |
157
|
|
|
if ($this->config->isServiceActive($service) === true) { |
158
|
|
|
$tags = $this->$service->getTags($this->getServiceDataCache($service)); |
159
|
|
|
} |
160
|
|
|
} |
161
|
|
|
|
162
|
|
|
return $tags; |
163
|
|
|
} |
164
|
|
|
|
165
|
|
|
/** |
166
|
|
|
* @param $description |
167
|
|
|
* @param $projectsDefault |
168
|
|
|
* @param null $tagsDefault |
169
|
|
|
* @param boolean $startDefault |
170
|
|
|
* @return string |
171
|
|
|
*/ |
172
|
|
|
public function startTimer($description = '', $projectsDefault = null, $tagsDefault = null, $startDefault = false) |
173
|
|
|
{ |
174
|
|
|
$message = ''; |
175
|
|
|
$startType = $startDefault === true ? 'start_default' : 'start'; |
176
|
|
|
$atLeastOneServiceStarted = false; |
177
|
|
|
$implementedServices = $this->config->implementedServicesForFeature($startType); |
178
|
|
|
|
179
|
|
|
/* |
180
|
|
|
* When starting a new timer, all the services timer IDs have to be put to null |
181
|
|
|
* so that when the user uses the UNDO feature, it doesn't delete old previous |
182
|
|
|
* other services timers. The timer IDs are used for the UNDO feature and |
183
|
|
|
* should then contain the IDs of the last starts through the workflow, not |
184
|
|
|
* through each individual sefrvice |
185
|
|
|
*/ |
186
|
|
|
if (empty($implementedServices) === false) { |
187
|
|
|
foreach ($this->config->activatedServices() as $service) { |
188
|
|
|
$this->config->update('workflow', 'timer_' . $service . '_id', null); |
189
|
|
|
} |
190
|
|
|
|
191
|
|
|
foreach ($implementedServices as $service) { |
192
|
|
|
$defaultProjectId = isset($projectsDefault[$service]) ? $projectsDefault[$service] : null; |
193
|
|
|
$defaultTags = isset($tagsDefault[$service]) ? $tagsDefault[$service] : null; |
194
|
|
|
|
195
|
|
|
$timerId = $this->$service->startTimer($description, $defaultProjectId, $defaultTags); |
196
|
|
|
$this->config->update('workflow', 'timer_' . $service . '_id', $timerId); |
197
|
|
|
|
198
|
|
|
if ($timerId !== null) { |
199
|
|
|
$atLeastOneServiceStarted = true; |
200
|
|
|
$message .= '- ' . ucfirst($service) . ': started' . "\r\n"; |
201
|
|
|
} else { |
202
|
|
|
$message .= '- ' . ucfirst($service) . ': cannot start' . "\r\n"; |
203
|
|
|
} |
204
|
|
|
} |
205
|
|
|
} |
206
|
|
|
|
207
|
|
|
if ($atLeastOneServiceStarted === true) { |
208
|
|
|
$this->config->update('workflow', 'timer_description', $description); |
209
|
|
|
$this->config->update('workflow', 'is_timer_running', true); |
210
|
|
|
} |
211
|
|
|
|
212
|
|
|
return $message; |
213
|
|
|
} |
214
|
|
|
|
215
|
|
|
/** |
216
|
|
|
* @param $description |
217
|
|
|
* @return string |
218
|
|
|
*/ |
219
|
|
|
public function startTimerWithDefaultOptions($description) |
220
|
|
|
{ |
221
|
|
|
$projectsDefault = [ |
222
|
|
|
'toggl' => $this->config->get('toggl', 'default_project_id'), |
223
|
|
|
'harvest' => $this->config->get('harvest', 'default_project_id'), |
224
|
|
|
]; |
225
|
|
|
|
226
|
|
|
$tagsDefault = [ |
227
|
|
|
'toggl' => $this->config->get('toggl', 'default_tags'), |
228
|
|
|
'harvest' => $this->config->get('harvest', 'default_task_id'), |
229
|
|
|
]; |
230
|
|
|
|
231
|
|
|
return $this->startTimer($description, $projectsDefault, $tagsDefault, true); |
232
|
|
|
} |
233
|
|
|
|
234
|
|
|
/** |
235
|
|
|
* @return string |
236
|
|
|
*/ |
237
|
|
|
public function stopRunningTimer() |
238
|
|
|
{ |
239
|
|
|
$message = ''; |
240
|
|
|
$atLeastOneServiceStopped = false; |
241
|
|
|
|
242
|
|
View Code Duplication |
foreach ($this->config->activatedServices() as $service) { |
|
|
|
|
243
|
|
|
$timerId = $this->config->get('workflow', 'timer_' . $service . '_id'); |
244
|
|
|
|
245
|
|
|
if ($this->$service->stopTimer($timerId) === true) { |
246
|
|
|
$atLeastOneServiceStopped = true; |
247
|
|
|
$message .= '- ' . ucfirst($service) . ': stopped' . "\r\n"; |
248
|
|
|
} else { |
249
|
|
|
$message .= '- ' . ucfirst($service) . ': cannot stop' . "\r\n"; |
250
|
|
|
} |
251
|
|
|
} |
252
|
|
|
|
253
|
|
|
if ($atLeastOneServiceStopped === true) { |
254
|
|
|
$this->config->update('workflow', 'is_timer_running', false); |
255
|
|
|
} |
256
|
|
|
|
257
|
|
|
return $message; |
258
|
|
|
} |
259
|
|
|
|
260
|
|
|
/** |
261
|
|
|
* @return string |
262
|
|
|
*/ |
263
|
|
View Code Duplication |
public function syncOnlineDataToLocalCache() |
|
|
|
|
264
|
|
|
{ |
265
|
|
|
$message = ''; |
266
|
|
|
|
267
|
|
|
foreach ($this->config->implementedServicesForFeature('sync_data') as $service) { |
268
|
|
|
if ($this->config->isServiceActive($service) === true) { |
269
|
|
|
$message .= $this->syncServiceOnlineDataToLocalCache($service); |
270
|
|
|
} |
271
|
|
|
} |
272
|
|
|
|
273
|
|
|
return $message; |
274
|
|
|
} |
275
|
|
|
|
276
|
|
|
/** |
277
|
|
|
* @return string |
278
|
|
|
*/ |
279
|
|
|
public function undoTimer() |
280
|
|
|
{ |
281
|
|
|
$message = ''; |
282
|
|
|
|
283
|
|
|
if ($this->config->hasTimerRunning() === true) { |
284
|
|
|
$this->stopRunningTimer(); |
285
|
|
|
} |
286
|
|
|
|
287
|
|
|
$atLeastOneTimerDeleted = false; |
288
|
|
|
|
289
|
|
View Code Duplication |
foreach ($this->config->servicesToUndo() as $service) { |
|
|
|
|
290
|
|
|
if ($this->deleteServiceTimer($service, $this->config->get('workflow', 'timer_' . $service . '_id')) === true) { |
291
|
|
|
$atLeastOneTimerDeleted = true; |
292
|
|
|
$message .= '- ' . ucfirst($service) . ': undid' . "\r\n"; |
293
|
|
|
} else { |
294
|
|
|
$message .= '- ' . ucfirst($service) . ': cannot undo' . "\r\n"; |
295
|
|
|
} |
296
|
|
|
} |
297
|
|
|
|
298
|
|
|
if ($atLeastOneTimerDeleted === true) { |
299
|
|
|
$this->config->update('workflow', 'is_timer_running', false); |
300
|
|
|
} |
301
|
|
|
|
302
|
|
|
return $message; |
303
|
|
|
} |
304
|
|
|
|
305
|
|
|
/** |
306
|
|
|
* @return mixed |
307
|
|
|
*/ |
308
|
|
|
private function getRecentServiceTimers($service) |
309
|
|
|
{ |
310
|
|
|
return $this->$service->getRecentTimers(); |
311
|
|
|
} |
312
|
|
|
|
313
|
|
|
/** |
314
|
|
|
* @param $data |
315
|
|
|
* @param string $service |
316
|
|
|
*/ |
317
|
|
|
private function saveServiceDataCache($service, $data) |
318
|
|
|
{ |
319
|
|
|
$cacheFile = getenv('alfred_workflow_data') . '/' . $service . '_cache.json'; |
320
|
|
|
file_put_contents($cacheFile, json_encode($data)); |
321
|
|
|
} |
322
|
|
|
|
323
|
|
|
/** |
324
|
|
|
* @param string $service |
325
|
|
|
* @return string |
326
|
|
|
*/ |
327
|
|
View Code Duplication |
private function syncServiceOnlineDataToLocalCache($service) |
|
|
|
|
328
|
|
|
{ |
329
|
|
|
$message = ''; |
330
|
|
|
$data = $this->$service->getOnlineData(); |
331
|
|
|
|
332
|
|
|
if (empty($data) === false) { |
333
|
|
|
$this->saveServiceDataCache($service, $data); |
334
|
|
|
$message .= '- ' . ucfirst($service) . ': data cached' . "\r\n"; |
335
|
|
|
} else { |
336
|
|
|
$message .= '- ' . ucfirst($service) . ': cannot cache data' . "\r\n"; |
337
|
|
|
} |
338
|
|
|
|
339
|
|
|
return $message; |
340
|
|
|
} |
341
|
|
|
} |
342
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.