|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace AlfredTime; |
|
4
|
|
|
|
|
5
|
|
|
use AlfredTime\ServiceApiCall; |
|
6
|
|
|
|
|
7
|
|
|
/** |
|
8
|
|
|
* |
|
9
|
|
|
*/ |
|
10
|
|
|
class Toggl |
|
11
|
|
|
{ |
|
12
|
|
|
/** |
|
13
|
|
|
* @var string |
|
14
|
|
|
*/ |
|
15
|
|
|
private $message = ''; |
|
16
|
|
|
|
|
17
|
|
|
/** |
|
18
|
|
|
* @var mixed |
|
19
|
|
|
*/ |
|
20
|
|
|
private $serviceApiCall = null; |
|
21
|
|
|
|
|
22
|
|
|
/** |
|
23
|
|
|
* @param $apiToken |
|
24
|
|
|
*/ |
|
25
|
|
|
public function __construct($apiToken = null) |
|
26
|
|
|
{ |
|
27
|
|
|
$this->serviceApiCall = new ServiceApiCall([ |
|
28
|
|
|
'base_uri' => 'https://www.toggl.com/api/v8/', |
|
29
|
|
|
'headers' => [ |
|
30
|
|
|
'Authorization' => 'Basic ' . base64_encode($apiToken . ':api_token'), |
|
31
|
|
|
], |
|
32
|
|
|
]); |
|
33
|
|
|
} |
|
34
|
|
|
|
|
35
|
|
|
/** |
|
36
|
|
|
* @param $timerId |
|
37
|
|
|
* @return mixed |
|
38
|
|
|
*/ |
|
39
|
|
|
public function deleteTimer($timerId = null) |
|
40
|
|
|
{ |
|
41
|
|
|
return $this->timerAction('delete', 'time_entries/' . $timerId); |
|
42
|
|
|
} |
|
43
|
|
|
|
|
44
|
|
|
/** |
|
45
|
|
|
* @return string |
|
46
|
|
|
*/ |
|
47
|
|
|
public function getLastMessage() |
|
48
|
|
|
{ |
|
49
|
|
|
return $this->message; |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
|
|
/** |
|
53
|
|
|
* @return mixed |
|
54
|
|
|
*/ |
|
55
|
|
|
public function getOnlineData() |
|
56
|
|
|
{ |
|
57
|
|
|
return $this->timerAction('get_online_data', 'me?with_related_data=true'); |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
|
|
/** |
|
61
|
|
|
* @param $data |
|
62
|
|
|
* @return mixed |
|
63
|
|
|
*/ |
|
64
|
|
|
public function getProjects($data) |
|
65
|
|
|
{ |
|
66
|
|
|
return $this->getItems('projects', $data); |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
public function getRecentTimers() |
|
70
|
|
|
{ |
|
71
|
|
|
return array_reverse($this->timerAction('get_recent_timers', 'time_entries')); |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
|
|
/** |
|
75
|
|
|
* @param $data |
|
76
|
|
|
* @return mixed |
|
77
|
|
|
*/ |
|
78
|
|
|
public function getTags($data) |
|
79
|
|
|
{ |
|
80
|
|
|
return $this->getItems('tags', $data); |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
|
|
/** |
|
84
|
|
|
* @param $description |
|
85
|
|
|
* @param $projectId |
|
86
|
|
|
* @param $tagNames |
|
87
|
|
|
* @return mixed |
|
88
|
|
|
*/ |
|
89
|
|
|
public function startTimer($description, $projectId, $tagData) |
|
90
|
|
|
{ |
|
91
|
|
|
$togglId = null; |
|
92
|
|
|
$item = [ |
|
93
|
|
|
'time_entry' => [ |
|
94
|
|
|
'description' => $description, |
|
95
|
|
|
'pid' => $projectId, |
|
96
|
|
|
'tags' => explode(', ', $tagData), |
|
97
|
|
|
'created_with' => 'Alfred Time Workflow', |
|
98
|
|
|
], |
|
99
|
|
|
]; |
|
100
|
|
|
|
|
101
|
|
|
$data = $this->timerAction('start', 'time_entries/start', ['json' => $item]); |
|
102
|
|
|
|
|
103
|
|
|
if (isset($data['data']['id']) === true) { |
|
104
|
|
|
$togglId = $data['data']['id']; |
|
105
|
|
|
} |
|
106
|
|
|
|
|
107
|
|
|
return $togglId; |
|
108
|
|
|
} |
|
109
|
|
|
|
|
110
|
|
|
/** |
|
111
|
|
|
* @param $timerId |
|
112
|
|
|
* @return mixed |
|
113
|
|
|
*/ |
|
114
|
|
|
public function stopTimer($timerId = null) |
|
115
|
|
|
{ |
|
116
|
|
|
return $this->timerAction('stop', 'time_entries/' . $timerId . '/stop'); |
|
117
|
|
|
} |
|
118
|
|
|
|
|
119
|
|
|
/** |
|
120
|
|
|
* @param string $action |
|
121
|
|
|
* @param string $apiUri |
|
122
|
|
|
* @return mixed |
|
123
|
|
|
*/ |
|
124
|
|
|
public function timerAction($action, $apiUri, array $options = []) |
|
125
|
|
|
{ |
|
126
|
|
|
$returnDataFor = ['start', 'get_recent_timers', 'get_online_data']; |
|
127
|
|
|
$methods = [ |
|
128
|
|
|
'start' => 'post', |
|
129
|
|
|
'stop' => 'put', |
|
130
|
|
|
'delete' => 'delete', |
|
131
|
|
|
'get_recent_timers' => 'get', |
|
132
|
|
|
'get_online_data' => 'get', |
|
133
|
|
|
]; |
|
134
|
|
|
$method = isset($methods[$action]) ? $methods[$action] : ''; |
|
135
|
|
|
|
|
136
|
|
View Code Duplication |
if ($this->serviceApiCall->send($method, $apiUri, $options) === false) { |
|
|
|
|
|
|
137
|
|
|
$this->message = $this->serviceApiCall->getMessage(); |
|
138
|
|
|
|
|
139
|
|
|
return false; |
|
140
|
|
|
} |
|
141
|
|
|
|
|
142
|
|
|
if (in_array($action, $returnDataFor) === true) { |
|
143
|
|
|
return $this->serviceApiCall->getData(); |
|
144
|
|
|
} |
|
145
|
|
|
|
|
146
|
|
|
return $this->serviceApiCall->last('success'); |
|
147
|
|
|
} |
|
148
|
|
|
|
|
149
|
|
|
/** |
|
150
|
|
|
* @param $needle |
|
151
|
|
|
* @param array $haystack |
|
152
|
|
|
* @return mixed |
|
153
|
|
|
*/ |
|
154
|
|
|
private function getItems($needle, array $haystack = []) |
|
155
|
|
|
{ |
|
156
|
|
|
$items = []; |
|
157
|
|
|
|
|
158
|
|
|
if (isset($haystack['data'][$needle]) === false) { |
|
159
|
|
|
return []; |
|
160
|
|
|
} |
|
161
|
|
|
|
|
162
|
|
|
/** |
|
163
|
|
|
* To only show projects that are currently active |
|
164
|
|
|
* The Toggl API is slightly weird on that |
|
165
|
|
|
*/ |
|
166
|
|
|
foreach ($haystack['data'][$needle] as $key => $item) { |
|
167
|
|
|
if (isset($item['server_deleted_at']) === true) { |
|
168
|
|
|
unset($haystack['data'][$needle][$key]); |
|
169
|
|
|
} |
|
170
|
|
|
|
|
171
|
|
|
$items[] = [ |
|
172
|
|
|
'name' => $item['name'], |
|
173
|
|
|
'id' => $item['id'], |
|
174
|
|
|
]; |
|
175
|
|
|
} |
|
176
|
|
|
|
|
177
|
|
|
return $items; |
|
178
|
|
|
} |
|
179
|
|
|
} |
|
180
|
|
|
|
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.