1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Pixela\Api; |
4
|
|
|
|
5
|
|
|
class Graphs extends Api implements GraphsInterface |
6
|
|
|
{ |
7
|
|
|
/** |
8
|
|
|
* @var string |
9
|
|
|
*/ |
10
|
|
|
private $id; |
11
|
|
|
|
12
|
|
|
/** |
13
|
|
|
* @var string |
14
|
|
|
*/ |
15
|
|
|
private $name; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* @var string |
19
|
|
|
*/ |
20
|
|
|
private $unit; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* @var string |
24
|
|
|
*/ |
25
|
|
|
private $type; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* @var string |
29
|
|
|
*/ |
30
|
|
|
private $color; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* @var string |
34
|
|
|
*/ |
35
|
|
|
private $timezone = 'UTC'; |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* @var array |
39
|
|
|
*/ |
40
|
|
|
private $purgeCacheURLs; |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* @return bool |
44
|
|
|
* @throws \GuzzleHttp\Exception\GuzzleException |
45
|
|
|
*/ |
46
|
|
|
public function create() |
47
|
|
|
{ |
48
|
|
|
$uri = Api::API_BASE_ENDPOINT . '/users/' . $this->getClient()->getUsername() . '/graphs'; |
49
|
|
|
|
50
|
|
|
$options = array( |
51
|
|
|
'headers' => array( |
52
|
|
|
'X-USER-TOKEN' => $this->getClient()->getToken() |
53
|
|
|
), |
54
|
|
|
'body' => json_encode( |
55
|
|
|
array( |
56
|
|
|
'id' => $this->getId(), |
57
|
|
|
'name' => $this->getName(), |
58
|
|
|
'unit' => $this->getUnit(), |
59
|
|
|
'type' => $this->getType(), |
60
|
|
|
'color' => $this->getColor(), |
61
|
|
|
'timezone' => $this->getTimezone() |
62
|
|
|
) |
63
|
|
|
), |
64
|
|
|
); |
65
|
|
|
|
66
|
|
|
$response = $this->getClient()->getHttpClient()->request('post', $uri, $options); |
|
|
|
|
67
|
|
|
|
68
|
|
|
return true; |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* @return \Pixela\Api\GraphsInterface[] |
73
|
|
|
* @throws \GuzzleHttp\Exception\GuzzleException |
74
|
|
|
*/ |
75
|
|
|
public function get() |
76
|
|
|
{ |
77
|
|
|
$uri = Api::API_BASE_ENDPOINT . '/users/' . $this->getClient()->getUsername() . '/graphs/'; |
78
|
|
|
|
79
|
|
|
$options = array( |
80
|
|
|
'headers' => array( |
81
|
|
|
'X-USER-TOKEN' => $this->getClient()->getToken() |
82
|
|
|
), |
83
|
|
|
); |
84
|
|
|
|
85
|
|
|
$response = $this->getClient()->getHttpClient()->request('get', $uri, $options); |
86
|
|
|
$contents = json_decode($response->getBody()->getContents(), true); |
87
|
|
|
|
88
|
|
|
return array_map(function ($data) { |
89
|
|
|
$graph = new \Pixela\Api\Graphs($this->getClient()); |
90
|
|
|
|
91
|
|
|
$graph->setId($data['id']) |
92
|
|
|
->setName($data['name']) |
93
|
|
|
->setUnit($data['unit']) |
94
|
|
|
->setType($data['type']) |
95
|
|
|
->setColor($data['color']) |
96
|
|
|
->setTimezone($data['timezone']) |
97
|
|
|
->setPurgeCacheURLs($data['purgeCacheURLs']); |
98
|
|
|
|
99
|
|
|
return $graph; |
100
|
|
|
}, $contents['graphs']); |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
/** |
104
|
|
|
* @param \DateTimeInterface|null $dateTime |
105
|
|
|
* @param null $mode |
|
|
|
|
106
|
|
|
* @return string |
107
|
|
|
* @throws \GuzzleHttp\Exception\GuzzleException |
108
|
|
|
*/ |
109
|
|
|
public function getSVG(\DateTimeInterface $dateTime = null, $mode = null) |
110
|
|
|
{ |
111
|
|
|
$params = array(); |
112
|
|
|
if ($dateTime !== null) { |
113
|
|
|
$params['date'] = $dateTime->format('Ymd'); |
114
|
|
|
} |
115
|
|
|
if ($mode !== null) { |
|
|
|
|
116
|
|
|
$params['mode'] = $mode; |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
$uri = Api::API_BASE_ENDPOINT . '/users/' . $this->getClient()->getUsername() . '/graphs/' . $this->getId(); |
120
|
|
|
|
121
|
|
|
if ($params) { |
122
|
|
|
$uri .= '?' . \GuzzleHttp\Psr7\build_query($params); |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
$options = array(); |
126
|
|
|
|
127
|
|
|
$response = $this->getClient()->getHttpClient()->request('get', $uri, $options); |
128
|
|
|
|
129
|
|
|
return $response->getBody()->getContents(); |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
/** |
133
|
|
|
* @throws \GuzzleHttp\Exception\GuzzleException |
134
|
|
|
*/ |
135
|
|
|
public function update() |
136
|
|
|
{ |
137
|
|
|
$uri = Api::API_BASE_ENDPOINT . '/users/' . $this->getClient()->getUsername() . '/graphs/' . $this->getId(); |
138
|
|
|
|
139
|
|
|
$options = array( |
140
|
|
|
'headers' => array( |
141
|
|
|
'X-USER-TOKEN' => $this->getClient()->getToken(), |
142
|
|
|
), |
143
|
|
|
'body' => json_encode(array( |
144
|
|
|
'name' => $this->getName(), |
145
|
|
|
'unit' => $this->getUnit(), |
146
|
|
|
'color' => $this->getColor(), |
147
|
|
|
'timezone' => $this->getTimezone(), |
148
|
|
|
'purgeCacheURLs' => $this->getPurgeCacheURLs() |
149
|
|
|
)) |
150
|
|
|
); |
151
|
|
|
|
152
|
|
|
$response = $this->getClient()->getHttpClient()->request('put', $uri, $options); |
|
|
|
|
153
|
|
|
|
154
|
|
|
return true; |
155
|
|
|
} |
156
|
|
|
|
157
|
|
|
/** |
158
|
|
|
* @return bool |
159
|
|
|
* @throws \GuzzleHttp\Exception\GuzzleException |
160
|
|
|
*/ |
161
|
|
|
public function delete() |
162
|
|
|
{ |
163
|
|
|
$uri = Api::API_BASE_ENDPOINT . '/users/' . $this->getClient()->getUsername() . '/graphs/' . $this->getId(); |
164
|
|
|
|
165
|
|
|
$options = array( |
166
|
|
|
'headers' => array( |
167
|
|
|
'X-USER-TOKEN' => $this->getClient()->getToken() |
168
|
|
|
), |
169
|
|
|
); |
170
|
|
|
|
171
|
|
|
$response = $this->getClient()->getHttpClient()->request('delete', $uri, $options); |
|
|
|
|
172
|
|
|
|
173
|
|
|
return true; |
174
|
|
|
} |
175
|
|
|
|
176
|
|
|
/** |
177
|
|
|
* @return string |
178
|
|
|
*/ |
179
|
|
|
public function getURL() |
180
|
|
|
{ |
181
|
|
|
return sprintf( |
182
|
|
|
Api::API_BASE_ENDPOINT . '/users/%s/graphs/%s.html', |
183
|
|
|
$this->getClient()->getUsername(), |
184
|
|
|
$this->getId() |
185
|
|
|
); |
186
|
|
|
} |
187
|
|
|
|
188
|
|
|
/** |
189
|
|
|
* @return string |
190
|
|
|
*/ |
191
|
|
|
public function getId() |
192
|
|
|
{ |
193
|
|
|
return $this->id; |
194
|
|
|
} |
195
|
|
|
|
196
|
|
|
/** |
197
|
|
|
* @param string $id |
198
|
|
|
*/ |
199
|
|
|
public function setId($id) |
200
|
|
|
{ |
201
|
|
|
$this->id = $id; |
202
|
|
|
|
203
|
|
|
return $this; |
204
|
|
|
} |
205
|
|
|
|
206
|
|
|
/** |
207
|
|
|
* @return string |
208
|
|
|
*/ |
209
|
|
|
public function getName() |
210
|
|
|
{ |
211
|
|
|
return $this->name; |
212
|
|
|
} |
213
|
|
|
|
214
|
|
|
/** |
215
|
|
|
* @param string $name |
216
|
|
|
*/ |
217
|
|
|
public function setName($name) |
218
|
|
|
{ |
219
|
|
|
$this->name = $name; |
220
|
|
|
|
221
|
|
|
return $this; |
222
|
|
|
} |
223
|
|
|
|
224
|
|
|
/** |
225
|
|
|
* @return string |
226
|
|
|
*/ |
227
|
|
|
public function getUnit() |
228
|
|
|
{ |
229
|
|
|
return $this->unit; |
230
|
|
|
} |
231
|
|
|
|
232
|
|
|
/** |
233
|
|
|
* @param string $unit |
234
|
|
|
*/ |
235
|
|
|
public function setUnit($unit) |
236
|
|
|
{ |
237
|
|
|
$this->unit = $unit; |
238
|
|
|
|
239
|
|
|
return $this; |
240
|
|
|
} |
241
|
|
|
|
242
|
|
|
/** |
243
|
|
|
* @return string |
244
|
|
|
*/ |
245
|
|
|
public function getType() |
246
|
|
|
{ |
247
|
|
|
return $this->type; |
248
|
|
|
} |
249
|
|
|
|
250
|
|
|
/** |
251
|
|
|
* @param string $type |
252
|
|
|
*/ |
253
|
|
|
public function setType($type) |
254
|
|
|
{ |
255
|
|
|
$this->type = $type; |
256
|
|
|
|
257
|
|
|
return $this; |
258
|
|
|
} |
259
|
|
|
|
260
|
|
|
/** |
261
|
|
|
* @return string |
262
|
|
|
*/ |
263
|
|
|
public function getColor() |
264
|
|
|
{ |
265
|
|
|
return $this->color; |
266
|
|
|
} |
267
|
|
|
|
268
|
|
|
/** |
269
|
|
|
* @param string $color |
270
|
|
|
*/ |
271
|
|
|
public function setColor($color) |
272
|
|
|
{ |
273
|
|
|
$this->color = $color; |
274
|
|
|
|
275
|
|
|
return $this; |
276
|
|
|
} |
277
|
|
|
|
278
|
|
|
/** |
279
|
|
|
* @return string |
280
|
|
|
*/ |
281
|
|
|
public function getTimezone() |
282
|
|
|
{ |
283
|
|
|
return $this->timezone; |
284
|
|
|
} |
285
|
|
|
|
286
|
|
|
/** |
287
|
|
|
* @param string $timezone |
288
|
|
|
*/ |
289
|
|
|
public function setTimezone($timezone) |
290
|
|
|
{ |
291
|
|
|
$this->timezone = $timezone; |
292
|
|
|
|
293
|
|
|
return $this; |
294
|
|
|
} |
295
|
|
|
|
296
|
|
|
/** |
297
|
|
|
* @return array |
298
|
|
|
*/ |
299
|
|
|
public function getPurgeCacheURLs() |
300
|
|
|
{ |
301
|
|
|
return $this->purgeCacheURLs; |
302
|
|
|
} |
303
|
|
|
|
304
|
|
|
/** |
305
|
|
|
* @param array $purgeCacheURLs |
306
|
|
|
*/ |
307
|
|
|
public function setPurgeCacheURLs($purgeCacheURLs = array()) |
308
|
|
|
{ |
309
|
|
|
$this->purgeCacheURLs = $purgeCacheURLs; |
310
|
|
|
} |
311
|
|
|
} |
312
|
|
|
|