1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace TonicForHealth\PagerDutyClient\Client; |
4
|
|
|
|
5
|
|
|
use Http\Client\Exception as HttpClientException; |
6
|
|
|
use Psr\Http\Message\ResponseInterface; |
7
|
|
|
use stdClass; |
8
|
|
|
use TonicForHealth\PagerDutyClient\Client\Exception\EventClientTransportException; |
9
|
|
|
use TonicForHealth\PagerDutyClient\Client\Exception\ResponseDataValidationException; |
10
|
|
|
use TonicForHealth\PagerDutyClient\Entity\PagerDutyEntityInterface; |
11
|
|
|
use TonicForHealth\PagerDutyClient\Validation\Event\Exception\EventValidationResponseException; |
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* Class EventClient. |
15
|
|
|
*/ |
16
|
|
|
class EventClient extends AbstractClient |
17
|
|
|
{ |
18
|
|
|
/** |
19
|
|
|
* Make HTTP POST Request to the Event api. |
20
|
|
|
* |
21
|
|
|
* @param PagerDutyEntityInterface $pagerDutyEntity |
22
|
|
|
* |
23
|
|
|
* @return stdClass |
24
|
|
|
* |
25
|
|
|
* @throws EventClientTransportException |
26
|
|
|
* @throws ResponseDataValidationException |
27
|
|
|
*/ |
28
|
4 |
|
public function post(PagerDutyEntityInterface $pagerDutyEntity) |
29
|
|
|
{ |
30
|
4 |
|
$data = null; |
31
|
|
|
|
32
|
|
|
try { |
33
|
4 |
|
$response = $this->getHttpClient()->post( |
34
|
4 |
|
$this->getResourceUrl($pagerDutyEntity), |
35
|
4 |
|
$this->headers, |
36
|
4 |
|
$this->getRepresentProcessor()->representJSON($pagerDutyEntity) |
37
|
4 |
|
); |
38
|
|
|
|
39
|
3 |
|
$data = $this->performResponse($response); |
40
|
4 |
|
} catch (HttpClientException $exception) { |
41
|
1 |
|
throw EventClientTransportException::transportProblem($exception); |
42
|
2 |
|
} catch (EventValidationResponseException $exception) { |
43
|
2 |
|
throw ResponseDataValidationException::validationFail($exception); |
44
|
|
|
} |
45
|
|
|
|
46
|
1 |
|
return $data; |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* Get Full resource Url. |
51
|
|
|
* |
52
|
|
|
* @param PagerDutyEntityInterface $pagerDutyEntity |
53
|
|
|
* |
54
|
|
|
* @return string |
55
|
|
|
*/ |
56
|
4 |
|
private function getResourceUrl(PagerDutyEntityInterface $pagerDutyEntity) |
57
|
|
|
{ |
58
|
4 |
|
return sprintf( |
59
|
4 |
|
'%s/%s', |
60
|
4 |
|
$this->getApiRootUrl(), |
61
|
4 |
|
$this->getRepresentProcessor()->getRESTResourcePath($pagerDutyEntity) |
62
|
4 |
|
); |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* Perform response and gets it date. |
67
|
|
|
* |
68
|
|
|
* @param mixed $response |
69
|
|
|
* |
70
|
|
|
* @return stdClass |
71
|
|
|
* |
72
|
|
|
* @throws EventValidationResponseException |
73
|
|
|
*/ |
74
|
3 |
|
private function performResponse($response) |
75
|
|
|
{ |
76
|
3 |
|
$data = false; |
77
|
3 |
|
if ($response instanceof ResponseInterface) { |
78
|
3 |
|
$data = json_decode($response->getBody()->getContents()); |
79
|
3 |
|
$this->getValidationResponse()->validateResponseData($data); |
80
|
1 |
|
} |
81
|
|
|
|
82
|
1 |
|
return $data; |
83
|
|
|
} |
84
|
|
|
} |
85
|
|
|
|