1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace AlfredTime; |
4
|
|
|
|
5
|
|
|
use AlfredTime\ServiceApiCall; |
6
|
|
|
|
7
|
|
|
/** |
8
|
|
|
* |
9
|
|
|
*/ |
10
|
|
|
class Toggl extends Service |
11
|
|
|
{ |
12
|
|
|
/** |
13
|
|
|
* @var string |
14
|
|
|
*/ |
15
|
|
|
protected $apiBaseUrl = 'https://www.toggl.com/api/v8/'; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* @var array |
19
|
|
|
*/ |
20
|
|
|
protected $methods = [ |
21
|
|
|
'start' => 'post', |
22
|
|
|
'stop' => 'put', |
23
|
|
|
'delete' => 'delete', |
24
|
|
|
'get_recent_timers' => 'get', |
25
|
|
|
'get_online_data' => 'get', |
26
|
|
|
]; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* @param $apiToken |
30
|
|
|
*/ |
31
|
|
|
public function __construct($apiToken = null) |
32
|
|
|
{ |
33
|
|
|
parent::__construct($this->apiBaseUrl, base64_encode($apiToken . ':api_token')); |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* @param $timerId |
38
|
|
|
*/ |
39
|
|
|
public function apiDeleteUrl($timerId) |
40
|
|
|
{ |
41
|
|
|
return 'time_entries/' . $timerId; |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
public function apiStartUrl() |
45
|
|
|
{ |
46
|
|
|
return 'time_entries/start'; |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* @param $timerId |
51
|
|
|
*/ |
52
|
|
|
public function apiStopUrl($timerId) |
53
|
|
|
{ |
54
|
|
|
return 'time_entries/' . $timerId . '/stop'; |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* @param $description |
59
|
|
|
* @param $projectId |
60
|
|
|
* @param $tagData |
61
|
|
|
*/ |
62
|
|
|
public function generateTimer($description, $projectId, $tagData) |
63
|
|
|
{ |
64
|
|
|
return [ |
65
|
|
|
'time_entry' => [ |
66
|
|
|
'description' => $description, |
67
|
|
|
'pid' => $projectId, |
68
|
|
|
'tags' => explode(', ', $tagData), |
69
|
|
|
'created_with' => 'Alfred Time Workflow', |
70
|
|
|
], |
71
|
|
|
]; |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* @return mixed |
76
|
|
|
*/ |
77
|
|
|
public function getOnlineData() |
78
|
|
|
{ |
79
|
|
|
return $this->timerAction('get_online_data', 'me?with_related_data=true'); |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* @param $projectId |
84
|
|
|
* @return mixed |
85
|
|
|
*/ |
86
|
|
|
public function getProjectName($projectId, array $data = []) |
87
|
|
|
{ |
88
|
|
|
$projectName = ''; |
89
|
|
|
$projects = $this->getProjects($data); |
90
|
|
|
|
91
|
|
|
foreach ($projects as $project) { |
92
|
|
|
if ($project['id'] === $projectId) { |
93
|
|
|
$projectName = $project['name']; |
94
|
|
|
break; |
95
|
|
|
} |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
return $projectName; |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
/** |
102
|
|
|
* @param $data |
103
|
|
|
* @return mixed |
104
|
|
|
*/ |
105
|
|
|
public function getProjects($data) |
106
|
|
|
{ |
107
|
|
|
return $this->getItems('projects', $data); |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
/** |
111
|
|
|
* @return mixed |
112
|
|
|
*/ |
113
|
|
View Code Duplication |
public function getRecentTimers() |
|
|
|
|
114
|
|
|
{ |
115
|
|
|
$timers = []; |
116
|
|
|
|
117
|
|
|
foreach ($this->timerAction('get_recent_timers', 'time_entries') as $timeEntry) { |
118
|
|
|
$timers[] = [ |
119
|
|
|
'id' => $timeEntry['id'], |
120
|
|
|
'description' => $timeEntry['description'], |
121
|
|
|
'project_id' => $timeEntry['pid'], |
122
|
|
|
'project_name' => $timeEntry['pid'], |
123
|
|
|
'tags' => empty($timeEntry['tags']) ? '' : implode(', ', $timeEntry['tags']), |
124
|
|
|
'duration' => $timeEntry['duration'], |
125
|
|
|
]; |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
return array_reverse($timers); |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
/** |
132
|
|
|
* @param $data |
133
|
|
|
* @return mixed |
134
|
|
|
*/ |
135
|
|
|
public function getTags($data) |
136
|
|
|
{ |
137
|
|
|
return $this->getItems('tags', $data); |
138
|
|
|
} |
139
|
|
|
|
140
|
|
|
/** |
141
|
|
|
* @param string $action |
142
|
|
|
* @param string $apiUri |
143
|
|
|
* @return mixed |
144
|
|
|
*/ |
145
|
|
View Code Duplication |
protected function timerAction($action, $apiUri, array $options = []) |
|
|
|
|
146
|
|
|
{ |
147
|
|
|
$returnDataFor = ['start', 'get_recent_timers', 'get_online_data']; |
148
|
|
|
|
149
|
|
|
$method = $this->methodForAction($action); |
150
|
|
|
|
151
|
|
|
return $this->serviceApiCall->send( |
152
|
|
|
$method, |
153
|
|
|
$apiUri, |
154
|
|
|
$options, |
155
|
|
|
in_array($action, $returnDataFor) |
156
|
|
|
); |
157
|
|
|
} |
158
|
|
|
|
159
|
|
|
/** |
160
|
|
|
* @param $needle |
161
|
|
|
* @param array $haystack |
162
|
|
|
* @return mixed |
163
|
|
|
*/ |
164
|
|
|
private function getItems($needle, array $haystack = []) |
165
|
|
|
{ |
166
|
|
|
$items = []; |
167
|
|
|
|
168
|
|
|
if (isset($haystack['data'][$needle]) === false) { |
169
|
|
|
return []; |
170
|
|
|
} |
171
|
|
|
|
172
|
|
|
/** |
173
|
|
|
* To only show projects that are currently active |
174
|
|
|
* The Toggl API is slightly weird on that |
175
|
|
|
*/ |
176
|
|
|
foreach ($haystack['data'][$needle] as $key => $item) { |
177
|
|
|
if (isset($item['server_deleted_at']) === true) { |
178
|
|
|
unset($haystack['data'][$needle][$key]); |
179
|
|
|
} |
180
|
|
|
|
181
|
|
|
$items[] = [ |
182
|
|
|
'name' => $item['name'], |
183
|
|
|
'id' => ($needle === 'tags') ? $item['name'] : $item['id'], |
184
|
|
|
]; |
185
|
|
|
} |
186
|
|
|
|
187
|
|
|
return $items; |
188
|
|
|
} |
189
|
|
|
} |
190
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.