Completed
Pull Request — master (#4)
by dan
14:39
created

DependencyInjection/Factory/ChannelFactory.php (4 issues)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

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
    protected $adapterConfigurationId;
16
17
    public function getChannelKeyAdapterMap()
18
    {
19
        return [
20
            'channel' => $this->channelKey,
21
            'adapter' => $this->adapterName,
22
            'config_id' => $this->adapterConfigurationId,
23
            'config' => $this->adapterConfiguration,
24
        ];
25
    }
26
27
    public function create(ContainerBuilder $container, $channel, array $config, $type = '')
28
    {
29
        if (empty($type)) {
30
            $type = $channel;
31
        }
32
33
        // Merge config with type config
34
        if ($container->hasParameter('notification.channel.' . $type . '.configuration')) {
35
            $defaultConfig = $container->getParameter('notification.channel.' . $type . '.configuration');
36
            $config = array_merge($defaultConfig, $config);
37
        }
38
39
        $adapterName = 'notification.adapter.' . $type;
40
        $adapter = new Reference($adapterName);
41
        $eventDispatcher = new Reference('event_dispatcher');
42
43
        $parameterName = 'notification.channel.' . $channel . '.configuration';
44
45
46
        if (!$container->hasParameter($parameterName)) {
47
            $container->setParameter($parameterName, $config);
48
        }
49
50
        $definition = new Definition();
51
52
        // Create a Channel or an EventChannel depending on the config.
53
54
        $definition->setClass('IrishDan\NotificationBundle\Channel\Channel');
55
        $definition->setArguments(
56
            [
57
                '%' . $parameterName . '%',
58
                $channel,
59
                $adapter,
60
            ]
61
        );
62
63
        $definition->setMethodCalls(
64
            [
65
                ['setEventDispatcher', [$eventDispatcher]],
66
            ]
67
        );
68
69
        // @TODO We need to allow for both formatting and dispatching to be offloaded else where via events
70
        // @TODO The architecture used by event channel might be better suited to this. eg
71
        // every notification goes through the direct channel
72
        //
73
74
        // $definition->addMethodCall('setFormatAsEvent', [$config['channel_type'] === 'event']);
75
        $definition->addMethodCall('setDispatchAsEvent', [$config['channel_type'] === 'event']);
76
77
        $serviceName = 'notification.channel.' . $channel;
78
        $container->setDefinition($serviceName, $definition);
79
80
        $this->adapterConfigurationId = $parameterName;
81
        $this->adapterConfiguration = $config;
82
        $this->adapterName = $adapterName;
83
        $this->channelKey = $type;
84
85
        return $serviceName;
86
    }
87
88
    public function addConfiguration(ArrayNodeDefinition $node, $type)
89
    {
90
        switch ($type) {
91 View Code Duplication
            case 'mail':
0 ignored issues
show
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...
92
                $node
93
                    ->children()
94
                    ->enumNode('channel_type')
95
                    ->values(['direct', 'event'])
96
                    ->defaultValue('direct')
97
                    ->end()
98
                    ->scalarNode('default_sender')->defaultValue('')->end()
99
                    ->arrayNode('cc')->end()
100
                    ->arrayNode('bcc')->end()
101
                    ->end();
102
                break;
103
104
            case 'database':
105
                $node
106
                    ->children()
107
                    ->enumNode('channel_type')
108
                    ->values(['direct', 'event'])
109
                    ->end()
110
                    ->scalarNode('entity')->defaultValue('App:Notification')->end()
111
                    ->end();
112
                break;
113
            case 'pusher':
114
                $node
115
                    ->children()
116
                    ->enumNode('channel_type')
117
                    ->values(['direct', 'event'])
118
                    ->defaultValue('direct')
119
                    ->end()
120
                    ->scalarNode('auth_key')->defaultValue('')->end()
121
                    ->scalarNode('secret')->defaultValue('')->end()
122
                    ->scalarNode('app_id')->defaultValue('')->end()
123
                    ->scalarNode('cluster')->defaultValue('')->end()
124
                    ->booleanNode('encrypted')->defaultTrue()->end()
125
                    ->scalarNode('channel_name')->defaultValue('')->end()
126
                    ->scalarNode('event')->defaultValue('')->end()
127
                    ->end();
128
                break;
129 View Code Duplication
            case 'nexmo':
0 ignored issues
show
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...
130
                $node
131
                    ->children()
132
                    ->enumNode('channel_type')
133
                    ->values(['direct', 'event'])
134
                    ->defaultValue('direct')
135
                    ->end()
136
                    ->scalarNode('api_key')->defaultValue('')->end()
137
                    ->scalarNode('api_secret')->defaultValue('')->end()
138
                    ->scalarNode('from')->defaultValue('')->end()
139
                    ->end();
140
                break;
141 View Code Duplication
            case 'slack':
0 ignored issues
show
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...
142
                $node
143
                    ->children()
144
                    ->enumNode('channel_type')
145
                    ->values(['direct', 'event'])
146
                    ->defaultValue('direct')
147
                    ->end()
148
                    ->scalarNode('webhook')->defaultNull()->end()
149
                    ->end();
150
                break;
151 View Code Duplication
            case 'logger':
0 ignored issues
show
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...
152
                $node
153
                    ->children()
154
                    ->enumNode('channel_type')
155
                    ->values(['direct', 'event'])
156
                    ->defaultValue('direct')
157
                    ->end()
158
                    ->scalarNode('severity')->defaultValue('info')->end()
159
                    ->end();
160
                break;
161
            default:
162
                // Should allow any config
163
                // @TODO: The type key to define the allowed configuration
164
                break;
165
        }
166
    }
167
}
168