Completed
Push — master ( b0e7fe...884141 )
by Guillaume
02:00
created

AlfredTime::deleteHarvestTimer()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 4
nc 1
nop 1
dl 0
loc 7
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
require 'Toggl.class.php';
4
require 'Harvest.class.php';
5
6
class AlfredTime
7
{
8
    private $config;
9
    private $message;
10
    private $currentImplementation = [
11
        'start' => ['toggl'],
12
        'start_default' => ['toggl', 'harvest'],
13
        'stop' => ['toggl', 'harvest'],
14
        'delete' => ['toggl'],
15
    ];
16
    private $toggl;
17
    private $harvest;
18
19
    public function __construct()
20
    {
21
        $this->config = $this->loadConfiguration();
22
        $this->message = '';
23
        $this->toggl = new Toggl($this->config['toggl']['api_token']);
24
        $this->harvest = new Harvest($this->config['harvest']['domain'], $this->config['harvest']['api_token']);
25
    }
26
27
    public function isConfigured()
28
    {
29
        return $this->config === null ? false : true;
30
    }
31
32
    public function hasTimerRunning()
33
    {
34
        return $this->config['workflow']['is_timer_running'] === false ? false : true;
35
    }
36
37
    public function getTimerDescription()
38
    {
39
        return $this->config['workflow']['timer_description'];
40
    }
41
42
    public function startTimer($description = '', $projectsDefault = null, $tagsDefault = null, $startDefault = false)
43
    {
44
        $message = '';
45
        $startType = $startDefault === true ? 'start_default' : 'start';
46
        $atLeastOneServiceStarted = false;
47
        $implementedServices = $this->implementedServicesForFeature($startType);
48
49
        /**
50
         * When starting a new timer, all the services timer IDs have to be put to null
51
         * so that when the user uses the UNDO feature, it doesn't delete old previous
52
         * other services timers. The timer IDs are used for the UNDO feature and
53
         * should then contain the IDs of the last starts through the workflow, not
54
         * through each individual service
55
         */
56
        if (empty($implementedServices) === false) {
57
            foreach ($this->activatedServices() as $service) {
58
                $this->config['workflow']['timer_' . $service . '_id'] = null;
59
                $this->saveConfiguration();
60
            }
61
        }
62
63
        foreach ($implementedServices as $service) {
64
            $defaultProjectId = isset($projectsDefault[$service]) ? $projectsDefault[$service] : null;
65
            $defaultTags = isset($tagsDefault[$service]) ? $tagsDefault[$service] : null;
66
67
            $functionName = 'start' . ucfirst($service) . 'Timer';
68
            $timerId = call_user_func_array(['AlfredTime', $functionName], [$description, $defaultProjectId, $defaultTags]);
69
            $this->config['workflow']['timer_' . $service . '_id'] = $timerId;
70
            if ($timerId !== null) {
71
                $atLeastOneServiceStarted = true;
72
            }
73
74
            $message .= $this->getLastMessage() . "\r\n";
75
        }
76
77
        if ($atLeastOneServiceStarted === true) {
78
            $this->config['workflow']['timer_description'] = $description;
79
            $this->config['workflow']['is_timer_running'] = true;
80
            $this->saveConfiguration();
81
        }
82
83
        return $message;
84
    }
85
86
    public function startTimerWithDefaultOptions($description)
87
    {
88
        $projectsDefault = [
89
            'toggl' => $this->config['toggl']['default_project_id'],
90
            'harvest' => $this->config['harvest']['default_project_id'],
91
        ];
92
93
        $tagsDefault = [
94
            'toggl' => $this->config['toggl']['default_tags'],
95
            'harvest' => $this->config['harvest']['default_task_id'],
96
        ];
97
98
        return $this->startTimer($description, $projectsDefault, $tagsDefault, true);
99
    }
100
101
    public function stopRunningTimer()
102
    {
103
        $message = '';
104
        $atLeastOneServiceStopped = false;
105
106
        foreach ($this->activatedServices() as $service) {
107
            $functionName = 'stop' . ucfirst($service) . 'Timer';
108
            if (call_user_func(['AlfredTime', $functionName]) === true) {
109
                $atLeastOneServiceStopped = true;
110
            }
111
            $message .= $this->getLastMessage() . "\r\n";
112
        }
113
114 View Code Duplication
        if ($atLeastOneServiceStopped === true) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

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.

Loading history...
115
            $this->config['workflow']['is_timer_running'] = false;
116
            $this->saveConfiguration();
117
        }
118
119
        return $message;
120
    }
121
122
    public function generateDefaultConfigurationFile()
123
    {
124
        $this->config = [
125
            'workflow' => [
126
                'is_timer_running' => false,
127
                'timer_toggl_id' => null,
128
                'timer_harvest_id' => null,
129
                'timer_description' => '',
130
            ],
131
            'toggl' => [
132
                'is_active' => true,
133
                'api_token' => '',
134
                'default_project_id' => '',
135
                'default_tags' => '',
136
            ],
137
            'harvest' => [
138
                'is_active' => true,
139
                'domain' => '',
140
                'api_token' => '',
141
                'default_project_id' => '',
142
                'default_task_id' => '',
143
            ],
144
        ];
145
146
        $this->saveConfiguration();
147
    }
148
149
    public function activatedServices()
150
    {
151
        $services = [];
152
153
        if ($this->isTogglActive() === true) {
154
            array_push($services, 'toggl');
155
        }
156
157
        if ($this->isHarvestActive() === true) {
158
            array_push($services, 'harvest');
159
        }
160
161
        return $services;
162
    }
163
164
    public function servicesToUndo()
165
    {
166
        $services = [];
167
168
        foreach ($this->activatedServices() as $service) {
169
            if ($this->config['workflow']['timer_' . $service . '_id'] !== null) {
170
                array_push($services, $service);
171
            }
172
        }
173
174
        return $services;
175
    }
176
177
    public function syncOnlineDataToLocalCache()
178
    {
179
        $message = '';
180
181
        if ($this->isTogglActive() === true) {
182
            $message .= $this->syncTogglOnlineDataToLocalCache();
183
        }
184
185
        return $message;
186
    }
187
188
    public function getProjects()
189
    {
190
        $projects = [];
191
192
        if ($this->isTogglActive() === true) {
193
            $projects = array_merge($projects, $this->getTogglProjects());
194
        }
195
196
        return $projects;
197
    }
198
199
    public function getTags()
200
    {
201
        $tags = [];
202
203
        if ($this->isTogglActive() === true) {
204
            $tags = array_merge($tags, $this->getTogglTags());
205
        }
206
207
        return $tags;
208
    }
209
210
    public function UndoTimer()
211
    {
212
        $message = '';
213
214
        if ($this->hasTimerRunning() === true) {
215
            $this->stopRunningTimer();
216
        }
217
218
        $atLeastOneTimerDeleted = false;
219
        foreach ($this->servicesToUndo() as $service) {
220
            $functionName = 'delete' . ucfirst($service) . 'Timer';
221
            if (call_user_func_array(['AlfredTime', $functionName], [$this->config['workflow']['timer_' . $service . '_id']]) === true) {
222
                $this->config['workflow']['timer_' . $service . '_id'] = null;
223
                $atLeastOneTimerDeleted = true;
224
            }
225
226
            $message .= $this->getLastMessage() . "\r\n";
227
        }
228
229
        if ($atLeastOneTimerDeleted === true) {
230
            $this->saveConfiguration();
231
        }
232
233
        return $message;
234
    }
235
236
    public function getRecentTimers()
237
    {
238
        $timers = [];
239
240
        if ($this->isTogglActive() === true) {
241
            $timers = array_merge($timers, $this->getRecentTogglTimers());
242
        }
243
244
        return $timers;
245
    }
246
247
    public function deleteTimer($timerId)
248
    {
249
        $message = '';
250
251
        $atLeastOneTimerDeleted = false;
252
        foreach ($this->implementedServicesForFeature('delete') as $service) {
253
            $functionName = 'delete' . ucfirst($service) . 'Timer';
254
            if (call_user_func_array(['AlfredTime', $functionName], [$timerId]) === true) {
255
                $this->config['workflow']['timer_' . $service . '_id'] = null;
256
                $atLeastOneTimerDeleted = true;
257
            }
258
259
            $message .= $this->getLastMessage() . "\r\n";
260
        }
261
262 View Code Duplication
        if ($atLeastOneTimerDeleted === true) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

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.

Loading history...
263
            $this->config['workflow']['is_timer_running'] = false;
264
            $this->saveConfiguration();
265
        }
266
267
        return $message;
268
    }
269
270
    public function implementedServicesForFeature($feature = null)
271
    {
272
        $services = [];
273
274
        if (isset($this->currentImplementation[$feature]) === true) {
275
            $services = $this->currentImplementation[$feature];
276
        }
277
278
        return $services;
279
    }
280
281
    public function getProjectName($projectId)
282
    {
283
        $projectName = '';
284
285
        $projects = $this->getProjects();
286
        foreach ($projects as $project) {
287
            if ($project['id'] === $projectId) {
288
                $projectName = $project['name'];
289
                break;
290
            }
291
        }
292
293
        return $projectName;
294
    }
295
296
    private function getRecentTogglTimers()
297
    {
298
        return $this->toggl->getRecentTimers();
299
    }
300
301
    private function getLastMessage()
302
    {
303
        return $this->message;
304
    }
305
306
    private function deleteTogglTimer($togglId)
307
    {
308
        $res = $this->toggl->deleteTimer($togglId);
309
        $this->message = $this->toggl->getLastMessage();
310
311
        return $res;
312
    }
313
314
    private function deleteHarvestTimer($harvestId)
315
    {
316
        $res = $this->harvest->deleteTimer($harvestId);
317
        $this->message = $this->harvest->getLastMessage();
318
319
        return $res;
320
    }
321
322
    private function syncTogglOnlineDataToLocalCache()
323
    {
324
        $data = $this->toggl->getOnlineData();
325
        $this->message = $this->toggl->getLastMessage();
326
327
        if (empty($data) === false) {
328
            $this->saveTogglDataCache($data);
329
        }
330
331
        return $this->message;
332
    }
333
334
    private function saveTogglDataCache($data)
335
    {
336
        $cacheFile = getenv('alfred_workflow_data') . '/toggl_cache.json';
337
        file_put_contents($cacheFile, json_encode($data));
338
    }
339
340
    private function loadConfiguration()
341
    {
342
        $config = null;
343
        $configFile = getenv('alfred_workflow_data') . '/config.json';
344
345
        if (file_exists($configFile)) {
346
            $config = json_decode(file_get_contents($configFile), true);
347
        }
348
349
        return $config;
350
    }
351
352
    private function saveConfiguration()
353
    {
354
        $workflowDir = getenv('alfred_workflow_data');
355
        $configFile = $workflowDir . '/config.json';
356
357
        if (file_exists($workflowDir) === false) {
358
            mkdir($workflowDir);
359
        }
360
361
        file_put_contents($configFile, json_encode($this->config, JSON_PRETTY_PRINT));
362
    }
363
364
    private function startTogglTimer($description, $projectId, $tagNames)
365
    {
366
        $togglId = $this->toggl->startTimer($description, $projectId, $tagNames);
367
        $this->message = $this->toggl->getLastMessage();
368
369
        return $togglId;
370
    }
371
372 View Code Duplication
    private function stopTogglTimer()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
373
    {
374
        $togglId = $this->config['workflow']['timer_toggl_id'];
375
376
        $res = $this->toggl->stopTimer($togglId);
377
        $this->message = $this->toggl->getLastMessage();
378
379
        return $res;
380
    }
381
382
    private function startHarvestTimer($description, $projectId, $taskId)
383
    {
384
        $harvestId = $this->harvest->startTimer($description, $projectId, $taskId);
385
        $this->message = $this->harvest->getLastMessage();
386
387
        return $harvestId;
388
    }
389
390 View Code Duplication
    private function stopHarvestTimer()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
391
    {
392
        $harvestId = $this->config['workflow']['timer_harvest_id'];
393
394
        $res = $this->harvest->stopTimer($harvestId);
395
        $this->message = $this->harvest->getLastMessage();
396
397
        return $res;
398
    }
399
400
    private function getTogglProjects()
401
    {
402
        $cacheData = [];
403
        $cacheFile = getenv('alfred_workflow_data') . '/toggl_cache.json';
404
405
        if (file_exists($cacheFile)) {
406
            $cacheData = json_decode(file_get_contents($cacheFile), true);
407
        }
408
409
        /**
410
         * To only show projects that are currently active
411
         * The Toggl API is slightly weird on that
412
         */
413
        foreach ($cacheData['data']['projects'] as $key => $project) {
414
            if (isset($project['server_deleted_at']) === true) {
415
                unset($cacheData['data']['projects'][$key]);
416
            }
417
        }
418
419
        return $cacheData['data']['projects'];
420
    }
421
422
    private function getTogglTags()
423
    {
424
        $cacheFile = getenv('alfred_workflow_data') . '/toggl_cache.json';
425
        $cacheData = [];
426
427
        if (file_exists($cacheFile)) {
428
            $cacheData = json_decode(file_get_contents($cacheFile), true);
429
        }
430
431
        return $cacheData['data']['tags'];
432
    }
433
434
    private function isTogglActive()
435
    {
436
        return $this->config['toggl']['is_active'];
437
    }
438
439
    private function isHarvestActive()
440
    {
441
        return $this->config['harvest']['is_active'];
442
    }
443
}
444