Completed
Pull Request — master (#27)
by Matthieu
02:05
created

YokaiMessengerExtension::load()   F

Complexity

Conditions 11
Paths 288

Size

Total Lines 46
Code Lines 28

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 27
CRAP Score 11.0397

Importance

Changes 0
Metric Value
dl 0
loc 46
ccs 27
cts 29
cp 0.931
rs 3.8181
c 0
b 0
f 0
cc 11
eloc 28
nc 288
nop 2
crap 11.0397

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 Yokai\MessengerBundle\DependencyInjection;
4
5
use Symfony\Component\Config\FileLocator;
6
use Symfony\Component\DependencyInjection\ContainerBuilder;
7
use Symfony\Component\DependencyInjection\Loader\XmlFileLoader;
8
use Symfony\Component\HttpKernel\DependencyInjection\Extension;
9
use Yokai\MessengerBundle\DependencyInjection\Factory\MessageDefinitionFactory;
10
11
/**
12
 * @author Yann Eugoné <[email protected]>
13
 */
14
class YokaiMessengerExtension extends Extension
15
{
16
    /**
17
     * @var array
18
     */
19
    private $enabledChannelMap = [
20
        'swiftmailer' => [
21
            'class' => 'Symfony\Bundle\SwiftmailerBundle\SwiftmailerBundle',
22
            'bundle' => true,
23
        ],
24
        'twilio' => [
25
            'class' => 'Twilio\Rest\Api',
26
            'bundle' => false,
27
        ],
28
        'doctrine' => [
29
            'class' => 'Doctrine\Bundle\DoctrineBundle\DoctrineBundle',
30
            'bundle' => true,
31
        ],
32
        'mobile' => [
33
            'class' => 'Sly\NotificationPusher\NotificationPusher',
34
            'bundle' => false,
35
        ],
36
    ];
37
38
    /**
39
     * @inheritdoc
40
     */
41 14
    public function load(array $configs, ContainerBuilder $container)
42
    {
43 14
        $config = $this->processConfiguration($this->getConfiguration($configs, $container), $configs);
0 ignored issues
show
Documentation introduced by
$this->getConfiguration($configs, $container) is of type object|null, but the function expects a object<Symfony\Component...ConfigurationInterface>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
44
45 14
        $container->setParameter('yokai_messenger.content_builder_defaults', $config['content_builder']);
46 14
        $container->setParameter('yokai_messenger.logging_channel', $config['logging_channel']);
47
48 14
        $loader = new XmlFileLoader($container, new FileLocator(__DIR__ . '/../Resources/config'));
49 14
        $loader->load('services.xml');
50
51 14
        $bundles = $container->getParameter('kernel.bundles');
52 14
        $enabledChannels = [];
53 14
        foreach ($this->enabledChannelMap as $channel => $enableConfig) {
54 14
            $enabled = true;
55 14
            if (!$config['channels'][$channel]['enabled']) {
56 11
                $enabled = false;
57
            }
58 14
            if (!class_exists($enableConfig['class'])) {
59
                $enabled = false;
60
            }
61 14
            if ($enableConfig['bundle'] && !in_array($enableConfig['class'], $bundles)) {
62
                $enabled = false;
63
            }
64
65 14
            $enabledChannels[$channel] = $enabled;
66
        }
67
68 14
        foreach ($enabledChannels as $channel => $enabled) {
69 14
            $container->setParameter('yokai_messenger.'.$channel.'_enabled', $enabled);
70
        }
71
72 14
        if ($enabledChannels['swiftmailer']) {
73 4
            $this->registerSwiftmailer($config['channels']['swiftmailer'], $container, $loader);
74
        }
75 14
        if ($enabledChannels['twilio']) {
76 4
            $this->registerTwilio($config['channels']['twilio'], $container, $loader);
77
        }
78 14
        if ($enabledChannels['doctrine']) {
79 4
            $this->registerDoctrine($config['channels']['doctrine'], $container, $loader);
80
        }
81 14
        if ($enabledChannels['mobile']) {
82 4
            $this->registerMobile($config['channels']['mobile'], $container, $loader);
83
        }
84
85 14
        $this->registerMessages($config['messages'], $container);
86 14
    }
87
88
    /**
89
     * @param array            $config
90
     * @param ContainerBuilder $container
91
     * @param XmlFileLoader    $loader
92
     */
93 4
    private function registerSwiftmailer(array $config, ContainerBuilder $container, XmlFileLoader $loader)
94
    {
95 4
        $container->setParameter(
96 4
            'yokai_messenger.swiftmailer_channel_defaults',
97
            [
98 4
                'from' => $config['from'],
99 4
                'translator_catalog' => $config['translator_catalog'],
100
            ]
101
        );
102 4
        $loader->load('swiftmailer.xml');
103 4
    }
104
105
    /**
106
     * @param array            $config
107
     * @param ContainerBuilder $container
108
     * @param XmlFileLoader    $loader
109
     */
110 4
    private function registerTwilio(array $config, ContainerBuilder $container, XmlFileLoader $loader)
111
    {
112 4
        $container->setParameter(
113 4
            'yokai_messenger.twilio_channel_defaults',
114
            [
115 4
                'from' => $config['from'],
116 4
                'api_id' => $config['api_id'],
117 4
                'api_token' => $config['api_token'],
118
            ]
119
        );
120 4
        $loader->load('twilio.xml');
121 4
    }
122
123
    /**
124
     * @param array            $config
125
     * @param ContainerBuilder $container
126
     * @param XmlFileLoader    $loader
127
     */
128 4
    private function registerDoctrine(array $config, ContainerBuilder $container, XmlFileLoader $loader)
129
    {
130 4
        $container->setAlias(
131 4
            'yokai_messenger.doctrine_channel_manager',
132 4
            sprintf(
133 4
                'doctrine.orm.%s_entity_manager',
134 4
                $config['manager']
135
            )
136
        );
137 4
        $loader->load('doctrine.xml');
138
139 4
        $container->setParameter('yokai_messenger.load_doctrine_orm_mapping', true);
140 4
    }
141
142
    /**
143
     * @param array            $config
144
     * @param ContainerBuilder $container
145
     * @param XmlFileLoader    $loader
146
     */
147 4
    private function registerMobile(array $config, ContainerBuilder $container, XmlFileLoader $loader)
148
    {
149 4
        $apnsEnabled = $config['apns']['enabled'] && class_exists('Sly\NotificationPusher\Adapter\Apns');
150 4
        $gcmEnabled = $config['gcm']['enabled'] && class_exists('Sly\NotificationPusher\Adapter\Gcm');
151
152 4
        if (!$apnsEnabled && !$gcmEnabled) {
153
            return;
154
        }
155
156 4
        $container->setParameter('yokai_messenger.mobile.push_manager.environment', $config['environment']);
157
158 4
        $loader->load('mobile.xml');
159
160 4
        if ($apnsEnabled) {
161 4
            $loader->load('mobile/apns.xml');
162 4
            $container->setParameter('yokai_messenger.mobile.apns_adapter.certificate', $config['apns']['certificate']);
163 4
            $container->setParameter('yokai_messenger.mobile.apns_adapter.pass_phrase', $config['apns']['pass_phrase']);
164
        }
165 4
        if ($gcmEnabled) {
166 4
            $loader->load('mobile/gcm.xml');
167 4
            $container->setParameter('yokai_messenger.mobile.gcm_adapter.api_key', $config['gcm']['api_key']);
168
        }
169 4
    }
170
171
    /**
172
     * @param array            $config
173
     * @param ContainerBuilder $container
174
     */
175 14
    private function registerMessages(array $config, ContainerBuilder $container)
176
    {
177 14
        foreach ($config as $messageConfig) {
178 2
            MessageDefinitionFactory::create(
179 2
                $container,
180 2
                $messageConfig['id'],
181 2
                $messageConfig['channels'],
182 2
                $messageConfig['defaults'],
183 2
                $messageConfig['options']
184
            );
185
        }
186 14
    }
187
}
188