1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Godbout\Alfred\Time\Services; |
4
|
|
|
|
5
|
|
|
use Carbon\Carbon; |
6
|
|
|
use JDecool\Clockify\ClientBuilder; |
7
|
|
|
|
8
|
|
|
class Clockify extends TimerService |
9
|
|
|
{ |
10
|
|
|
private $client; |
11
|
|
|
|
12
|
|
|
private $data = null; |
|
|
|
|
13
|
|
|
|
14
|
22 |
|
|
15
|
|
|
public function __construct($apiToken) |
16
|
22 |
|
{ |
17
|
22 |
|
$this->client = (new ClientBuilder())->createClientV1($apiToken); |
18
|
|
|
} |
19
|
11 |
|
|
20
|
|
|
public function workspaces() |
21
|
|
|
{ |
22
|
11 |
|
try { |
23
|
11 |
|
return $this->client->get('workspaces'); |
24
|
11 |
|
} catch (\Exception $e) { |
25
|
|
|
return []; |
26
|
|
|
} |
27
|
|
|
} |
28
|
11 |
|
|
29
|
|
|
public function projects() |
30
|
|
|
{ |
31
|
11 |
|
try { |
32
|
|
|
$workspaceId = getenv('timer_workspace_id'); |
33
|
11 |
|
|
34
|
11 |
|
$projects = $this->client->get("workspaces/$workspaceId/projects"); |
35
|
11 |
|
|
36
|
|
|
return array_column($projects, 'name', 'id'); |
37
|
|
|
} catch (\Exception $e) { |
38
|
|
|
return []; |
39
|
|
|
} |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
public function tags() |
43
|
|
|
{ |
44
|
|
|
try { |
45
|
|
|
$workspaceId = getenv('timer_workspace_id'); |
46
|
|
|
|
47
|
|
|
$tags = $this->client->get("workspaces/$workspaceId/tags"); |
48
|
|
|
|
49
|
|
|
return array_column($tags, 'name', 'id'); |
50
|
|
|
} catch (\Exception $e) { |
51
|
|
|
return []; |
52
|
|
|
} |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
public function startTimer() |
56
|
|
|
{ |
57
|
|
|
try { |
58
|
|
|
$workspaceId = getenv('timer_workspace_id'); |
59
|
22 |
|
|
60
|
|
|
$timer = $this->client->post("workspaces/$workspaceId/time-entries", [ |
61
|
22 |
|
'start' => (new \DateTime())->format('Y-m-d\TH:i:s\Z'), |
62
|
|
|
'description' => getenv('timer_description'), |
63
|
|
|
'projectId' => getenv('timer_project_id'), |
64
|
|
|
'tagIds' => getenv('timer_tag_id') ? [getenv('timer_tag_id')] : [''], |
65
|
|
|
]); |
66
|
|
|
|
67
|
|
|
if (! isset($timer['id'])) { |
68
|
|
|
return false; |
69
|
|
|
} |
70
|
|
|
} catch (Exception $e) { |
|
|
|
|
71
|
|
|
return false; |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
return $timer['id']; |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
public function stopCurrentTimer() |
78
|
|
|
{ |
79
|
|
|
$workspaceId = getenv('timer_workspace_id'); |
80
|
|
|
$userId = getenv('timer_user_id'); |
81
|
|
|
|
82
|
|
|
if ($timerId = $this->runningTimer()) { |
|
|
|
|
83
|
|
|
$timer = $this->client->patch("workspaces/$workspaceId/user/$userId/time-entries", [ |
84
|
|
|
'end' => (new \DateTime())->format('Y-m-d\TH:i:s\Z'), |
85
|
|
|
]) ; |
86
|
|
|
|
87
|
|
|
if (! isset($timer['timeInterval']['end'])) { |
88
|
|
|
throw new Exception("Can't stop current running timer.", 1); |
89
|
|
|
|
90
|
|
|
return false; |
|
|
|
|
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
return true; |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
return false; |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
public function runningTimer() |
100
|
|
|
{ |
101
|
|
|
$workspaceId = getenv('timer_workspace_id'); |
102
|
|
|
$userId = getenv('timer_user_id'); |
103
|
|
|
|
104
|
|
|
$timer = $this->client->get("workspaces/$workspaceId/user/$userId/time-entries?in-progress=true"); |
105
|
|
|
|
106
|
|
|
return $timer[0]['id'] ?? false; |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
public function pastTimers() |
110
|
|
|
{ |
111
|
|
|
try { |
112
|
|
|
$workspaceId = getenv('timer_workspace_id'); |
113
|
|
|
$userId = getenv('timer_user_id'); |
114
|
|
|
$pastTimers = []; |
|
|
|
|
115
|
|
|
|
116
|
|
|
$clockifyTimers = $this->client->get("workspaces/$workspaceId/user/$userId/time-entries", [ |
117
|
|
|
'start' => Carbon::today(), |
118
|
|
|
'end' => Carbon::today()->subDays(30), |
119
|
|
|
]); |
120
|
|
|
|
121
|
|
|
return $this->convertToPastTimers($clockifyTimers); |
122
|
|
|
} catch (Exception $e) { |
123
|
|
|
return []; |
124
|
|
|
} |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
public function continueTimer($timerId = null) |
128
|
|
|
{ |
129
|
|
|
return false; |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
public function deleteTimer($timerId) |
133
|
|
|
{ |
134
|
|
|
return false; |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
protected function convertToPastTimers($clockifyTimers) |
138
|
|
|
{ |
139
|
|
|
$projects = $this->projects(); |
140
|
|
|
$tags = $this->tags(); |
141
|
|
|
|
142
|
|
|
return array_map(function ($clockifyTimer) use ($projects, $tags) { |
143
|
|
|
return $this->buildPastTimerObject($clockifyTimer, $projects, $tags); |
144
|
|
|
}, $clockifyTimers); |
145
|
|
|
} |
146
|
|
|
|
147
|
|
|
protected function buildPastTimerObject($clockifyTimer, $projects, $tags) |
148
|
|
|
{ |
149
|
|
|
$pastTimer['id'] = $clockifyTimer['id']; |
|
|
|
|
150
|
|
|
$pastTimer['description'] = $clockifyTimer['description']; |
151
|
|
|
|
152
|
|
|
if (isset($clockifyTimer['projectId'])) { |
153
|
|
|
$pastTimer['project_id'] = $clockifyTimer['projectId']; |
154
|
|
|
$pastTimer['project_name'] = $projects[$clockifyTimer['projectId']]; |
155
|
|
|
} |
156
|
|
|
|
157
|
|
|
if (isset($clockifyTimer['tagIds'][0])) { |
158
|
|
|
$pastTimer['tag_id'] = $clockifyTimer['tagIds'][0]; |
159
|
|
|
$pastTimer['tags'] = $tags[$clockifyTimer['tagIds'][0]]; |
160
|
|
|
} |
161
|
|
|
|
162
|
|
|
$pastTimer['duration'] = $clockifyTimer['timeInterval']['duration']; |
163
|
|
|
|
164
|
|
|
return (object) $pastTimer; |
165
|
|
|
} |
166
|
|
|
} |
167
|
|
|
|