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