|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace TonicHealthCheck\Incident\Siren\NotificationType; |
|
4
|
|
|
|
|
5
|
|
|
use Http\Client\Common\HttpMethodsClient; |
|
6
|
|
|
use Psr\Http\Message\ResponseInterface; |
|
7
|
|
|
use TonicHealthCheck\Incident\IncidentInterface; |
|
8
|
|
|
use TonicHealthCheck\Incident\Siren\Subject\SubjectInterface; |
|
9
|
|
|
|
|
10
|
|
|
/** |
|
11
|
|
|
* Class RequestNotificationType. |
|
12
|
|
|
*/ |
|
13
|
|
|
class RequestNotificationType implements NotificationTypeInterface |
|
14
|
|
|
{ |
|
15
|
|
|
/** |
|
16
|
|
|
* @var HttpMethodsClient |
|
17
|
|
|
*/ |
|
18
|
|
|
protected $httpClient; |
|
19
|
|
|
|
|
20
|
|
|
/** |
|
21
|
|
|
* @var string |
|
22
|
|
|
*/ |
|
23
|
|
|
protected $resourceUrl; |
|
24
|
|
|
|
|
25
|
|
|
/** |
|
26
|
|
|
* RequestNotificationType constructor. |
|
27
|
|
|
* |
|
28
|
|
|
* @param HttpMethodsClient $httpClient |
|
29
|
|
|
* @param string $resourceUrl |
|
30
|
|
|
*/ |
|
31
|
2 |
|
public function __construct(HttpMethodsClient $httpClient, $resourceUrl) |
|
32
|
|
|
{ |
|
33
|
2 |
|
$this->setHttpClient($httpClient); |
|
34
|
2 |
|
$this->setResourceUrl($resourceUrl); |
|
35
|
2 |
|
} |
|
36
|
|
|
|
|
37
|
|
|
/** |
|
38
|
|
|
* @param SubjectInterface $subject |
|
39
|
|
|
* @param IncidentInterface $incident |
|
40
|
|
|
*/ |
|
41
|
2 |
|
public function notify(SubjectInterface $subject, IncidentInterface $incident) |
|
42
|
|
|
{ |
|
43
|
2 |
|
$serverUrl = $subject; |
|
44
|
2 |
|
if ($incident->getStatus() != IncidentInterface::STATUS_OK) { |
|
45
|
1 |
|
$this->incidentCreate($incident, $serverUrl); |
|
46
|
|
|
} else { |
|
47
|
1 |
|
$this->incidentUpdate($incident, $serverUrl); |
|
48
|
|
|
} |
|
49
|
2 |
|
} |
|
50
|
|
|
|
|
51
|
|
|
/** |
|
52
|
|
|
* @return HttpMethodsClient |
|
53
|
|
|
*/ |
|
54
|
2 |
|
public function getHttpClient() |
|
55
|
|
|
{ |
|
56
|
2 |
|
return $this->httpClient; |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
|
/** |
|
60
|
|
|
* @return string |
|
61
|
|
|
*/ |
|
62
|
2 |
|
public function getResourceUrl() |
|
63
|
|
|
{ |
|
64
|
2 |
|
return $this->resourceUrl; |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
/** |
|
68
|
|
|
* @param HttpMethodsClient $httpClient |
|
69
|
|
|
*/ |
|
70
|
2 |
|
protected function setHttpClient(HttpMethodsClient $httpClient) |
|
71
|
|
|
{ |
|
72
|
2 |
|
$this->httpClient = $httpClient; |
|
73
|
2 |
|
} |
|
74
|
|
|
|
|
75
|
|
|
/** |
|
76
|
|
|
* @param string $resourceUrl |
|
77
|
|
|
*/ |
|
78
|
2 |
|
protected function setResourceUrl($resourceUrl) |
|
79
|
|
|
{ |
|
80
|
2 |
|
$this->resourceUrl = $resourceUrl; |
|
81
|
2 |
|
} |
|
82
|
|
|
|
|
83
|
|
|
/** |
|
84
|
|
|
* @param IncidentInterface $incident |
|
85
|
|
|
* @param $serverUrl |
|
86
|
|
|
*/ |
|
87
|
1 |
|
protected function incidentCreate(IncidentInterface $incident, $serverUrl) |
|
88
|
|
|
{ |
|
89
|
1 |
|
$response = $this->getHttpClient()->post( |
|
90
|
1 |
|
$serverUrl.$this->getResourceUrl(), |
|
91
|
1 |
|
['Content-type' => 'application/json'], |
|
92
|
|
|
json_encode( |
|
93
|
|
|
[ |
|
94
|
1 |
|
'name' => $incident->getIdent(), |
|
95
|
1 |
|
'message' => $incident->getMessage(), |
|
96
|
1 |
|
'status' => 1, |
|
97
|
1 |
|
'visible' => 1, |
|
98
|
|
|
] |
|
99
|
|
|
) |
|
100
|
|
|
); |
|
101
|
|
|
|
|
102
|
1 |
|
if ($response instanceof ResponseInterface) { |
|
103
|
1 |
|
$data = json_decode($response->getBody()->getContents()); |
|
104
|
1 |
|
if (isset($data->data->id)) { |
|
105
|
1 |
|
$incident->setExternalId($data->data->id); |
|
106
|
|
|
} |
|
107
|
|
|
} |
|
108
|
1 |
|
} |
|
109
|
|
|
|
|
110
|
|
|
/** |
|
111
|
|
|
* @param IncidentInterface $incident |
|
112
|
|
|
* @param $serverUrl |
|
113
|
|
|
*/ |
|
114
|
1 |
|
protected function incidentUpdate(IncidentInterface $incident, $serverUrl) |
|
115
|
|
|
{ |
|
116
|
1 |
|
$this->getHttpClient()->put( |
|
117
|
1 |
|
$serverUrl.$this->getResourceUrl().'/'.$incident->getExternalId(), |
|
118
|
1 |
|
['Content-type' => 'application/json'], |
|
119
|
|
|
json_encode( |
|
120
|
|
|
[ |
|
121
|
1 |
|
'status' => 4, |
|
122
|
|
|
] |
|
123
|
|
|
) |
|
124
|
|
|
); |
|
125
|
1 |
|
} |
|
126
|
|
|
} |
|
127
|
|
|
|