1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace TonicForHealth\PagerDutyClient\Client; |
4
|
|
|
|
5
|
|
|
use Http\Client\Exception as HttpClientException; |
6
|
|
|
use Http\Client\Common\HttpMethodsClient; |
7
|
|
|
use Http\Discovery\HttpClientDiscovery; |
8
|
|
|
use Http\Discovery\MessageFactoryDiscovery; |
9
|
|
|
use Psr\Http\Message\ResponseInterface; |
10
|
|
|
use stdClass; |
11
|
|
|
use TonicForHealth\PagerDutyClient\Client\Exception\EventApiResponseErrorException; |
12
|
|
|
use TonicForHealth\PagerDutyClient\Client\Exception\EventClientException; |
13
|
|
|
use TonicForHealth\PagerDutyClient\Entity\Event\EventRepresentation; |
14
|
|
|
use TonicForHealth\PagerDutyClient\Entity\PagerDutyEntityInterface; |
15
|
|
|
use TonicForHealth\PagerDutyClient\RepresentProcessor\RepresentProcessor; |
16
|
|
|
use TonicForHealth\PagerDutyClient\RepresentProcessor\RepresentProcessorInterface; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* Class EventClient. |
20
|
|
|
*/ |
21
|
|
|
class EventClient |
22
|
|
|
{ |
23
|
|
|
protected $headers = ['Content-type' => 'application/json']; |
24
|
|
|
/** |
25
|
|
|
* @var HttpMethodsClient |
26
|
|
|
*/ |
27
|
|
|
protected $httpClient; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* @var string |
31
|
|
|
*/ |
32
|
|
|
protected $apiRootUrl; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* @var RepresentProcessorInterface |
36
|
|
|
*/ |
37
|
|
|
protected $representProcessor; |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* RequestNotificationType constructor. |
41
|
|
|
* |
42
|
|
|
* @param string $apiRootUrl |
43
|
|
|
* @param HttpMethodsClient $httpClient |
44
|
|
|
* @param RepresentProcessorInterface $representProcessor |
45
|
|
|
*/ |
46
|
1 |
|
public function __construct( |
47
|
|
|
$apiRootUrl, |
48
|
|
|
HttpMethodsClient $httpClient = null, |
49
|
|
|
RepresentProcessorInterface $representProcessor = null |
50
|
|
|
) { |
51
|
1 |
|
$this->setApiRootUrl($apiRootUrl); |
52
|
|
|
|
53
|
1 |
|
if (null === $httpClient) { |
54
|
|
|
$httpClient = $this->createHttpClient(); |
55
|
|
|
} |
56
|
1 |
|
$this->setHttpClient($httpClient); |
57
|
|
|
|
58
|
1 |
|
if (null === $representProcessor) { |
59
|
|
|
$representProcessor = $this->createRepresentProcessor(); |
60
|
|
|
} |
61
|
1 |
|
$this->setRepresentProcessor($representProcessor); |
62
|
|
|
|
63
|
1 |
|
$this->addBasicRepresentation(); |
64
|
1 |
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* @param PagerDutyEntityInterface $pagerDutyEntity |
68
|
|
|
* |
69
|
|
|
* @return ResponseInterface |
70
|
|
|
* |
71
|
|
|
* @throws EventApiResponseErrorException |
72
|
|
|
*/ |
73
|
1 |
|
public function post(PagerDutyEntityInterface $pagerDutyEntity) |
74
|
|
|
{ |
75
|
1 |
|
$data = null; |
76
|
|
|
|
77
|
|
|
try { |
78
|
1 |
|
$response = $this->getHttpClient()->post( |
79
|
1 |
|
$this->getResourceUrl($pagerDutyEntity), |
80
|
1 |
|
$this->headers, |
81
|
1 |
|
$this->getRepresentProcessor()->representJSON($pagerDutyEntity) |
82
|
|
|
); |
83
|
|
|
|
84
|
1 |
|
$data = $this->performeResponse($response); |
85
|
1 |
|
} catch (HttpClientException $exception) { |
86
|
|
|
EventClientException::internalProblem($exception); |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
return $data; |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* @return HttpMethodsClient |
94
|
|
|
*/ |
95
|
1 |
|
public function getHttpClient() |
96
|
|
|
{ |
97
|
1 |
|
return $this->httpClient; |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
/** |
101
|
|
|
* @return string |
102
|
|
|
*/ |
103
|
1 |
|
public function getApiRootUrl() |
104
|
|
|
{ |
105
|
1 |
|
return $this->apiRootUrl; |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
/** |
109
|
|
|
* @return RepresentProcessorInterface |
110
|
|
|
*/ |
111
|
1 |
|
public function getRepresentProcessor() |
112
|
|
|
{ |
113
|
1 |
|
return $this->representProcessor; |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
/** |
117
|
|
|
* @param HttpMethodsClient $httpClient |
118
|
|
|
*/ |
119
|
1 |
|
protected function setHttpClient($httpClient) |
120
|
|
|
{ |
121
|
1 |
|
$this->httpClient = $httpClient; |
122
|
1 |
|
} |
123
|
|
|
|
124
|
|
|
/** |
125
|
|
|
* @param string $apiRootUrl |
126
|
|
|
*/ |
127
|
1 |
|
protected function setApiRootUrl($apiRootUrl) |
128
|
|
|
{ |
129
|
1 |
|
$this->apiRootUrl = $apiRootUrl; |
130
|
1 |
|
} |
131
|
|
|
|
132
|
|
|
/** |
133
|
|
|
* @param RepresentProcessorInterface $representProcessor |
134
|
|
|
*/ |
135
|
1 |
|
protected function setRepresentProcessor($representProcessor) |
136
|
|
|
{ |
137
|
1 |
|
$this->representProcessor = $representProcessor; |
138
|
1 |
|
} |
139
|
|
|
|
140
|
|
|
/** |
141
|
|
|
* add basic representation for EventClient. |
142
|
|
|
*/ |
143
|
1 |
|
protected function addBasicRepresentation() |
144
|
|
|
{ |
145
|
1 |
|
$this->getRepresentProcessor()->addRepresentation(new EventRepresentation()); |
146
|
1 |
|
} |
147
|
|
|
|
148
|
|
|
/** |
149
|
|
|
* @return HttpMethodsClient |
150
|
|
|
*/ |
151
|
|
|
protected function createHttpClient() |
152
|
|
|
{ |
153
|
|
|
$httpClient = new HttpMethodsClient( |
154
|
|
|
HttpClientDiscovery::find(), |
155
|
|
|
MessageFactoryDiscovery::find() |
156
|
|
|
); |
157
|
|
|
|
158
|
|
|
return $httpClient; |
159
|
|
|
} |
160
|
|
|
|
161
|
|
|
/** |
162
|
|
|
* @return RepresentProcessor |
163
|
|
|
*/ |
164
|
|
|
protected function createRepresentProcessor() |
165
|
|
|
{ |
166
|
|
|
return new RepresentProcessor(); |
167
|
|
|
} |
168
|
|
|
|
169
|
|
|
/** |
170
|
|
|
* @param PagerDutyEntityInterface $pagerDutyEntity |
171
|
|
|
* |
172
|
|
|
* @return string |
173
|
|
|
*/ |
174
|
1 |
|
private function getResourceUrl(PagerDutyEntityInterface $pagerDutyEntity) |
175
|
|
|
{ |
176
|
1 |
|
return sprintf( |
177
|
1 |
|
'%s/%s', |
178
|
1 |
|
$this->getApiRootUrl(), |
179
|
1 |
|
$this->getRepresentProcessor()->getRESTResourcePath($pagerDutyEntity) |
180
|
|
|
); |
181
|
|
|
} |
182
|
|
|
|
183
|
|
|
/** |
184
|
|
|
* @param $response |
185
|
|
|
* |
186
|
|
|
* @return bool|\stdClass |
187
|
|
|
* |
188
|
|
|
* @throws EventApiResponseErrorException |
189
|
|
|
*/ |
190
|
1 |
|
private function performeResponse($response) |
191
|
|
|
{ |
192
|
1 |
|
$data = false; |
193
|
1 |
|
if ($response instanceof ResponseInterface) { |
194
|
1 |
|
$data = json_decode($response->getBody()->getContents()); |
195
|
1 |
|
self::validateResponseData($data); |
196
|
|
|
} |
197
|
|
|
|
198
|
|
|
return $data; |
199
|
|
|
} |
200
|
|
|
|
201
|
|
|
/** |
202
|
|
|
* @param stdClass $data |
203
|
|
|
* @throws EventApiResponseErrorException |
204
|
|
|
*/ |
205
|
|
|
private static function validateResponseData(stdClass $data) |
206
|
|
|
{ |
207
|
|
|
if (isset($data->errors)) { |
208
|
|
|
throw EventApiResponseErrorException::eventApiResponseError($data->errors[0]); |
209
|
|
|
} |
210
|
|
|
} |
211
|
|
|
} |
212
|
|
|
|