1
|
|
|
<?php |
|
|
|
|
2
|
|
|
|
3
|
|
|
namespace TonicHealthCheck\Incident; |
4
|
|
|
|
5
|
|
|
use Doctrine\Common\EventSubscriber; |
6
|
|
|
use Doctrine\ORM\Event\OnFlushEventArgs; |
7
|
|
|
use Doctrine\ORM\Events;; |
8
|
|
|
use Doctrine\ORM\UnitOfWork; |
9
|
|
|
use TonicHealthCheck\Incident\Siren\IncidentSiren; |
10
|
|
|
use TonicHealthCheck\Incident\Siren\IncidentSirenCollection; |
11
|
|
|
use TonicHealthCheck\Incident\Siren\NotificationType\EmailNotificationType; |
12
|
|
|
use TonicHealthCheck\Incident\Siren\NotificationType\FileNotificationType; |
13
|
|
|
use TonicHealthCheck\Incident\Siren\NotificationType\NotificationTypeInterface; |
14
|
|
|
use TonicHealthCheck\Incident\Siren\NotificationType\PagerDutyNotificationType; |
15
|
|
|
use TonicHealthCheck\Incident\Siren\NotificationType\RequestNotificationType; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* Class IncidentEventSubscriber. |
19
|
|
|
*/ |
20
|
|
|
class IncidentEventSubscriber implements EventSubscriber |
21
|
|
|
{ |
22
|
|
|
protected static $typeEventPolitic = [ |
23
|
|
|
IncidentInterface::TYPE_URGENT => [ |
24
|
|
|
EmailNotificationType::class, |
25
|
|
|
FileNotificationType::class, |
26
|
|
|
RequestNotificationType::class, |
27
|
|
|
PagerDutyNotificationType::class, |
28
|
|
|
], |
29
|
|
|
IncidentInterface::TYPE_WARNING => [ |
30
|
|
|
EmailNotificationType::class, |
31
|
|
|
RequestNotificationType::class, |
32
|
|
|
], |
33
|
|
|
IncidentInterface::TYPE_MINOR => [ |
34
|
|
|
EmailNotificationType::class, |
35
|
|
|
RequestNotificationType::class, |
36
|
|
|
], |
37
|
|
|
]; |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* @var IncidentSirenCollection |
41
|
|
|
*/ |
42
|
|
|
private $incidentSirenCollection; |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* IncidentHandler constructor. |
46
|
|
|
* |
47
|
|
|
* @param IncidentSirenCollection $incidentSirenC |
48
|
|
|
*/ |
49
|
2 |
|
public function __construct($incidentSirenC) |
50
|
|
|
{ |
51
|
2 |
|
$this->setIncidentSirenCollection($incidentSirenC); |
52
|
2 |
|
} |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* @return array |
56
|
|
|
*/ |
57
|
1 |
|
public function getSubscribedEvents() |
58
|
|
|
{ |
59
|
|
|
return array( |
60
|
1 |
|
Events::onFlush, |
61
|
|
|
); |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* @param OnFlushEventArgs $eventArgs |
66
|
|
|
*/ |
67
|
1 |
|
public function onFlush(OnFlushEventArgs $eventArgs) |
68
|
|
|
{ |
69
|
1 |
|
$entityM = $eventArgs->getEntityManager(); |
70
|
1 |
|
$uow = $entityM->getUnitOfWork(); |
71
|
1 |
|
$updates = $uow->getScheduledEntityUpdates(); |
72
|
|
|
|
73
|
1 |
|
foreach ($updates as $entity) { |
74
|
|
|
/** @var IncidentInterface $entity */ |
75
|
1 |
|
if ($entity instanceof IncidentInterface) { |
76
|
1 |
|
$this->preFlushIncident($uow, $entity); |
77
|
|
|
} |
78
|
|
|
} |
79
|
1 |
|
} |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* @return IncidentSirenCollection |
83
|
|
|
*/ |
84
|
1 |
|
public function getIncidentSirenCollection() |
85
|
|
|
{ |
86
|
1 |
|
return $this->incidentSirenCollection; |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* @param IncidentSirenCollection $incidentSiren |
91
|
|
|
*/ |
92
|
2 |
|
protected function setIncidentSirenCollection(IncidentSirenCollection $incidentSiren) |
93
|
|
|
{ |
94
|
2 |
|
$this->incidentSirenCollection = $incidentSiren; |
95
|
2 |
|
} |
96
|
|
|
|
97
|
|
|
/** |
98
|
|
|
* @param IncidentInterface $entity |
99
|
|
|
*/ |
100
|
1 |
|
protected function preUpdateIncidentStatus(IncidentInterface $entity) |
101
|
|
|
{ |
102
|
|
|
/** @var IncidentSiren $incidentI */ |
103
|
1 |
|
foreach ($this->getIncidentSirenCollection() as $incidentI) { |
104
|
1 |
|
if (isset(static::$typeEventPolitic[$entity->getType()]) |
105
|
1 |
|
&& $this->checkIsNotificationAllow($entity->getType(), $incidentI->getNotificationTypeI()) |
106
|
|
|
) { |
107
|
1 |
|
$entity->attach($incidentI); |
108
|
|
|
} else { |
109
|
1 |
|
$entity->detach($incidentI); |
110
|
|
|
} |
111
|
|
|
} |
112
|
1 |
|
} |
113
|
|
|
|
114
|
|
|
/** |
115
|
|
|
* @param string $type |
116
|
|
|
* @param NotificationTypeInterface $notificationTypeI |
117
|
|
|
* |
118
|
|
|
* @return bool |
119
|
|
|
*/ |
120
|
1 |
|
protected function checkIsNotificationAllow($type, NotificationTypeInterface $notificationTypeI) |
121
|
|
|
{ |
122
|
1 |
|
$isNotificationAllow = false; |
123
|
|
|
|
124
|
1 |
|
foreach (static::$typeEventPolitic[$type] as $notificationType) { |
125
|
1 |
|
if (is_a($notificationTypeI, $notificationType)) { |
126
|
1 |
|
$isNotificationAllow = true; |
127
|
1 |
|
break; |
128
|
|
|
} |
129
|
|
|
} |
130
|
|
|
|
131
|
1 |
|
return $isNotificationAllow; |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
/** |
135
|
|
|
* @param UnitOfWork $uow |
136
|
|
|
* @param IncidentInterface $entity |
137
|
|
|
*/ |
138
|
1 |
|
protected function preFlushIncident(UnitOfWork $uow, IncidentInterface $entity) |
139
|
|
|
{ |
140
|
1 |
|
$changeSet = $uow->getEntityChangeSet($entity); |
141
|
1 |
|
if (array_key_exists('status', $changeSet) || array_key_exists('type', $changeSet)) { |
142
|
1 |
|
$this->preUpdateIncidentStatus($entity); |
143
|
1 |
|
$entity->notify(); |
144
|
|
|
} |
145
|
1 |
|
} |
146
|
|
|
} |
147
|
|
|
|
The PSR-1: Basic Coding Standard recommends that a file should either introduce new symbols, that is classes, functions, constants or similar, or have side effects. Side effects are anything that executes logic, like for example printing output, changing ini settings or writing to a file.
The idea behind this recommendation is that merely auto-loading a class should not change the state of an application. It also promotes a cleaner style of programming and makes your code less prone to errors, because the logic is not spread out all over the place.
To learn more about the PSR-1, please see the PHP-FIG site on the PSR-1.