Skip to content

Commit bdd7dc1

Browse files
committed
Fixing bugs found via manual testing.
1 parent 3592713 commit bdd7dc1

File tree

1 file changed

+53
-19
lines changed

1 file changed

+53
-19
lines changed

toggl.php

Lines changed: 53 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ class TogglException extends Exception {}
66
* Toggl API Class
77
*
88
* @see https://www.toggl.com/public/api
9+
* @see https://github.com/davereid/toggl-php-sdk
910
*/
1011
class Toggl {
1112
/**
@@ -22,6 +23,14 @@ public function __construct($token) {
2223
$this->token = $token;
2324
}
2425

26+
public function setToken($token) {
27+
$this->token = $token;
28+
}
29+
30+
public function getToken() {
31+
return $this->token;
32+
}
33+
2534
/**
2635
* Construct the request URI.
2736
*/
@@ -36,8 +45,8 @@ protected function getURL($name) {
3645
*/
3746
protected function getHeaders() {
3847
return array(
39-
'Authorization' => 'Basic ' . base64_encode($this->token . ':api_token'),
40-
'User-Agent' => 'Toggl PHP SDK',
48+
//'Authorization' => 'Basic ' . base64_encode($this->token . ':api_token'),
49+
//'User-Agent' => 'Toggl PHP SDK (+https://github.com/davereid/toggl-php-sdk)',
4150
);
4251
}
4352

@@ -46,44 +55,52 @@ protected function getRequest($url, array $options = array()) {
4655
$url .= '?' . http_build_query($options['data'], '&');
4756
$options['data'] = NULL;
4857
}
49-
return $this->sendRequest($url, $options);
58+
return $this->request($url, $options);
5059
}
5160

52-
protected function sendRequest($url, array $options = array()) {
61+
protected function request($url, array $options = array()) {
5362
$options += array(
5463
'headers' => array(),
5564
'method' => 'GET',
5665
'data' => NULL,
5766
);
58-
67+
5968
// Set the CURL variables.
6069
$ch = curl_init();
6170

6271
// Include post data.
6372
if (isset($options['data'])) {
6473
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($options['data']));
65-
$headers['Content-Type'] = 'application/json';
74+
$options['headers']['Content-Type'] = 'application/json';
6675
}
6776

6877
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
6978
curl_setopt($ch, CURLOPT_URL, $url);
7079
curl_setopt($ch, CURLOPT_HEADER, FALSE);
71-
curl_setopt($ch, CURLOPT_CUSTOMREQUSET, $options['method']);
80+
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE); // Needed since Toggl's SSL fails without this.
81+
curl_setopt($ch, CURLOPT_USERAGENT, 'Toggl PHP SDK (+https://github.com/davereid/toggl-php-sdk)');
82+
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $options['method']);
83+
curl_setopt($ch, CURLOPT_USERPWD, $this->getToken() . ':api_token');
7284

7385
// Build and format the headers.
74-
foreach (array_merge($this->getHeaders(), $headers) as $header => $value) {
75-
$headers[$header] = $header . ': ' . $value;
86+
foreach (array_merge($this->getHeaders(), $options['headers']) as $header => $value) {
87+
$options['headers'][$header] = $header . ': ' . $value;
7688
}
77-
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
78-
89+
curl_setopt($ch, CURLOPT_HTTPHEADER, $options['headers']);
7990

8091
// Perform the API request.
8192
$result = curl_exec($ch);
93+
if ($result == FALSE) {
94+
throw new TogglException(curl_error($ch));
95+
}
8296

97+
// Build the response.
8398
$response = new stdClass();
8499
$response->data = json_decode($result);
85100
$response->code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
86-
$resposne->success = $response->code == 200;
101+
$response->success = $response->code == 200;
102+
103+
curl_close($ch);
87104
return $response;
88105
}
89106

@@ -98,17 +115,17 @@ public function timeEntriesLoadRecent($start_date = NULL, $end_date = NULL) {
98115
throw new ToggleException("Invalid parameters for getTimeEntries.");
99116
}
100117

101-
$data = array();
118+
$options = array();
102119
if (isset($start_date) && isset($end_date)) {
103120
if ($end_date < $start_date) {
104121
throw new TogglException("Start date cannot be after the end date.");
105122
}
106-
$data['start_date'] = gmdate(DATE_ISO8601, $start_date);
107-
$data['end_date'] = gmdate(DATE_ISO8601, $end_date);
123+
$options['data']['start_date'] = gmdate(DATE_ISO8601, $start_date);
124+
$options['data']['end_date'] = gmdate(DATE_ISO8601, $end_date);
108125
}
109126

110127
// @todo Convert this into an array of timeEntry classes.
111-
return $this->getRequest($this->getURL('tasks'), $data);
128+
return $this->getRequest($this->getURL('tasks'), $options);
112129
}
113130

114131
/**
@@ -119,7 +136,7 @@ public function timeEntriesLoadRecent($start_date = NULL, $end_date = NULL) {
119136
*/
120137
public function timeEntrySave($timeEntry) {
121138
$options['method'] = !empty($timeEntry->id) ? 'PUT' : 'POST';
122-
$url = 'time_entries' . !empty($timeEntry->id) ? '/' . $timeEntry->id : '';
139+
$url = 'time_entries' . (!empty($timeEntry->id) ? '/' . $timeEntry->id : '');
123140
$options['data']['time_entry'] = $timeEntry;
124141

125142
$response = $this->request($this->getURL($url), $options);
@@ -152,7 +169,7 @@ public function clientLoadAll() {
152169

153170
public function clientSave($client) {
154171
$options['method'] = !empty($client->id) ? 'PUT' : 'POST';
155-
$url = 'clients' . !empty($client->id) ? '/' . $client->id : '';
172+
$url = 'clients' . (!empty($client->id) ? '/' . $client->id : '');
156173
$options['data']['client'] = $client;
157174

158175
$response = $this->request($this->getURL($url), $options);
@@ -175,7 +192,7 @@ public function projectLoadAll() {
175192

176193
public function projectSave($project) {
177194
$options['method'] = !empty($project->id) ? 'PUT' : 'POST';
178-
$url = 'projects' . !empty($project->id) ? '/' . $project->id : '';
195+
$url = 'projects' . (!empty($project->id) ? '/' . $project->id : '');
179196
$options['data']['project'] = $project;
180197

181198
$response = $this->request($this->getURL($url), $options);
@@ -211,3 +228,20 @@ public function delete() {
211228
$this->parent->timeEntryDelete($this);
212229
}
213230
}
231+
232+
function toggl_filter_array_set_variables(array $variables = NULL) {
233+
static $stored_variables = array();
234+
235+
if (isset($stored_variables)) {
236+
$stored_variables = $variables;
237+
}
238+
239+
return $variables;
240+
}
241+
242+
function toggl_filter_array($item) {
243+
$variables = toggl_filter_array_set_variables();
244+
foreach ($variables as $key => $value) {
245+
246+
}
247+
}

0 commit comments

Comments
 (0)