1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of the Doctrine Bundle |
5
|
|
|
* |
6
|
|
|
* (c) Fabien Potencier <[email protected]> |
7
|
|
|
* (c) Doctrine Project, Benjamin Eberlei <[email protected]> |
8
|
|
|
* |
9
|
|
|
* For the full copyright and license information, please view the LICENSE |
10
|
|
|
* file that was distributed with this source code. |
11
|
|
|
*/ |
12
|
|
|
|
13
|
|
|
namespace IncidentBundle\DependencyInjection\Compiler; |
14
|
|
|
|
15
|
|
|
use IncidentBundle\Provider\SubjectsConfigProvider; |
16
|
|
|
use Symfony\Component\DependencyInjection\ContainerBuilder; |
17
|
|
|
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface; |
18
|
|
|
use Symfony\Component\DependencyInjection\DefinitionDecorator; |
19
|
|
|
use Symfony\Component\DependencyInjection\Reference; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* Class for NotificationsSubjectsPass |
23
|
|
|
*/ |
24
|
|
|
class NotificationsSubjectsPass implements CompilerPassInterface |
25
|
|
|
{ |
26
|
|
|
const SUBJECTS_ID = 'incident.siren.notification.subjects.%s.%s'; |
27
|
|
|
const SCHEDULE_ID = 'incident.siren.notification.schedule.%s.%s'; |
28
|
|
|
const INCIDENT_SCHEDULE_PROTOTYPE = 'incident.schedule_prototype'; |
29
|
|
|
const INCIDENT_SUBJECT_PROTOTYPE = 'incident.subject_prototype'; |
30
|
|
|
const INCIDENT_NOTIFICATIONS_SUBJECTS = 'incident.notifications.subjects'; |
31
|
|
|
const INCIDENT_SIREN_NOTIFICATION_SUBJECTS = 'incident.siren.notification.subjects'; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* @var ContainerBuilder |
35
|
|
|
*/ |
36
|
|
|
private $container; |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* @var SubjectsConfigProvider |
40
|
|
|
*/ |
41
|
|
|
private $subjectsProvider; |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* {@inheritdoc} |
45
|
|
|
*/ |
46
|
|
|
public function process(ContainerBuilder $container) |
47
|
|
|
{ |
48
|
|
|
$this->setContainer($container); |
49
|
|
|
|
50
|
|
|
if (!$this->getContainer()->hasParameter(self::INCIDENT_NOTIFICATIONS_SUBJECTS)) { |
51
|
|
|
return; |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
$subjectsConfig = $this->getContainer()->getParameter( |
55
|
|
|
self::INCIDENT_NOTIFICATIONS_SUBJECTS |
56
|
|
|
); |
57
|
|
|
|
58
|
|
|
$this->setSubjectsProvider(new SubjectsConfigProvider($subjectsConfig)); |
59
|
|
|
|
60
|
|
|
$taggedServices = $this->getContainer()->findTaggedServiceIds( |
61
|
|
|
self::INCIDENT_SIREN_NOTIFICATION_SUBJECTS |
62
|
|
|
); |
63
|
|
|
|
64
|
|
|
$this->processNotificationsTypes($taggedServices); |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* @param $taggedServices |
69
|
|
|
*/ |
70
|
|
|
protected function processNotificationsTypes($taggedServices) |
71
|
|
|
{ |
72
|
|
|
foreach ($taggedServices as $notSubjectsId => $tags) { |
73
|
|
|
if (!isset($tags[0]['type_name'])) { |
74
|
|
|
continue; |
75
|
|
|
} |
76
|
|
|
$typeName = $tags[0]['type_name']; |
77
|
|
|
|
78
|
|
|
$subjects = $this->getSubjectsProvider()->getByType($typeName); |
79
|
|
|
$this->processSubjects($subjects, $typeName, $notSubjectsId); |
80
|
|
|
} |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* @param array $subjects |
85
|
|
|
* @param string $typeName |
86
|
|
|
* @param string $notSubjectsId |
87
|
|
|
*/ |
88
|
|
|
protected function processSubjects($subjects, $typeName, $notSubjectsId) |
89
|
|
|
{ |
90
|
|
|
foreach ($subjects as $subjectsId => $subjectsItem) { |
91
|
|
|
$subjectDef = $this->createSubjectDef($typeName, $subjectsItem, $subjectsId); |
92
|
|
|
$subjectId = sprintf(self::SUBJECTS_ID, $typeName, $subjectsId); |
93
|
|
|
$this->getContainer()->setDefinition($subjectId, $subjectDef); |
94
|
|
|
$notSubjectsDef = $this->getContainer()->getDefinition($notSubjectsId); |
95
|
|
|
|
96
|
|
|
$notSubjectsDef->addMethodCall( |
97
|
|
|
'add', |
98
|
|
|
[new Reference($subjectId)] |
99
|
|
|
); |
100
|
|
|
} |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
/** |
104
|
|
|
* @param string $schedule |
105
|
|
|
* |
106
|
|
|
* @return DefinitionDecorator |
107
|
|
|
*/ |
108
|
|
|
protected function createScheduleDef($schedule) |
109
|
|
|
{ |
110
|
|
|
$scheduleDef = new DefinitionDecorator(self::INCIDENT_SCHEDULE_PROTOTYPE); |
111
|
|
|
$scheduleDef->replaceArgument(0, $schedule); |
112
|
|
|
|
113
|
|
|
return $scheduleDef; |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
/** |
117
|
|
|
* @param string $typeName |
118
|
|
|
* @param array $subjectsItem |
119
|
|
|
* @param string $subjectsId |
120
|
|
|
* |
121
|
|
|
* @return DefinitionDecorator |
122
|
|
|
*/ |
123
|
|
|
protected function createSubjectDef($typeName, $subjectsItem, $subjectsId) |
124
|
|
|
{ |
125
|
|
|
$subjectDef = new DefinitionDecorator(self::INCIDENT_SUBJECT_PROTOTYPE); |
126
|
|
|
if (isset($subjectsItem['target'])) { |
127
|
|
|
$subjectDef->replaceArgument(0, $subjectsItem['target']); |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
if (isset($subjectsItem['schedule'])) { |
131
|
|
|
$scheduleDef = $this->createScheduleDef($subjectsItem['schedule']); |
132
|
|
|
$scheduleId = sprintf(self::SCHEDULE_ID, $typeName, $subjectsId); |
133
|
|
|
$this->getContainer()->setDefinition($scheduleId, $scheduleDef); |
134
|
|
|
$subjectDef->replaceArgument(1, new Reference($scheduleId)); |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
return $subjectDef; |
138
|
|
|
} |
139
|
|
|
|
140
|
|
|
/** |
141
|
|
|
* @return ContainerBuilder |
142
|
|
|
*/ |
143
|
|
|
protected function getContainer() |
144
|
|
|
{ |
145
|
|
|
return $this->container; |
146
|
|
|
} |
147
|
|
|
|
148
|
|
|
/** |
149
|
|
|
* @return mixed |
150
|
|
|
*/ |
151
|
|
|
protected function getSubjectsProvider() |
152
|
|
|
{ |
153
|
|
|
return $this->subjectsProvider; |
154
|
|
|
} |
155
|
|
|
|
156
|
|
|
/** |
157
|
|
|
* @param ContainerBuilder $container |
158
|
|
|
*/ |
159
|
|
|
private function setContainer(ContainerBuilder $container) |
160
|
|
|
{ |
161
|
|
|
$this->container = $container; |
162
|
|
|
} |
163
|
|
|
|
164
|
|
|
/** |
165
|
|
|
* @param mixed $subjectsProvider |
166
|
|
|
*/ |
167
|
|
|
private function setSubjectsProvider($subjectsProvider) |
168
|
|
|
{ |
169
|
|
|
$this->subjectsProvider = $subjectsProvider; |
170
|
|
|
} |
171
|
|
|
} |
172
|
|
|
|