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