Completed
Push — master ( 398a9b...97130c )
by dan
02:04
created

BroadcasterFactory::create()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 27
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 27
rs 8.8571
c 0
b 0
f 0
cc 1
eloc 15
nc 1
nop 3
1
<?php
2
3
namespace IrishDan\NotificationBundle\DependencyInjection\Factory;
4
5
use Symfony\Component\DependencyInjection\ContainerBuilder;
6
use Symfony\Component\DependencyInjection\Definition;
7
use Symfony\Component\DependencyInjection\Reference;
8
9
class BroadcasterFactory
10
{
11
    public function create(ContainerBuilder $container, $name, array $config)
12
    {
13
        $type = array_keys($config)[0];
14
        $parameterName = 'notification.broadcast.config.' . $name;
15
        $serviceName = 'notification.broadcast.' . $name;
16
        $channelServiceName = 'notification.channel.' . $type;
17
18
        // Create the configuration as a parameter.
19
        $container->setParameter($parameterName, $config[$type]);
20
21
        // Createe the broadcast service
22
        $definition = new Definition();
23
        $definition->setClass('IrishDan\NotificationBundle\Broadcast\Broadcaster');
24
        $definition->setArguments(
25
            [
26
                '@notification.broadcast.notifiable',
27
                '@' . $channelServiceName,
28
                '%' . $parameterName . '%',
29
            ]
30
        );
31
32
        $container->setDefinition($serviceName, $definition);
33
34
        // Add the broadcast to the notification manager.
35
        $notificationManager = $container->getDefinition('notification.manager');
36
        $notificationManager->addMethodCall('setBroadcaster', [$name, new Reference($serviceName)]);
37
    }
38
}
39