Passed
Pull Request — master (#30)
by Guillaume
36:47 queued 35:00
created

Everhour::deleteTimer()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
nc 1
nop 1
dl 0
loc 3
ccs 0
cts 2
cp 0
crap 2
rs 10
c 1
b 0
f 0
1
<?php
2
3
namespace Godbout\Alfred\Time\Services;
4
5
use Carbon\CarbonInterval;
6
use GuzzleHttp\Client;
7
8
class Everhour extends TimerService
9
{
10
    private $client;
11
12
13 78
    public function __construct($apiToken)
14
    {
15 78
        $this->client = new Client([
16 78
            'base_uri' => 'https://api.everhour.com/',
17
            'headers' => [
18 78
                'X-Api-Key' => $apiToken
19
            ]
20
        ]);
21 78
    }
22
23 30
    public function projects()
24
    {
25
        try {
26 30
            $response = $this->client->get('projects');
27 27
            $projects = json_decode($response->getBody()->getContents());
28
29 27
            return array_column($projects, 'name', 'id');
30 3
        } catch (\Exception $e) {
31 3
            return [];
32
        }
33
    }
34
35 12
    public function tags()
36
    {
37
        try {
38 12
            $projectId = getenv('timer_project_id');
39 12
            $response = $this->client->get("projects/$projectId/tasks");
40 9
            $tasks = json_decode($response->getBody()->getContents());
41
42
            array_walk($tasks, function ($task) use (&$tags) {
43 9
                $tags[$task->id] = $task->name;
44 9
            });
45
46 9
            return $tags;
47 3
        } catch (\Exception $e) {
48 3
            return [];
49
        }
50
    }
51
52 18
    public function pastTimers()
53
    {
54
        try {
55 18
            $response = $this->client->get('users/me');
56 18
            $me = json_decode($response->getBody()->getContents());
57
58 18
            $response = $this->client->get("users/{$me->id}/time?limit=20&offset=0");
59 18
            $everhourTimers = json_decode($response->getBody()->getContents());
60
61 18
            return $this->convertToPastTimers($everhourTimers);
62
        } catch (\Exception $e) {
63
            return [];
64
        }
65
    }
66
67 45
    public function startTimer()
68
    {
69
        try {
70 45
            $response = $this->client->post('timers', [
71
                'json' => [
72 45
                    'task' => getenv('timer_tag_id'),
73 45
                    'comment' => getenv('timer_description')
74
                ]
75
            ]);
76
77 45
            $timer = json_decode($response->getBody()->getContents());
78
79 45
            if (! isset($timer->status) || $timer->status !== 'active') {
80 45
                return false;
81
            }
82
        } catch (\Exception $e) {
83
            return false;
84
        }
85
86 45
        return true;
87
    }
88
89 45
    public function stopCurrentTimer()
90
    {
91
        try {
92 45
            $response = $this->client->delete('timers/current');
93
94 45
            $timer = json_decode($response->getBody()->getContents());
95
96 45
            if (! isset($timer->taskTime) || $timer->status !== 'stopped') {
97 45
                return false;
98
            }
99
        } catch (\Exception $e) {
100
            return false;
101
        }
102
103 45
        return true;
104
    }
105
106 9
    public function runningTimer()
107
    {
108
        try {
109 9
            $response = $this->client->get('timers/current');
110
111 9
            $timer = json_decode($response->getBody()->getContents());
112
113 9
            if (! isset($timer->duration) || $timer->status !== 'active') {
114 9
                return false;
115
            }
116
        } catch (\Exception $e) {
117
            return false;
118
        }
119
120 9
        return true;
121
    }
122
123 9
    public function continueTimer($timerId = null)
124
    {
125
        /**
126
         * Timer attributes are stored in env variables
127
         * gathered in startTimer.
128
         */
129
130 9
        return $this->startTimer();
131
    }
132
133 18
    protected function convertToPastTimers($everhourTimers)
134
    {
135 18
        $projects = $this->projects();
136
137
        return array_map(function ($everhourTimer) use ($projects) {
138 18
            return $this->buildPastTimerObject($everhourTimer, $projects);
139 18
        }, $everhourTimers);
140
    }
141
142 18
    protected function buildPastTimerObject($everhourTimer, $projects)
143
    {
144 18
        $pastTimer['id'] = $everhourTimer->id;
0 ignored issues
show
Comprehensibility Best Practice introduced by
$pastTimer was never initialized. Although not strictly required by PHP, it is generally a good practice to add $pastTimer = array(); before regardless.
Loading history...
145 18
        $pastTimer['description'] = $everhourTimer->comment ?? '';
146
147 18
        if (isset($everhourTimer->task)) {
148 18
            $pastTimer['project_id'] = $everhourTimer->task->projects[0];
149 18
            $pastTimer['project_name'] = $projects[$everhourTimer->task->projects[0]];
150 18
            $pastTimer['tag_id'] = $everhourTimer->task->id;
151 18
            $pastTimer['tags'] = $everhourTimer->task->name;
152
        }
153
154 18
        $pastTimer['duration'] = CarbonInterval::seconds(
155 18
            floor($everhourTimer->time)
156 18
        )->cascade()->format('%H:%I:%S');
157
158 18
        return (object) $pastTimer;
159
    }
160
}
161