ChannelFactory::addConfiguration()   B
last analyzed

Complexity

Conditions 7
Paths 7

Size

Total Lines 56

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 56
rs 8.0266
c 0
b 0
f 0
cc 7
nc 7
nop 2

How to fix   Long Method   

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\Factory;
4
5
use Symfony\Component\Config\Definition\Builder\ArrayNodeDefinition;
6
use Symfony\Component\DependencyInjection\ContainerBuilder;
7
use Symfony\Component\DependencyInjection\Definition;
8
use Symfony\Component\DependencyInjection\Reference;
9
10
class ChannelFactory
11
{
12
    protected $channelKey;
13
    protected $adapterName;
14
    protected $adapterConfiguration;
15
16
    public function getChannelKeyAdapterMap()
17
    {
18
        return [
19
            'channel' => $this->channelKey,
20
            'adapter' => $this->adapterName,
21
            'config' => $this->adapterConfiguration,
22
        ];
23
    }
24
25
    public function create(ContainerBuilder $container, $channel, array $config, $type = '')
26
    {
27
        if (empty($type)) {
28
            $type = $channel;
29
        }
30
31
        // Merge config with type config
32
        if ($container->hasParameter('notification.channel.' . $type . '.configuration')) {
33
            $defaultConfig = $container->getParameter('notification.channel.' . $type . '.configuration');
34
            $config = array_merge($defaultConfig, $config);
35
        }
36
37
        $adapterName = 'notification.adapter.' . $type;
38
        $adapter = new Reference($adapterName);
39
        $eventDispatcher = new Reference('event_dispatcher');
40
41
        $parameterName = 'notification.channel.' . $channel . '.configuration';
42
        if (!$container->hasParameter($parameterName)) {
43
            $container->setParameter($parameterName, $config);
44
        }
45
46
        $definition = new Definition();
47
        $definition->setClass('IrishDan\NotificationBundle\Channel\DirectChannel');
48
        $definition->setArguments(
49
            [
50
                '%' . $parameterName . '%',
51
                $channel,
52
                $adapter,
53
            ]
54
        );
55
56
        $definition->setMethodCalls(
57
            [
58
                ['setEventDispatcher', [$eventDispatcher]],
59
            ]
60
        );
61
62
        $definition->addMethodCall('setDispatchToEvent', [empty($config['direct_dispatch'])]);
63
64
        $serviceName = 'notification.channel.' . $channel;
65
        $container->setDefinition($serviceName, $definition);
66
67
        $this->adapterConfiguration = $parameterName;
68
        $this->adapterName = $adapterName;
69
        $this->channelKey = $type;
70
71
        return $serviceName;
72
    }
73
74
    public function addConfiguration(ArrayNodeDefinition $node, $type)
75
    {
76
        switch ($type) {
77
            case 'mail':
78
                $node
79
                    ->children()
80
                    ->scalarNode('default_sender')->defaultValue('')->end()
81
                    ->arrayNode('cc')->end()
82
                    ->arrayNode('bcc')->end()
83
                    ->end();
84
                break;
85
86
            case 'database':
87
                $node
88
                    ->children()
89
                    ->scalarNode('entity')->defaultValue('AppBundle:Notification')->end()
90
                    ->end();
91
                break;
92
            case 'pusher':
93
                $node
94
                    ->children()
95
                    ->scalarNode('auth_key')->defaultValue('')->end()
96
                    ->scalarNode('secret')->defaultValue('')->end()
97
                    ->scalarNode('app_id')->defaultValue('')->end()
98
                    ->scalarNode('cluster')->defaultValue('')->end()
99
                    ->booleanNode('encrypted')->defaultTrue()->end()
100
                    ->scalarNode('channel_name')->defaultValue('')->end()
101
                    ->scalarNode('event')->defaultValue('')->end()
102
                    ->end();
103
                break;
104
            case 'nexmo':
105
                $node
106
                    ->children()
107
                    ->scalarNode('api_key')->defaultValue('')->end()
108
                    ->scalarNode('api_secret')->defaultValue('')->end()
109
                    ->scalarNode('from')->defaultValue('')->end()
110
                    ->end();
111
                break;
112
            case 'slack':
113
                $node
114
                    ->children()
115
                    ->scalarNode('webhook')->defaultNull()->end()
116
                    ->end();
117
                break;
118
            case 'logger':
119
                $node
120
                    ->children()
121
                    ->scalarNode('severity')->defaultValue('info')->end()
122
                    ->end();
123
                break;
124
            default:
125
                // Should allow any config
126
                // @TODO: The type key to define the allowed configuration
127
                break;
128
        }
129
    }
130
}
131