|
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
|
74 |
|
public function __construct($apiToken) |
|
14
|
|
|
{ |
|
15
|
74 |
|
$this->client = new Client([ |
|
16
|
74 |
|
'base_uri' => 'https://api.everhour.com/', |
|
17
|
|
|
'headers' => [ |
|
18
|
74 |
|
'X-Api-Key' => $apiToken |
|
19
|
|
|
] |
|
20
|
|
|
]); |
|
21
|
74 |
|
} |
|
22
|
|
|
|
|
23
|
26 |
|
public function projects() |
|
24
|
|
|
{ |
|
25
|
|
|
try { |
|
26
|
26 |
|
$response = $this->client->get('projects'); |
|
27
|
21 |
|
$projects = json_decode($response->getBody()->getContents()); |
|
28
|
|
|
|
|
29
|
21 |
|
return array_column($projects, 'name', 'id'); |
|
30
|
5 |
|
} catch (\Exception $e) { |
|
31
|
5 |
|
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
|
7 |
|
$tasks = json_decode($response->getBody()->getContents()); |
|
41
|
|
|
|
|
42
|
|
|
array_walk($tasks, function ($task) use (&$tags) { |
|
43
|
7 |
|
$tags[$task->id] = $task->name; |
|
44
|
7 |
|
}); |
|
45
|
|
|
|
|
46
|
7 |
|
return $tags; |
|
47
|
5 |
|
} catch (\Exception $e) { |
|
48
|
5 |
|
return []; |
|
49
|
|
|
} |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
14 |
|
public function pastTimers() |
|
53
|
|
|
{ |
|
54
|
|
|
try { |
|
55
|
14 |
|
$response = $this->client->get('users/me'); |
|
56
|
14 |
|
$me = json_decode($response->getBody()->getContents()); |
|
57
|
|
|
|
|
58
|
14 |
|
$response = $this->client->get("users/{$me->id}/time?limit=20&offset=0"); |
|
59
|
14 |
|
$everhourTimers = json_decode($response->getBody()->getContents()); |
|
60
|
|
|
|
|
61
|
14 |
|
return $this->convertToPastTimers($everhourTimers); |
|
62
|
|
|
} catch (\Exception $e) { |
|
63
|
|
|
return []; |
|
64
|
|
|
} |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
35 |
|
public function startTimer() |
|
68
|
|
|
{ |
|
69
|
|
|
try { |
|
70
|
35 |
|
$response = $this->client->post('timers', [ |
|
71
|
|
|
'json' => [ |
|
72
|
35 |
|
'task' => getenv('timer_tag_id'), |
|
73
|
35 |
|
'comment' => getenv('timer_description') |
|
74
|
|
|
] |
|
75
|
|
|
]); |
|
76
|
|
|
|
|
77
|
35 |
|
$timer = json_decode($response->getBody()->getContents()); |
|
78
|
|
|
|
|
79
|
35 |
|
if (! isset($timer->status) || $timer->status !== 'active') { |
|
80
|
35 |
|
return false; |
|
81
|
|
|
} |
|
82
|
|
|
} catch (\Exception $e) { |
|
83
|
|
|
return false; |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
35 |
|
return true; |
|
87
|
|
|
} |
|
88
|
|
|
|
|
89
|
35 |
|
public function stopCurrentTimer() |
|
90
|
|
|
{ |
|
91
|
|
|
try { |
|
92
|
35 |
|
$response = $this->client->delete('timers/current'); |
|
93
|
|
|
|
|
94
|
35 |
|
$timer = json_decode($response->getBody()->getContents()); |
|
95
|
|
|
|
|
96
|
35 |
|
if (! isset($timer->taskTime) || $timer->status !== 'stopped') { |
|
97
|
35 |
|
return false; |
|
98
|
|
|
} |
|
99
|
|
|
} catch (\Exception $e) { |
|
100
|
|
|
return false; |
|
101
|
|
|
} |
|
102
|
|
|
|
|
103
|
35 |
|
return true; |
|
104
|
|
|
} |
|
105
|
|
|
|
|
106
|
7 |
|
public function runningTimer() |
|
107
|
|
|
{ |
|
108
|
|
|
try { |
|
109
|
7 |
|
$response = $this->client->get('timers/current'); |
|
110
|
|
|
|
|
111
|
7 |
|
$timer = json_decode($response->getBody()->getContents()); |
|
112
|
|
|
|
|
113
|
7 |
|
if (! isset($timer->duration) || $timer->status !== 'active') { |
|
114
|
7 |
|
return false; |
|
115
|
|
|
} |
|
116
|
|
|
} catch (\Exception $e) { |
|
117
|
|
|
return false; |
|
118
|
|
|
} |
|
119
|
|
|
|
|
120
|
7 |
|
return true; |
|
121
|
|
|
} |
|
122
|
|
|
|
|
123
|
7 |
|
public function continueTimer($timerId = null) |
|
124
|
|
|
{ |
|
125
|
|
|
/** |
|
126
|
|
|
* Timer attributes are stored in env variables |
|
127
|
|
|
* gathered in startTimer. |
|
128
|
|
|
*/ |
|
129
|
|
|
|
|
130
|
7 |
|
return $this->startTimer(); |
|
131
|
|
|
} |
|
132
|
|
|
|
|
133
|
14 |
|
protected function convertToPastTimers($everhourTimers) |
|
134
|
|
|
{ |
|
135
|
14 |
|
$projects = $this->projects(); |
|
136
|
|
|
|
|
137
|
|
|
return array_map(function ($everhourTimer) use ($projects) { |
|
138
|
14 |
|
return $this->buildPastTimerObject($everhourTimer, $projects); |
|
139
|
14 |
|
}, $everhourTimers); |
|
140
|
|
|
} |
|
141
|
|
|
|
|
142
|
14 |
|
protected function buildPastTimerObject($everhourTimer, $projects) |
|
143
|
|
|
{ |
|
144
|
14 |
|
$pastTimer['id'] = $everhourTimer->id; |
|
|
|
|
|
|
145
|
14 |
|
$pastTimer['description'] = $everhourTimer->comment ?? ''; |
|
146
|
|
|
|
|
147
|
14 |
|
if (isset($everhourTimer->task)) { |
|
148
|
14 |
|
$pastTimer['project_id'] = $everhourTimer->task->projects[0]; |
|
149
|
14 |
|
$pastTimer['project_name'] = $projects[$everhourTimer->task->projects[0]]; |
|
150
|
14 |
|
$pastTimer['tag_id'] = $everhourTimer->task->id; |
|
151
|
14 |
|
$pastTimer['tags'] = $everhourTimer->task->name; |
|
152
|
|
|
} |
|
153
|
|
|
|
|
154
|
14 |
|
$pastTimer['duration'] = CarbonInterval::seconds( |
|
155
|
14 |
|
floor($everhourTimer->time) |
|
156
|
14 |
|
)->cascade()->format('%H:%I:%S'); |
|
157
|
|
|
|
|
158
|
14 |
|
return (object) $pastTimer; |
|
159
|
|
|
} |
|
160
|
|
|
} |
|
161
|
|
|
|