Completed
Push — master ( ccd20f...6ee4e9 )
by dan
02:09
created

NotificationExtension::load()   C

Complexity

Conditions 11
Paths 76

Size

Total Lines 47
Code Lines 26

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 47
rs 5.2653
c 0
b 0
f 0
cc 11
eloc 26
nc 76
nop 2

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
namespace IrishDan\NotificationBundle\DependencyInjection;
4
5
use IrishDan\NotificationBundle\DependencyInjection\Factory\Broadcaster\SlackBroadcasterFactory;
6
use Symfony\Component\HttpKernel\DependencyInjection\Extension;
7
use Symfony\Component\DependencyInjection\ContainerBuilder;
8
9
class NotificationExtension extends Extension
10
{
11
    public function load(array $configs, ContainerBuilder $container)
12
    {
13
        $configuration = new Configuration();
14
        $config        = $this->processConfiguration($configuration, $configs);
15
16
        foreach ($configs as $subConfig) {
17
            $config = array_merge($config, $subConfig);
18
        }
19
20
        $channels        = ['mail_channel', 'database_channel', 'pusher_channel', 'nexmo_channel'];
21
        $enabledChannels = [];
22
23
        foreach ($channels as $channel) {
24
            // Set an enabled flag for each channel.
25
            if (!empty($config[$channel]['enabled'])) {
26
                $enabledChannels[] = $channel;
27
            }
28
            $container->setParameter('notification.' . $channel . '.enabled', !empty($config[$channel]['enabled']));
29
30
            // Set a configuration parameter for each channel also.
31
            switch ($channel) {
32
                case 'mail_channel':
33
                    if (!empty($config[$channel])) {
34
                        $configuration = $this->mailChannelConfiguration($config[$channel]);
35
                        $container->setParameter('notification.' . $channel . '.configuration', $configuration);
36
                    }
37
                    break;
38
39
                case 'database_channel':
40
                case 'pusher_channel':
41
                case 'nexmo_channel':
42
                    $configuration = empty($config[$channel]) ? [] : $config[$channel];
43
                    $container->setParameter('notification.' . $channel . '.configuration', $configuration);
44
                    break;
45
            }
46
        }
47
48
        $container->setParameter('notification.available_channels', $enabledChannels);
49
50
        // foreach ($config['channels'] as $name => $config) {
0 ignored issues
show
Unused Code Comprehensibility introduced by
64% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
51
        //     $adapters[$name] = $this->createAdapter($name, $config, $container);
0 ignored issues
show
Unused Code Comprehensibility introduced by
67% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
52
        // }
53
54
        foreach ($config['broadcasters'] as $name => $config) {
55
            $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...
56
        }
57
    }
58
59
    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...
60
    {
61
        // var_dump($name);
0 ignored issues
show
Unused Code Comprehensibility introduced by
67% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
62
        // var_dump($broadcaster);
0 ignored issues
show
Unused Code Comprehensibility introduced by
67% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
63
//
64
        // $type = array_keys($broadcaster)[0];
0 ignored issues
show
Unused Code Comprehensibility introduced by
62% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
65
        // var_dump($type);
0 ignored issues
show
Unused Code Comprehensibility introduced by
67% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
66
        // // @TODO: Use to get the factory
67
        // $factory = new SlackBroadcasterFactory();
0 ignored issues
show
Unused Code Comprehensibility introduced by
46% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
68
69
    }
70
71
    private function mailChannelConfiguration($config)
72
    {
73
        // @TODO: If the email config is not set use the parameters.
74
75
        return $config;
76
    }
77
}