1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace IrishDan\NotificationBundle\DependencyInjection; |
4
|
|
|
|
5
|
|
|
use IrishDan\NotificationBundle\DependencyInjection\Factory\Broadcaster\SlackBroadcasterFactory; |
6
|
|
|
use Symfony\Component\DependencyInjection\Definition; |
7
|
|
|
use Symfony\Component\DependencyInjection\Reference; |
8
|
|
|
use Symfony\Component\HttpKernel\DependencyInjection\Extension; |
9
|
|
|
use Symfony\Component\DependencyInjection\ContainerBuilder; |
10
|
|
|
|
11
|
|
|
class NotificationExtension extends Extension |
12
|
|
|
{ |
13
|
|
|
public function load(array $configs, ContainerBuilder $container) |
14
|
|
|
{ |
15
|
|
|
$configuration = new Configuration(); |
16
|
|
|
$config = $this->processConfiguration($configuration, $configs); |
17
|
|
|
|
18
|
|
|
foreach ($configs as $subConfig) { |
19
|
|
|
$config = array_merge($config, $subConfig); |
20
|
|
|
} |
21
|
|
|
|
22
|
|
|
$enabledChannels = []; |
23
|
|
|
foreach ($config['channels'] as $channel => $channelConfig) { |
24
|
|
|
$enabledChannels[] = $channel; |
25
|
|
|
$container->setParameter('notification.channel.' . $channel . '.enabled', true); |
26
|
|
|
|
27
|
|
|
// Set a configuration parameter for each channel also. |
28
|
|
|
switch ($channel) { |
29
|
|
View Code Duplication |
case 'mail': |
|
|
|
|
30
|
|
|
$configuration = empty($config[$channel]) ? [] : $config[$channel]; |
31
|
|
|
$container->setParameter('notification.channel.' . $channel . '.configuration', $configuration); |
32
|
|
|
|
33
|
|
|
break; |
34
|
|
|
|
35
|
|
View Code Duplication |
default: |
|
|
|
|
36
|
|
|
$configuration = empty($config[$channel]) ? [] : $config[$channel]; |
37
|
|
|
$container->setParameter('notification.channel.' . $channel . '.configuration', $configuration); |
38
|
|
|
break; |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
// Create a sevice for this channel. |
42
|
|
|
$this->createChannelService($channel, $container); |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
|
46
|
|
|
// Create the channel service |
47
|
|
|
$this->createChannelManagerService($enabledChannels, $container); |
48
|
|
|
|
49
|
|
|
$container->setParameter('notification.available_channels', $enabledChannels); |
50
|
|
|
|
51
|
|
|
foreach ($config['broadcasters'] as $name => $config) { |
52
|
|
|
$adapters[$name] = $this->createBroadcaster($name, $config, $container); |
|
|
|
|
53
|
|
|
} |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
private function createChannelService($channel, ContainerBuilder $container) |
57
|
|
|
{ |
58
|
|
|
$definition = new Definition(); |
59
|
|
|
$definition->setClass('IrishDan\NotificationBundle\Channel\DefaultChannel'); |
60
|
|
|
$definition->setArguments( |
61
|
|
|
[ |
62
|
|
|
'%notification.channel.' . $channel . '.enabled%', |
63
|
|
|
'%notification.channel.' . $channel . '.configuration%', |
64
|
|
|
] |
65
|
|
|
); |
66
|
|
|
$formatter = new Reference('notification.' . $channel . '_data_formatter'); |
67
|
|
|
$dispatcher = new Reference('notification.' . $channel . '_message_dispatcher'); |
68
|
|
|
$definition->setMethodCalls( |
69
|
|
|
[ |
70
|
|
|
['setDataFormatter', [$formatter]], |
71
|
|
|
['setDispatcher', [$dispatcher]], |
72
|
|
|
] |
73
|
|
|
); |
74
|
|
|
$container->setDefinition('notification.channel.' . $channel, $definition); |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
private function createChannelManagerService(array $enabledChannels, ContainerBuilder $container) |
78
|
|
|
{ |
79
|
|
|
$definition = new Definition(); |
80
|
|
|
$definition->setClass('IrishDan\NotificationBundle\ChannelManager'); |
81
|
|
|
$definition->setArguments( |
82
|
|
|
[ |
83
|
|
|
new Reference('event_dispatcher'), |
84
|
|
|
$enabledChannels, // @TODO: can we reference a parameter |
85
|
|
|
] |
86
|
|
|
); |
87
|
|
|
$container->setDefinition('notification.channel_manager', $definition); |
88
|
|
|
|
89
|
|
|
|
90
|
|
|
foreach ($enabledChannels as $channel) { |
91
|
|
|
// Add the channel to the channel manager service |
92
|
|
|
$channelManager = $container->getDefinition('notification.channel_manager'); |
93
|
|
|
$channelId = 'notification.channel.' . $channel; |
94
|
|
|
$channelManager->addMethodCall('setChannel', [$channel, new Reference($channelId)]); |
95
|
|
|
} |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
private function createBroadcaster($name, $broadcaster, $container) |
|
|
|
|
99
|
|
|
{ |
100
|
|
|
// |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
private function mailChannelConfiguration($config) |
|
|
|
|
104
|
|
|
{ |
105
|
|
|
// @TODO: If the email config is not set use the parameters. |
106
|
|
|
return $config; |
107
|
|
|
} |
108
|
|
|
} |
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.