|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace TonicHealthCheck\Incident\Siren\NotificationType; |
|
4
|
|
|
|
|
5
|
|
|
use TonicForHealth\PagerDutyClient\Client\EventClient; |
|
6
|
|
|
use TonicForHealth\PagerDutyClient\Entity\Event\Event; |
|
7
|
|
|
use TonicHealthCheck\Incident\IncidentInterface; |
|
8
|
|
|
use TonicHealthCheck\Incident\Siren\Subject\SubjectInterface; |
|
9
|
|
|
|
|
10
|
|
|
/** |
|
11
|
|
|
* Class PagerDutyNotificationType. |
|
12
|
|
|
*/ |
|
13
|
|
|
class PagerDutyNotificationType implements NotificationTypeInterface |
|
14
|
|
|
{ |
|
15
|
|
|
/** |
|
16
|
|
|
* PagerDuty Event Api client. |
|
17
|
|
|
* |
|
18
|
|
|
* @var EventClient |
|
19
|
|
|
*/ |
|
20
|
|
|
protected $eventClient; |
|
21
|
|
|
|
|
22
|
|
|
/** |
|
23
|
|
|
* @var string; |
|
24
|
|
|
*/ |
|
25
|
|
|
public $serviceKey; |
|
26
|
|
|
|
|
27
|
|
|
/** |
|
28
|
|
|
* RequestNotificationType constructor. |
|
29
|
|
|
* |
|
30
|
|
|
* @param EventClient $eventClient |
|
31
|
|
|
* @param string $serviceKey |
|
32
|
|
|
*/ |
|
33
|
2 |
|
public function __construct(EventClient $eventClient, $serviceKey) |
|
34
|
|
|
{ |
|
35
|
2 |
|
$this->setEventClient($eventClient); |
|
36
|
2 |
|
$this->setServiceKey($serviceKey); |
|
37
|
2 |
|
} |
|
38
|
|
|
|
|
39
|
|
|
/** |
|
40
|
|
|
* @param SubjectInterface $subject |
|
41
|
|
|
* @param IncidentInterface $incident |
|
42
|
|
|
*/ |
|
43
|
1 |
|
public function notify(SubjectInterface $subject, IncidentInterface $incident) |
|
44
|
|
|
{ |
|
45
|
1 |
|
$event = new Event(); |
|
46
|
1 |
|
$event->serviceKey = $this->getServiceKey(); |
|
47
|
1 |
|
$event->incidentKey = sprintf('hci_%d', $incident->getId()); |
|
48
|
1 |
|
if ($incident->getStatus() != IncidentInterface::STATUS_OK) { |
|
49
|
1 |
|
$event->description = $incident->getIdent(); |
|
50
|
1 |
|
$event->eventType = Event::EVENT_TYPE_TRIGGER; |
|
51
|
1 |
|
$event->details = [ |
|
52
|
1 |
|
'log' => $incident->getMessage(), |
|
53
|
1 |
|
'status' => $incident->getStatus(), |
|
54
|
1 |
|
'type' => $incident->getType(), |
|
55
|
1 |
|
'id' => $incident->getId(), |
|
56
|
|
|
]; |
|
57
|
|
|
} else { |
|
58
|
1 |
|
$event->eventType = Event::EVENT_TYPE_RESOLVE; |
|
59
|
|
|
} |
|
60
|
1 |
|
$this->getEventClient()->post($event); |
|
61
|
1 |
|
} |
|
62
|
|
|
|
|
63
|
|
|
/** |
|
64
|
|
|
* @return EventClient |
|
65
|
|
|
*/ |
|
66
|
1 |
|
public function getEventClient() |
|
67
|
|
|
{ |
|
68
|
1 |
|
return $this->eventClient; |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
|
/** |
|
72
|
|
|
* @return string |
|
73
|
|
|
*/ |
|
74
|
1 |
|
public function getServiceKey() |
|
75
|
|
|
{ |
|
76
|
1 |
|
return $this->serviceKey; |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
|
|
/** |
|
80
|
|
|
* @param EventClient $eventClient |
|
81
|
|
|
*/ |
|
82
|
2 |
|
protected function setEventClient($eventClient) |
|
83
|
|
|
{ |
|
84
|
2 |
|
$this->eventClient = $eventClient; |
|
85
|
2 |
|
} |
|
86
|
|
|
|
|
87
|
|
|
/** |
|
88
|
|
|
* @param string $serviceKey |
|
89
|
|
|
*/ |
|
90
|
2 |
|
protected function setServiceKey($serviceKey) |
|
91
|
|
|
{ |
|
92
|
2 |
|
$this->serviceKey = $serviceKey; |
|
93
|
2 |
|
} |
|
94
|
|
|
} |
|
95
|
|
|
|