|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Godbout\Alfred\Time\Services; |
|
4
|
|
|
|
|
5
|
|
|
use Carbon\Carbon; |
|
6
|
|
|
use Carbon\CarbonInterval; |
|
7
|
|
|
use Exception; |
|
8
|
|
|
use MorningTrain\TogglApi\TogglApi; |
|
9
|
|
|
|
|
10
|
|
|
class Toggl extends TimerService |
|
11
|
|
|
{ |
|
12
|
|
|
private $client; |
|
13
|
|
|
|
|
14
|
|
|
private $data = null; |
|
15
|
|
|
|
|
16
|
|
|
|
|
17
|
97 |
|
public function __construct($apiToken) |
|
18
|
|
|
{ |
|
19
|
97 |
|
$this->client = new TogglApi($apiToken); |
|
20
|
97 |
|
} |
|
21
|
|
|
|
|
22
|
29 |
|
public function projects() |
|
23
|
|
|
{ |
|
24
|
29 |
|
return $this->extractFromData('projects'); |
|
25
|
|
|
} |
|
26
|
|
|
|
|
27
|
14 |
|
public function tags() |
|
28
|
|
|
{ |
|
29
|
14 |
|
return $this->extractFromData('tags'); |
|
30
|
|
|
} |
|
31
|
|
|
|
|
32
|
5 |
|
public function pastTimers() |
|
33
|
|
|
{ |
|
34
|
|
|
try { |
|
35
|
5 |
|
$togglTimers = $this->client->getTimeEntriesInRange(Carbon::today(), Carbon::today()->subDays(30)); |
|
36
|
|
|
|
|
37
|
5 |
|
return $this->convertToPastTimers($togglTimers); |
|
38
|
|
|
} catch (Exception $e) { |
|
39
|
|
|
return []; |
|
40
|
|
|
} |
|
41
|
|
|
} |
|
42
|
|
|
|
|
43
|
35 |
|
public function startTimer() |
|
44
|
|
|
{ |
|
45
|
|
|
try { |
|
46
|
35 |
|
$timer = $this->client->startTimeEntry([ |
|
47
|
35 |
|
'description' => getenv('timer_description'), |
|
48
|
35 |
|
'pid' => getenv('timer_project_id'), |
|
49
|
35 |
|
'tags' => getenv('timer_tag') ? [getenv('timer_tag')] : '', |
|
50
|
35 |
|
'created_with' => 'Alfred Time' |
|
51
|
|
|
]); |
|
52
|
|
|
|
|
53
|
35 |
|
if (! isset($timer->id)) { |
|
54
|
35 |
|
return false; |
|
55
|
|
|
} |
|
56
|
|
|
} catch (Exception $e) { |
|
57
|
|
|
return false; |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
35 |
|
return $timer->id; |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
25 |
|
public function stopCurrentTimer() |
|
64
|
|
|
{ |
|
65
|
25 |
|
if ($timerId = $this->runningTimer()) { |
|
66
|
25 |
|
$response = $this->client->stopTimeEntry($timerId); |
|
67
|
|
|
|
|
68
|
25 |
|
if (! isset($response->id)) { |
|
69
|
1 |
|
throw new Exception("Can't stop current running timer.", 1); |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
25 |
|
return true; |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
5 |
|
return false; |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
79 |
|
public function runningTimer() |
|
79
|
|
|
{ |
|
80
|
79 |
|
$timer = $this->client->getRunningTimeEntry(); |
|
81
|
|
|
|
|
82
|
79 |
|
return $timer->id ?? false; |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
5 |
|
public function continueTimer($timerId) |
|
86
|
|
|
{ |
|
87
|
|
|
/** |
|
88
|
|
|
* Timer attributes are stored in env variables |
|
89
|
|
|
* gathered in startTimer. |
|
90
|
|
|
*/ |
|
91
|
5 |
|
return $this->startTimer(); |
|
92
|
|
|
} |
|
93
|
|
|
|
|
94
|
35 |
|
public function deleteTimer($timerId) |
|
95
|
|
|
{ |
|
96
|
|
|
try { |
|
97
|
35 |
|
$this->client->deleteTimeEntry($timerId); |
|
98
|
|
|
} catch (Exception $e) { |
|
99
|
|
|
return false; |
|
100
|
|
|
} |
|
101
|
|
|
|
|
102
|
35 |
|
return true; |
|
103
|
|
|
} |
|
104
|
|
|
|
|
105
|
43 |
|
private function extractFromData($needle) |
|
106
|
|
|
{ |
|
107
|
43 |
|
$data = $this->getData(); |
|
108
|
|
|
|
|
109
|
43 |
|
if (! isset($data->$needle)) { |
|
110
|
18 |
|
return []; |
|
111
|
|
|
} |
|
112
|
|
|
|
|
113
|
25 |
|
$choosableData = $this->filterOutServerwiseDeletedAndArchivedItemsFromData($data->$needle); |
|
114
|
|
|
|
|
115
|
25 |
|
return array_column($choosableData, 'name', 'id'); |
|
116
|
|
|
} |
|
117
|
|
|
|
|
118
|
43 |
|
private function getData() |
|
119
|
|
|
{ |
|
120
|
43 |
|
if (is_null($this->data)) { |
|
121
|
43 |
|
return $this->client->getMe(true); |
|
122
|
|
|
} |
|
123
|
|
|
|
|
124
|
|
|
return $this->data; |
|
125
|
|
|
} |
|
126
|
|
|
|
|
127
|
25 |
|
private function filterOutServerwiseDeletedAndArchivedItemsFromData($items = []) |
|
128
|
|
|
{ |
|
129
|
|
|
return array_filter($items, function ($item) { |
|
130
|
|
|
return |
|
131
|
25 |
|
! $this->itemIsDeletedServerwise($item) |
|
132
|
25 |
|
&& ! $this->itemIsArchivedServerwise($item); |
|
133
|
25 |
|
}); |
|
134
|
|
|
} |
|
135
|
|
|
|
|
136
|
25 |
|
private function itemIsDeletedServerwise($item) |
|
137
|
|
|
{ |
|
138
|
25 |
|
return isset($item->server_deleted_at); |
|
139
|
|
|
} |
|
140
|
|
|
|
|
141
|
25 |
|
private function itemIsArchivedServerwise($item) |
|
142
|
|
|
{ |
|
143
|
|
|
return |
|
144
|
25 |
|
isset($item->active) |
|
145
|
25 |
|
&& $item->active === false; |
|
146
|
|
|
} |
|
147
|
|
|
|
|
148
|
5 |
|
protected function convertToPastTimers($togglTimers) |
|
149
|
|
|
{ |
|
150
|
5 |
|
$projects = $this->projects(); |
|
151
|
|
|
|
|
152
|
5 |
|
return array_reverse( |
|
153
|
|
|
array_map(function ($togglTimer) use ($projects) { |
|
154
|
5 |
|
return $this->buildPastTimerObject($togglTimer, $projects); |
|
155
|
5 |
|
}, $togglTimers) |
|
156
|
|
|
); |
|
157
|
|
|
} |
|
158
|
|
|
|
|
159
|
5 |
|
protected function buildPastTimerObject($togglTimer, $projects) |
|
160
|
|
|
{ |
|
161
|
5 |
|
$pastTimer['id'] = $togglTimer->id; |
|
|
|
|
|
|
162
|
5 |
|
$pastTimer['description'] = $togglTimer->description ?? ''; |
|
163
|
5 |
|
$pastTimer['duration'] = CarbonInterval::seconds($togglTimer->duration)->cascade()->format('%H:%I:%S'); |
|
164
|
|
|
|
|
165
|
5 |
|
if (isset($togglTimer->pid)) { |
|
166
|
5 |
|
$pastTimer['project_id'] = $togglTimer->pid; |
|
167
|
5 |
|
$pastTimer['project_name'] = $projects[$togglTimer->pid] ?? $togglTimer->pid; |
|
168
|
|
|
} |
|
169
|
|
|
|
|
170
|
5 |
|
if (isset($togglTimer->tags)) { |
|
171
|
5 |
|
$pastTimer['tags'] = implode(', ', (array) $togglTimer->tags); |
|
172
|
|
|
} |
|
173
|
|
|
|
|
174
|
5 |
|
return (object) $pastTimer; |
|
175
|
|
|
} |
|
176
|
|
|
} |
|
177
|
|
|
|