1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* |
4
|
|
|
*/ |
5
|
|
|
class Toggl |
6
|
|
|
{ |
7
|
|
|
private $message; |
8
|
|
|
private $apiToken; |
9
|
|
|
|
10
|
|
|
public function __construct($apiToken = null) |
11
|
|
|
{ |
12
|
|
|
$this->message = ''; |
13
|
|
|
$this->apiToken = $apiToken; |
14
|
|
|
} |
15
|
|
|
|
16
|
|
|
public function startTimer($description, $projectId, $tagNames) |
17
|
|
|
{ |
18
|
|
|
$togglId = null; |
19
|
|
|
|
20
|
|
|
$url = 'https://www.toggl.com/api/v8/time_entries/start'; |
21
|
|
|
|
22
|
|
|
$headers = [ |
23
|
|
|
"Content-type: application/json", |
24
|
|
|
"Accept: application/json", |
25
|
|
|
'Authorization: Basic ' . base64_encode($this->apiToken . ':api_token'), |
26
|
|
|
]; |
27
|
|
|
|
28
|
|
|
$item = [ |
29
|
|
|
'time_entry' => [ |
30
|
|
|
'description' => $description, |
31
|
|
|
'pid' => $projectId, |
32
|
|
|
'tags' => explode(', ', $tagNames), |
33
|
|
|
'created_with' => 'Alfred Time Workflow', |
34
|
|
|
], |
35
|
|
|
]; |
36
|
|
|
|
37
|
|
|
$ch = curl_init($url); |
38
|
|
|
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers); |
39
|
|
|
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); |
40
|
|
|
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($item, true)); |
41
|
|
|
$response = curl_exec($ch); |
42
|
|
|
$lastHttpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE); |
43
|
|
|
curl_close($ch); |
44
|
|
|
|
45
|
|
|
if ($response === false || ($lastHttpCode < 200 || $lastHttpCode > 299)) { |
46
|
|
|
$this->message = '- Cannot start Toggl timer!'; |
47
|
|
|
} else { |
48
|
|
|
$data = json_decode($response, true); |
49
|
|
|
$togglId = $data['data']['id']; |
50
|
|
|
$this->message = '- Toggl timer started'; |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
return $togglId; |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
public function stopTimer($timerId = null) |
57
|
|
|
{ |
58
|
|
|
$res = false; |
59
|
|
|
|
60
|
|
|
$url = 'https://www.toggl.com/api/v8/time_entries/' . $timerId . '/stop'; |
61
|
|
|
|
62
|
|
|
$headers = [ |
63
|
|
|
"Content-type: application/json", |
64
|
|
|
"Accept: application/json", |
65
|
|
|
'Authorization: Basic ' . base64_encode($this->apiToken . ':api_token'), |
66
|
|
|
]; |
67
|
|
|
|
68
|
|
|
$ch = curl_init($url); |
69
|
|
|
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers); |
70
|
|
|
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "PUT"); |
71
|
|
|
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); |
72
|
|
|
$response = curl_exec($ch); |
73
|
|
|
curl_close($ch); |
74
|
|
|
|
75
|
|
|
if ($response === false) { |
76
|
|
|
$this->message = '- Could not stop Toggl timer!'; |
77
|
|
|
} else { |
78
|
|
|
$this->message = '- Toggl timer stopped'; |
79
|
|
|
$res = true; |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
return $res; |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
public function getLastMessage() |
86
|
|
|
{ |
87
|
|
|
return $this->message; |
88
|
|
|
} |
89
|
|
|
|
90
|
|
View Code Duplication |
public function deleteTimer($timerId = null) |
|
|
|
|
91
|
|
|
{ |
92
|
|
|
$res = false; |
93
|
|
|
|
94
|
|
|
$url = 'https://www.toggl.com/api/v8/time_entries/' . $timerId; |
95
|
|
|
|
96
|
|
|
$headers = [ |
97
|
|
|
"Content-type: application/json", |
98
|
|
|
"Accept: application/json", |
99
|
|
|
'Authorization: Basic ' . base64_encode($this->apiToken . ':api_token'), |
100
|
|
|
]; |
101
|
|
|
|
102
|
|
|
$ch = curl_init($url); |
103
|
|
|
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers); |
104
|
|
|
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "DELETE"); |
105
|
|
|
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); |
106
|
|
|
$response = curl_exec($ch); |
107
|
|
|
$lastHttpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE); |
108
|
|
|
curl_close($ch); |
109
|
|
|
|
110
|
|
|
if ($response === false || $lastHttpCode !== 200) { |
111
|
|
|
$this->message = '- Could not delete Toggl timer!'; |
112
|
|
|
} else { |
113
|
|
|
$this->message = '- Toggl timer deleted'; |
114
|
|
|
$res = true; |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
return $res; |
118
|
|
|
} |
119
|
|
|
|
120
|
|
View Code Duplication |
public function getRecentTimers() |
|
|
|
|
121
|
|
|
{ |
122
|
|
|
$timers = []; |
123
|
|
|
|
124
|
|
|
$url = 'https://www.toggl.com/api/v8/time_entries'; |
125
|
|
|
|
126
|
|
|
$headers = [ |
127
|
|
|
"Content-type: application/json", |
128
|
|
|
"Accept: application/json", |
129
|
|
|
'Authorization: Basic ' . base64_encode($this->apiToken . ':api_token'), |
130
|
|
|
]; |
131
|
|
|
|
132
|
|
|
$ch = curl_init($url); |
133
|
|
|
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers); |
134
|
|
|
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); |
135
|
|
|
$response = curl_exec($ch); |
136
|
|
|
$lastHttpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE); |
137
|
|
|
curl_close($ch); |
138
|
|
|
|
139
|
|
|
if ($response !== false && $lastHttpCode === 200) { |
140
|
|
|
$timers = json_decode($response, true); |
141
|
|
|
} |
142
|
|
|
|
143
|
|
|
return array_reverse($timers); |
144
|
|
|
} |
145
|
|
|
|
146
|
|
View Code Duplication |
public function getOnlineData() |
|
|
|
|
147
|
|
|
{ |
148
|
|
|
$data = []; |
149
|
|
|
|
150
|
|
|
$url = 'https://www.toggl.com/api/v8/me?with_related_data=true'; |
151
|
|
|
|
152
|
|
|
$headers = [ |
153
|
|
|
"Content-type: application/json", |
154
|
|
|
"Accept: application/json", |
155
|
|
|
'Authorization: Basic ' . base64_encode($this->apiToken . ':api_token'), |
156
|
|
|
]; |
157
|
|
|
|
158
|
|
|
$ch = curl_init($url); |
159
|
|
|
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers); |
160
|
|
|
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); |
161
|
|
|
$response = curl_exec($ch); |
162
|
|
|
$lastHttpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE); |
163
|
|
|
curl_close($ch); |
164
|
|
|
|
165
|
|
|
if ($response === false || ($lastHttpCode < 200 || $lastHttpCode > 299)) { |
166
|
|
|
$this->message = '- Cannot get Toggl online data!'; |
167
|
|
|
} else { |
168
|
|
|
$data = json_decode($response, true); |
169
|
|
|
$this->message = '- Toggl data cached'; |
170
|
|
|
} |
171
|
|
|
|
172
|
|
|
return $data; |
173
|
|
|
} |
174
|
|
|
} |
175
|
|
|
|
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.