1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
/* |
6
|
|
|
* This file is part of the Explicit Architecture POC, |
7
|
|
|
* which is created on top of the Symfony Demo application. |
8
|
|
|
* |
9
|
|
|
* (c) Herberto Graça <[email protected]> |
10
|
|
|
* |
11
|
|
|
* For the full copyright and license information, please view the LICENSE |
12
|
|
|
* file that was distributed with this source code. |
13
|
|
|
*/ |
14
|
|
|
|
15
|
|
|
namespace Acme\App\Infrastructure\Notification\Config; |
16
|
|
|
|
17
|
|
|
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface; |
18
|
|
|
use Symfony\Component\DependencyInjection\ContainerBuilder; |
19
|
|
|
use Symfony\Component\DependencyInjection\Reference; |
20
|
|
|
|
21
|
|
|
final class NotificationConfigCompilerPass implements CompilerPassInterface |
22
|
|
|
{ |
23
|
|
|
public const KEY_NOTIFICATION = 'notification'; |
24
|
|
|
public const KEY_GENERATOR_METHOD_NAME = 'method'; |
25
|
|
|
public const KEY_VOTER = 'voterId'; |
26
|
|
|
public const KEY_VOTER_METHOD_NAME = 'voterMethod'; |
27
|
|
|
public const NOTIFICATION_STRATEGY_TAG = 'notification_strategy'; |
28
|
|
|
|
29
|
|
|
public function process(ContainerBuilder $containerBuilder): void |
30
|
|
|
{ |
31
|
|
|
$notificationStrategyList = $containerBuilder->findTaggedServiceIds(self::NOTIFICATION_STRATEGY_TAG); |
32
|
|
|
|
33
|
|
|
foreach ($notificationStrategyList as $serviceId => $tags) { |
34
|
|
|
$this->processNotificationStrategy($containerBuilder, $serviceId); |
35
|
|
|
} |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
private function processNotificationStrategy(ContainerBuilder $containerBuilder, string $strategyServiceId): void |
39
|
|
|
{ |
40
|
|
|
if (!$containerBuilder->has($strategyServiceId)) { |
41
|
|
|
return; |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
$definition = $containerBuilder->findDefinition($strategyServiceId); |
45
|
|
|
|
46
|
|
|
$taggedServices = $containerBuilder->findTaggedServiceIds($strategyServiceId); |
47
|
|
|
|
48
|
|
|
foreach ($taggedServices as $serviceId => $tags) { |
49
|
|
|
foreach ($tags as $attributes) { |
50
|
|
|
if (!$containerBuilder->has($serviceId)) { |
51
|
|
|
throw new NotificationConfigException( |
52
|
|
|
"The configured notification generator '$serviceId' does not exist in the container" |
53
|
|
|
); |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
$methodName = $attributes[self:: KEY_GENERATOR_METHOD_NAME]; |
57
|
|
|
if (!method_exists($containerBuilder->getDefinition($serviceId)->getClass(), $methodName)) { |
58
|
|
|
throw new NotificationConfigException( |
59
|
|
|
"The configured notification generator '$serviceId'" |
60
|
|
|
. " does not have the configured method '$methodName'" |
61
|
|
|
); |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
$definition->addMethodCall( |
65
|
|
|
'addNotificationMessageGenerator', |
66
|
|
|
[ |
67
|
|
|
new Reference($serviceId), |
68
|
|
|
$attributes[self::KEY_NOTIFICATION], |
69
|
|
|
$methodName, |
70
|
|
|
array_key_exists(self::KEY_VOTER, $attributes) |
71
|
|
|
? new Reference($attributes[self::KEY_VOTER]) |
72
|
|
|
: null, |
73
|
|
|
$attributes[self::KEY_VOTER_METHOD_NAME] ?? null, |
74
|
|
|
] |
75
|
|
|
); |
76
|
|
|
} |
77
|
|
|
} |
78
|
|
|
} |
79
|
|
|
} |
80
|
|
|
|