Completed
Push — master ( 6ee4e9...166b13 )
by dan
02:00
created

createChannelManagerService()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 20
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 20
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 11
nc 2
nop 2
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':
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

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.

Loading history...
30
                    $configuration = empty($config[$channel]) ? [] : $config[$channel];
31
                    $container->setParameter('notification.channel.' . $channel . '.configuration', $configuration);
32
33
                    break;
34
35 View Code Duplication
                default:
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

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.

Loading history...
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);
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...
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)
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...
99
    {
100
        //
101
    }
102
103
    private function mailChannelConfiguration($config)
0 ignored issues
show
Unused Code introduced by
This method is not used, and could be removed.
Loading history...
104
    {
105
        // @TODO: If the email config is not set use the parameters.
106
        return $config;
107
    }
108
}