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
|
|
View Code Duplication |
public function __construct($apiToken = null) |
|
|
|
|
26
|
|
|
{ |
27
|
|
|
$this->serviceApiCall = new ServiceApiCall([ |
28
|
|
|
'base_uri' => 'https://www.toggl.com/api/v8/', |
29
|
|
|
'headers' => [ |
30
|
|
|
'Content-type' => 'application/json', |
31
|
|
|
'Accept' => 'application/json', |
32
|
|
|
'Authorization' => 'Basic ' . base64_encode($apiToken . ':api_token'), |
33
|
|
|
], |
34
|
|
|
]); |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* @param $timerId |
39
|
|
|
* @return mixed |
40
|
|
|
*/ |
41
|
|
View Code Duplication |
public function deleteTimer($timerId = null) |
|
|
|
|
42
|
|
|
{ |
43
|
|
|
$res = false; |
44
|
|
|
|
45
|
|
|
if ($this->serviceApiCall->send('delete', 'time_entries/' . $timerId) === true) { |
46
|
|
|
if ($this->serviceApiCall->last('success') === true) { |
47
|
|
|
$this->setMessage('timer deleted'); |
48
|
|
|
$res = true; |
49
|
|
|
} else { |
50
|
|
|
$this->setMessage('could not delete timer!'); |
51
|
|
|
} |
52
|
|
|
} else { |
53
|
|
|
$this->setMessage($this->serviceApiCall->getMessage()); |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
return $res; |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* @return mixed |
61
|
|
|
*/ |
62
|
|
|
public function getLastMessage() |
63
|
|
|
{ |
64
|
|
|
return $this->message; |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* @return mixed |
69
|
|
|
*/ |
70
|
|
|
public function getOnlineData() |
71
|
|
|
{ |
72
|
|
|
$data = []; |
73
|
|
|
|
74
|
|
|
if ($this->serviceApiCall->send('get', 'me?with_related_data=true') === true) { |
75
|
|
|
if ($this->serviceApiCall->last('success') === true) { |
76
|
|
|
$this->setMessage('data cached'); |
77
|
|
|
$data = $this->serviceApiCall->getData(); |
78
|
|
|
} else { |
79
|
|
|
$this->setMessage('cannot get online data!'); |
80
|
|
|
} |
81
|
|
|
} else { |
82
|
|
|
$this->setMessage($this->serviceApiCall->getMessage()); |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
return $data; |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
public function getRecentTimers() |
89
|
|
|
{ |
90
|
|
|
$timers = []; |
91
|
|
|
|
92
|
|
|
if ($this->serviceApiCall->send('get', 'time_entries') === true) { |
93
|
|
|
if ($this->serviceApiCall->last('success') === true) { |
94
|
|
|
$timers = $this->serviceApiCall->getData(); |
95
|
|
|
} |
96
|
|
|
} else { |
97
|
|
|
$this->setMessage($this->serviceApiCall->getMessage()); |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
return array_reverse($timers); |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
/** |
104
|
|
|
* @param $description |
105
|
|
|
* @param $projectId |
106
|
|
|
* @param $tagNames |
107
|
|
|
* @return mixed |
108
|
|
|
*/ |
109
|
|
|
public function startTimer($description, $projectId, $tagNames) |
110
|
|
|
{ |
111
|
|
|
$togglId = null; |
112
|
|
|
|
113
|
|
|
$item = [ |
114
|
|
|
'time_entry' => [ |
115
|
|
|
'description' => $description, |
116
|
|
|
'pid' => $projectId, |
117
|
|
|
'tags' => explode(', ', $tagNames), |
118
|
|
|
'created_with' => 'Alfred Time Workflow', |
119
|
|
|
], |
120
|
|
|
]; |
121
|
|
|
|
122
|
|
|
if ($this->serviceApiCall->send('post', 'time_entries/start', ['json' => $item]) === true) { |
123
|
|
|
if ($this->serviceApiCall->last('success') === true) { |
124
|
|
|
$this->setMessage('timer started'); |
125
|
|
|
$togglId = $this->serviceApiCall->getData()['data']['id']; |
126
|
|
|
} else { |
127
|
|
|
$this->setMessage('cannot start timer!'); |
128
|
|
|
} |
129
|
|
|
} else { |
130
|
|
|
$this->setMessage($this->serviceApiCall->getMessage()); |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
return $togglId; |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
/** |
137
|
|
|
* @param $timerId |
138
|
|
|
* @return mixed |
139
|
|
|
*/ |
140
|
|
View Code Duplication |
public function stopTimer($timerId = null) |
|
|
|
|
141
|
|
|
{ |
142
|
|
|
$res = false; |
143
|
|
|
|
144
|
|
|
if ($this->serviceApiCall->send('put', 'time_entries/' . $timerId . '/stop') === true) { |
145
|
|
|
if ($this->serviceApiCall->last('success') === true) { |
146
|
|
|
$this->setMessage('timer stopped'); |
147
|
|
|
$res = true; |
148
|
|
|
} else { |
149
|
|
|
$this->setMessage('could not stop timer!'); |
150
|
|
|
} |
151
|
|
|
} else { |
152
|
|
|
$this->setMessage($this->serviceApiCall->getMessage()); |
153
|
|
|
} |
154
|
|
|
|
155
|
|
|
return $res; |
156
|
|
|
} |
157
|
|
|
|
158
|
|
|
/** |
159
|
|
|
* @param $message |
160
|
|
|
*/ |
161
|
|
|
private function setMessage($message = null) |
162
|
|
|
{ |
163
|
|
|
$this->message = '- Toggl: ' . $message; |
164
|
|
|
} |
165
|
|
|
} |
166
|
|
|
|
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.