1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace TonicHealthCheck\Incident; |
4
|
|
|
|
5
|
|
|
use Doctrine\Common\EventSubscriber; |
6
|
|
|
use Doctrine\Common\Persistence\Event\LifecycleEventArgs; |
7
|
|
|
use Doctrine\ORM\Event\PreFlushEventArgs; |
8
|
|
|
use Doctrine\ORM\Event\PreUpdateEventArgs; |
9
|
|
|
use Doctrine\ORM\Events; |
10
|
|
|
|
11
|
|
|
use TonicHealthCheck\Incident\Siren\IncidentSiren; |
12
|
|
|
use TonicHealthCheck\Incident\Siren\IncidentSirenCollection; |
13
|
|
|
use TonicHealthCheck\Incident\Siren\NotificationType\EmailNotificationType; |
14
|
|
|
use TonicHealthCheck\Incident\Siren\NotificationType\FileNotificationType; |
15
|
|
|
use TonicHealthCheck\Incident\Siren\NotificationType\RequestNotificationType; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* Class IncidentEventSubscriber |
19
|
|
|
* @package TonicHealthCheck\Incident |
20
|
|
|
*/ |
21
|
|
|
class IncidentEventSubscriber implements EventSubscriber |
22
|
|
|
{ |
23
|
|
|
protected static $typeEventPolitic =[ |
24
|
|
|
IncidentInterface::TYPE_URGENT => [ |
25
|
|
|
EmailNotificationType::class, |
26
|
|
|
FileNotificationType::class, |
27
|
|
|
RequestNotificationType::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
|
|
|
* @param IncidentSirenCollection $incidentSirenC |
47
|
|
|
*/ |
48
|
|
|
public function __construct($incidentSirenC) |
49
|
|
|
{ |
50
|
|
|
$this->setIncidentSirenCollection($incidentSirenC); |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* @return array |
55
|
|
|
*/ |
56
|
|
|
public function getSubscribedEvents() |
57
|
|
|
{ |
58
|
|
|
return array( |
59
|
|
|
Events::preUpdate, |
60
|
|
|
Events::prePersist, |
61
|
|
|
); |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* @param PreUpdateEventArgs $args |
66
|
|
|
*/ |
67
|
|
View Code Duplication |
public function preUpdate(PreUpdateEventArgs $args) |
|
|
|
|
68
|
|
|
{ |
69
|
|
|
|
70
|
|
|
$entity = $args->getObject(); |
71
|
|
|
|
72
|
|
|
if ($entity instanceof IncidentInterface && $args->hasChangedField('status')) { |
73
|
|
|
$this->preUpdateIncidentStatus($entity); |
74
|
|
|
$entity->notify(); |
75
|
|
|
} |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* @param LifecycleEventArgs $args |
80
|
|
|
*/ |
81
|
|
View Code Duplication |
public function prePersist(LifecycleEventArgs $args) |
|
|
|
|
82
|
|
|
{ |
83
|
|
|
$entity = $args->getObject(); |
84
|
|
|
if ($entity instanceof IncidentInterface && $entity->getId() === null) { |
85
|
|
|
$this->preUpdateIncidentStatus($entity); |
86
|
|
|
$entity->notify(); |
87
|
|
|
} |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* @return IncidentSirenCollection |
92
|
|
|
*/ |
93
|
|
|
public function getIncidentSirenCollection() |
94
|
|
|
{ |
95
|
|
|
return $this->incidentSirenCollection; |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* @param IncidentSirenCollection $incidentSiren |
100
|
|
|
*/ |
101
|
|
|
protected function setIncidentSirenCollection(IncidentSirenCollection $incidentSiren) |
102
|
|
|
{ |
103
|
|
|
$this->incidentSirenCollection = $incidentSiren; |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
/** |
107
|
|
|
* @param IncidentInterface $entity |
108
|
|
|
*/ |
109
|
|
|
protected function preUpdateIncidentStatus(IncidentInterface $entity) |
110
|
|
|
{ |
111
|
|
|
/** @var IncidentSiren $incidentI */ |
112
|
|
|
foreach ($this->getIncidentSirenCollection() as $incidentI) { |
113
|
|
|
if (isset(static::$typeEventPolitic[$entity->getType()]) |
114
|
|
|
&& $this->checkIsNotificationAllow($entity->getType(), $incidentI->getNotificationTypeI()) |
115
|
|
|
) { |
116
|
|
|
$entity->attach($incidentI); |
117
|
|
|
} |
118
|
|
|
} |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
/** |
122
|
|
|
* @param $type |
123
|
|
|
* @param $notificationTypeI |
124
|
|
|
* @return bool |
125
|
|
|
*/ |
126
|
|
|
protected function checkIsNotificationAllow($type, $notificationTypeI) |
127
|
|
|
{ |
128
|
|
|
$isNotificationAllow = false; |
129
|
|
|
|
130
|
|
|
foreach (static::$typeEventPolitic[$type] as $notificationType) { |
131
|
|
|
if (is_a($notificationTypeI, $notificationType)) { |
132
|
|
|
$isNotificationAllow = true; |
133
|
|
|
break; |
134
|
|
|
} |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
return $isNotificationAllow; |
138
|
|
|
} |
139
|
|
|
} |
140
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.