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