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