|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
require_once 'serviceApiCall.class.php'; |
|
4
|
|
|
|
|
5
|
|
|
/** |
|
6
|
|
|
* |
|
7
|
|
|
*/ |
|
8
|
|
|
class Toggl |
|
9
|
|
|
{ |
|
10
|
|
|
private $serviceApiCall = null; |
|
11
|
|
|
private $message = ''; |
|
12
|
|
|
|
|
13
|
|
View Code Duplication |
public function __construct($apiToken = null) |
|
|
|
|
|
|
14
|
|
|
{ |
|
15
|
|
|
$this->serviceApiCall = new serviceApiCall([ |
|
16
|
|
|
'base_uri' => 'https://www.toggl.com/api/v8/', |
|
17
|
|
|
'headers' => [ |
|
18
|
|
|
'Content-type' => 'application/json', |
|
19
|
|
|
'Accept' => 'application/json', |
|
20
|
|
|
'Authorization' => 'Basic ' . base64_encode($apiToken . ':api_token'), |
|
21
|
|
|
], |
|
22
|
|
|
]); |
|
23
|
|
|
} |
|
24
|
|
|
|
|
25
|
|
|
public function startTimer($description, $projectId, $tagNames) |
|
26
|
|
|
{ |
|
27
|
|
|
$togglId = null; |
|
28
|
|
|
|
|
29
|
|
|
$item = [ |
|
30
|
|
|
'time_entry' => [ |
|
31
|
|
|
'description' => $description, |
|
32
|
|
|
'pid' => $projectId, |
|
33
|
|
|
'tags' => explode(', ', $tagNames), |
|
34
|
|
|
'created_with' => 'Alfred Time Workflow', |
|
35
|
|
|
], |
|
36
|
|
|
]; |
|
37
|
|
|
|
|
38
|
|
|
if ($this->serviceApiCall->send('post', 'time_entries/start', ['json' => $item]) === true) { |
|
39
|
|
|
if ($this->serviceApiCall->last('success') === true) { |
|
40
|
|
|
$this->setMessage('timer started'); |
|
41
|
|
|
$togglId = $this->serviceApiCall->getData()['data']['id']; |
|
42
|
|
|
} else { |
|
43
|
|
|
$this->setMessage('cannot start timer!'); |
|
44
|
|
|
} |
|
45
|
|
|
} else { |
|
46
|
|
|
$this->setMessage($this->serviceApiCall->getMessage()); |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
|
|
return $togglId; |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
|
View Code Duplication |
public function stopTimer($timerId = null) |
|
|
|
|
|
|
53
|
|
|
{ |
|
54
|
|
|
$res = false; |
|
55
|
|
|
|
|
56
|
|
|
if ($this->serviceApiCall->send('put', 'time_entries/' . $timerId . '/stop') === true) { |
|
57
|
|
|
if ($this->serviceApiCall->last('success') === true) { |
|
58
|
|
|
$this->setMessage('timer stopped'); |
|
59
|
|
|
$res = true; |
|
60
|
|
|
} else { |
|
61
|
|
|
$this->setMessage('could not stop timer!'); |
|
62
|
|
|
} |
|
63
|
|
|
} else { |
|
64
|
|
|
$this->setMessage($this->serviceApiCall->getMessage()); |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
return $res; |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
|
View Code Duplication |
public function deleteTimer($timerId = null) |
|
|
|
|
|
|
71
|
|
|
{ |
|
72
|
|
|
$res = false; |
|
73
|
|
|
|
|
74
|
|
|
if ($this->serviceApiCall->send('delete', 'time_entries/' . $timerId) === true) { |
|
75
|
|
|
if ($this->serviceApiCall->last('success') === true) { |
|
76
|
|
|
$this->setMessage('timer deleted'); |
|
77
|
|
|
$res = true; |
|
78
|
|
|
} else { |
|
79
|
|
|
$this->setMessage('could not delete timer!'); |
|
80
|
|
|
} |
|
81
|
|
|
} else { |
|
82
|
|
|
$this->setMessage($this->serviceApiCall->getMessage()); |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
|
|
return $res; |
|
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
|
|
|
public function getOnlineData() |
|
104
|
|
|
{ |
|
105
|
|
|
$data = []; |
|
106
|
|
|
|
|
107
|
|
|
if ($this->serviceApiCall->send('get', 'me?with_related_data=true') === true) { |
|
108
|
|
|
if ($this->serviceApiCall->last('success') === true) { |
|
109
|
|
|
$this->setMessage('data cached'); |
|
110
|
|
|
$data = $this->serviceApiCall->getData(); |
|
111
|
|
|
} else { |
|
112
|
|
|
$this->setMessage('cannot get online data!'); |
|
113
|
|
|
} |
|
114
|
|
|
} else { |
|
115
|
|
|
$this->setMessage($this->serviceApiCall->getMessage()); |
|
116
|
|
|
} |
|
117
|
|
|
|
|
118
|
|
|
return $data; |
|
119
|
|
|
} |
|
120
|
|
|
|
|
121
|
|
|
public function getLastMessage() |
|
122
|
|
|
{ |
|
123
|
|
|
return $this->message; |
|
124
|
|
|
} |
|
125
|
|
|
|
|
126
|
|
|
private function setMessage($message = null) |
|
127
|
|
|
{ |
|
128
|
|
|
$this->message = '- Toggl: ' . $message; |
|
129
|
|
|
} |
|
130
|
|
|
} |
|
131
|
|
|
|
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.