Completed
Push — master ( cd8958...94944b )
by dan
02:07
created

NotificationExtension::createBroadcaster()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
eloc 1
nc 1
nop 3
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
            $configuration = empty($channelConfig) ? [] : $channelConfig;
29
            $parameterName = 'notification.channel.' . $channel . '.configuration';
30
            $container->setParameter($parameterName, $configuration);
31
32
            // Create a service for this channel.
33
            $this->createChannelService($channel, $container);
34
        }
35
36
37
        // Create the channel service
38
        $this->createChannelManagerService($enabledChannels, $container);
39
40
        $container->setParameter('notification.available_channels', $enabledChannels);
41
42
        foreach ($config['broadcasters'] as $name => $config) {
43
            $adapters[$name] = $this->createBroadcaster($name, $config, $container);
0 ignored issues
show
Coding Style Comprehensibility introduced by
$adapters was never initialized. Although not strictly required by PHP, it is generally a good practice to add $adapters = array(); before regardless.

Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.

Let’s take a look at an example:

foreach ($collection as $item) {
    $myArray['foo'] = $item->getFoo();

    if ($item->hasBar()) {
        $myArray['bar'] = $item->getBar();
    }

    // do something with $myArray
}

As you can see in this example, the array $myArray is initialized the first time when the foreach loop is entered. You can also see that the value of the bar key is only written conditionally; thus, its value might result from a previous iteration.

This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.

Loading history...
Bug introduced by
Are you sure the assignment to $adapters[$name] is correct as $this->createBroadcaster...e, $config, $container) (which targets IrishDan\NotificationBun...on::createBroadcaster()) seems to always return null.

This check looks for function or method calls that always return null and whose return value is assigned to a variable.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
$object = $a->getObject();

The method getObject() can return nothing but null, so it makes no sense to assign that value to a variable.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
44
        }
45
    }
46
47
    private function createChannelService($channel, ContainerBuilder $container)
48
    {
49
        $definition = new Definition();
50
        $definition->setClass('IrishDan\NotificationBundle\Channel\DefaultChannel');
51
        $definition->setArguments(
52
            [
53
                '%notification.channel.' . $channel . '.enabled%',
54
                '%notification.channel.' . $channel . '.configuration%',
55
            ]
56
        );
57
        $formatter  = new Reference('notification.' . $channel . '_data_formatter');
58
        $dispatcher = new Reference('notification.' . $channel . '_message_dispatcher');
59
        $definition->setMethodCalls(
60
            [
61
                ['setDataFormatter', [$formatter]],
62
                ['setDispatcher', [$dispatcher]],
63
            ]
64
        );
65
        $container->setDefinition('notification.channel.' . $channel, $definition);
66
    }
67
68
    private function createChannelManagerService(array $enabledChannels, ContainerBuilder $container)
69
    {
70
        $definition = new Definition();
71
        $definition->setClass('IrishDan\NotificationBundle\ChannelManager');
72
        $definition->setArguments(
73
            [
74
                new Reference('event_dispatcher'),
75
                $enabledChannels, // @TODO: can we reference a parameter
76
            ]
77
        );
78
        $container->setDefinition('notification.channel_manager', $definition);
79
80
81
        foreach ($enabledChannels as $channel) {
82
            // Add the channel to the channel manager service
83
            $channelManager = $container->getDefinition('notification.channel_manager');
84
            $channelId      = 'notification.channel.' . $channel;
85
            $channelManager->addMethodCall('setChannel', [$channel, new Reference($channelId)]);
86
        }
87
    }
88
89
    private function createBroadcaster($name, $broadcaster, $container)
0 ignored issues
show
Unused Code introduced by
The parameter $name is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $broadcaster is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $container is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
90
    {
91
        // @TODO:
92
    }
93
}