Issues (36)

src/Client.php (1 issue)

1
<?php
2
3
namespace DevoraliveCachet;
4
5
use DevoraliveCachet\Entity\Point;
6
use DevoraliveCachet\Result\Point\PointEnvelope;
7
use JMS\Serializer\SerializerBuilder;
8
use DevoraliveCachet\Entity\Metric;
9
use DevoraliveCachet\Result\Generic\StringEnvelope;
10
use DevoraliveCachet\Entity\Component;
11
use DevoraliveCachet\Result\Component\ComponentEnvelope;
12
use DevoraliveCachet\Result\Component\ComponentsEnvelope;
13
use DevoraliveCachet\Entity\Incident;
14
use DevoraliveCachet\Result\Incident\IncidentEnvelope;
15
use DevoraliveCachet\Result\Incident\IncidentsEnvelope;
16
use DevoraliveCachet\Entity\Group;
17
use DevoraliveCachet\Result\Group\GroupEnvelope;
18
use DevoraliveCachet\Result\Group\GroupsEnvelope;
19
use DevoraliveCachet\Http\RequestHandler;
20
use DevoraliveCachet\Result\Metric\MetricEnvelope;
21
use DevoraliveCachet\Result\Metric\MetricsEnvelope;
22
use GuzzleHttp\Exception\GuzzleException;
23
24
/**
25
 * Class Client
26
 * @package DevoraliveCachet
27
 */
28
class Client
29
{
30
    /**
31
     * @var string
32
     */
33
    private $endpoint;
34
35
    /**
36
     * @var string
37
     */
38
    private $token;
39
40
    /**
41
     * RequestHandler
42
     */
43
    private $handler;
44
45
    /**
46
     * Client constructor.
47
     *
48
     * @param      $endpoint
49
     * @param null $token
0 ignored issues
show
Documentation Bug introduced by
Are you sure the doc-type for parameter $token is correct as it would always require null to be passed?
Loading history...
50
     */
51
    public function __construct($endpoint, $token = null)
52
    {
53
        $this->endpoint = $endpoint;
54
        $this->token = $token;
55
56
        $serializer = SerializerBuilder::create()->build();
57
        $this->handler = new RequestHandler($this, $serializer);
58
    }
59
60
    /**
61
     * Get the value of Endpoint
62
     *
63
     * @return string
64
     */
65
    public function getEndpoint()
66
    {
67
        return $this->endpoint;
68
    }
69
70
    /**
71
     * Get the value of Token
72
     *
73
     * @return string
74
     */
75
    public function getToken()
76
    {
77
        return $this->token;
78
    }
79
80
    /**
81
     * @return mixed
82
     * @throws GuzzleException
83
     */
84
    public function ping()
85
    {
86
        $envelope = $this->handler->get('ping', StringEnvelope::class);
87
88
        return $envelope->getData();
89
    }
90
91
    /**
92
     * @param array $params
93
     *
94
     * @return Component[]
95
     * @throws GuzzleException
96
     */
97
    public function getComponents(array $params = [])
98
    {
99
        $envelope = $this->handler->get('components', ComponentsEnvelope::class, $params);
100
101
        return $envelope->getData();
102
    }
103
104
    /**
105
     * @param $id
106
     *
107
     * @return Component
108
     * @throws GuzzleException
109
     */
110
    public function getComponent($id)
111
    {
112
        $envelope = $this->handler->get('components/' . $id, ComponentEnvelope::class);
113
114
        return $envelope->getData();
115
    }
116
117
    /**
118
     * @param Component $component
119
     *
120
     * @return Component
121
     * @throws GuzzleException
122
     */
123
    public function addComponent(Component $component)
124
    {
125
        $envelope = $this->handler->post('components', ComponentEnvelope::class, $component);
126
127
        return $envelope->getData();
128
    }
129
130
    /**
131
     * @param Component $component
132
     *
133
     * @return Component
134
     * @throws GuzzleException
135
     */
136
    public function updateComponent(Component $component)
137
    {
138
        $envelope = $this->handler->put('components/' . $component->getId(), ComponentEnvelope::class, $component);
139
140
        return $envelope->getData();
141
    }
142
143
    /**
144
     * @param $id
145
     *
146
     * @return bool
147
     * @throws GuzzleException
148
     */
149
    public function deleteComponent($id)
150
    {
151
        $this->handler->delete('components/' . $id, $id);
152
153
        return true;
154
    }
155
156
    /**
157
     * @param array $params
158
     *
159
     * @return Incident[]
160
     * @throws GuzzleException
161
     */
162
    public function getIncidents(array $params = [])
163
    {
164
        $envelope = $this->handler->get('incidents', IncidentsEnvelope::class, $params);
165
166
        return $envelope->getData();
167
    }
168
169
    /**
170
     * @param int $id
171
     *
172
     * @return Incident
173
     * @throws GuzzleException
174
     */
175
    public function getIncident($id)
176
    {
177
        $envelope = $this->handler->get('incidents/' . $id, IncidentEnvelope::class);
178
179
        return $envelope->getData();
180
    }
181
182
    /**
183
     * @param Incident $incident
184
     *
185
     * @return Incident
186
     * @throws GuzzleException
187
     */
188
    public function addIncident(Incident $incident)
189
    {
190
        $envelope = $this->handler->post('incidents', IncidentEnvelope::class, $incident);
191
192
        return $envelope->getData();
193
    }
194
195
    /**
196
     * @param Incident $incident
197
     *
198
     * @return Incident
199
     * @throws GuzzleException
200
     */
201
    public function updateIncident(Incident $incident)
202
    {
203
        $envelope = $this->handler->put('incidents/' . $incident->getId(), IncidentEnvelope::class, $incident);
204
205
        return $envelope->getData();
206
    }
207
208
    /**
209
     * @param $id
210
     *
211
     * @return bool
212
     * @throws GuzzleException
213
     */
214
    public function deleteIncident($id)
215
    {
216
        $this->handler->delete('incidents/' . $id, $id);
217
218
        return true;
219
    }
220
221
    /**
222
     * @param array $params
223
     *
224
     * @return Group[]
225
     * @throws GuzzleException
226
     */
227
    public function getGroups(array $params = [])
228
    {
229
        $envelope = $this->handler->get('components/groups', GroupsEnvelope::class, $params);
230
231
        return $envelope->getData();
232
    }
233
234
    /**
235
     * @param int $id
236
     *
237
     * @return Component
238
     * @throws GuzzleException
239
     */
240
    public function getGroup($id)
241
    {
242
        $envelope = $this->handler->get('components/groups/' . $id, GroupEnvelope::class);
243
244
        return $envelope->getData();
245
    }
246
247
    /**
248
     * @param array $params
249
     *
250
     * @return Metric[]
251
     * @throws GuzzleException
252
     */
253
    public function getMetrics(array $params = [])
254
    {
255
        $envelope = $this->handler->get('metrics', MetricsEnvelope::class, $params);
256
257
        return $envelope->getData();
258
    }
259
260
    /**
261
     * @param int $id
262
     *
263
     * @return Metric
264
     * @throws GuzzleException
265
     */
266
    public function getMetric($id)
267
    {
268
        $envelope = $this->handler->get('metrics/' . $id, MetricEnvelope::class);
269
270
        return $envelope->getData();
271
    }
272
273
    /**
274
     * @param Metric $metric
275
     *
276
     * @return Metric
277
     * @throws GuzzleException
278
     */
279
    public function addMetric(Metric $metric)
280
    {
281
        $envelope = $this->handler->post('metrics', MetricEnvelope::class, $metric);
282
283
        return $envelope->getData();
284
    }
285
286
    /**
287
     * @param Metric $metric
288
     *
289
     * @return Metric
290
     * @throws GuzzleException
291
     */
292
    public function updateMetric(Metric $metric)
293
    {
294
        $envelope = $this->handler->put('metrics/' . $metric->getId(), MetricEnvelope::class, $metric);
295
296
        return $envelope->getData();
297
    }
298
299
    /**
300
     * @param $id
301
     *
302
     * @return bool
303
     * @throws GuzzleException
304
     */
305
    public function deleteMetric($id)
306
    {
307
        $this->handler->delete('metrics/' . $id, $id);
308
309
        return true;
310
    }
311
312
    /**
313
     * @param Metric $metric
314
     * @param Point  $point
315
     *
316
     * @return mixed
317
     * @throws GuzzleException
318
     */
319
    public function addMetricPoint(Metric $metric, Point $point)
320
    {
321
        $envelope = $this->handler->post('metrics/' . $metric->getId() . '/points', PointEnvelope::class, $point);
322
323
        return $envelope->getData();
324
    }
325
}
326