Completed
Push — master ( 72db84...f978a2 )
by Yann
02:16
created

YokaiMessengerExtension::load()   D

Complexity

Conditions 10
Paths 144

Size

Total Lines 43
Code Lines 26

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 31
CRAP Score 10.1493

Importance

Changes 0
Metric Value
dl 0
loc 43
ccs 31
cts 35
cp 0.8857
rs 4.606
c 0
b 0
f 0
cc 10
eloc 26
nc 144
nop 2
crap 10.1493

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
        'doctrine' => [
25
            'class' => 'Doctrine\Bundle\DoctrineBundle\DoctrineBundle',
26
            'bundle' => true,
27
        ],
28
        'mobile' => [
29
            'class' => 'Sly\NotificationPusher\NotificationPusher',
30
            'bundle' => false,
31
        ],
32
    ];
33
34
    /**
35
     * @inheritdoc
36
     */
37 13
    public function load(array $configs, ContainerBuilder $container)
38
    {
39 13
        $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...
40
41 13
        $container->setParameter('yokai_messenger.content_builder_defaults', $config['content_builder']);
42 13
        $container->setParameter('yokai_messenger.logging_channel', $config['logging_channel']);
43
44 13
        $loader = new XmlFileLoader($container, new FileLocator(__DIR__ . '/../Resources/config'));
45 13
        $loader->load('services.xml');
46
47 13
        $bundles = $container->getParameter('kernel.bundles');
48 13
        $enabledChannels = [];
49 13
        foreach ($this->enabledChannelMap as $channel => $enableConfig) {
50 13
            $enabled = true;
51 13
            if (!$config['channels'][$channel]['enabled']) {
52 10
                $enabled = false;
53 10
            }
54 13
            if (!class_exists($enableConfig['class'])) {
55
                $enabled = false;
56
            }
57 13
            if ($enableConfig['bundle'] && !in_array($enableConfig['class'], $bundles)) {
58
                $enabled = false;
59
            }
60
61 13
            $enabledChannels[$channel] = $enabled;
62 13
        }
63
64 13
        foreach ($enabledChannels as $channel => $enabled) {
65 13
            $container->setParameter('yokai_messenger.'.$channel.'_enabled', $enabled);
66 13
        }
67
68 13
        if ($enabledChannels['swiftmailer']) {
69 4
            $this->registerSwiftmailer($config['channels']['swiftmailer'], $container, $loader);
70 4
        }
71 13
        if ($enabledChannels['doctrine']) {
72 4
            $this->registerDoctrine($config['channels']['doctrine'], $container, $loader);
73 4
        }
74 13
        if ($enabledChannels['mobile']) {
75 4
            $this->registerMobile($config['channels']['mobile'], $container, $loader);
76 4
        }
77
78 13
        $this->registerMessages($config['messages'], $container);
79 13
    }
80
81
    /**
82
     * @param array            $config
83
     * @param ContainerBuilder $container
84
     * @param XmlFileLoader    $loader
85
     */
86 4
    private function registerSwiftmailer(array $config, ContainerBuilder $container, XmlFileLoader $loader)
87
    {
88 4
        $container->setParameter(
89 4
            'yokai_messenger.swiftmailer_channel_defaults',
90
            [
91 4
                'from' => $config['from'],
92 4
                'translator_catalog' => $config['translator_catalog'],
93
            ]
94 4
        );
95 4
        $loader->load('swiftmailer.xml');
96 4
    }
97
98
    /**
99
     * @param array            $config
100
     * @param ContainerBuilder $container
101
     * @param XmlFileLoader    $loader
102
     */
103 4
    private function registerDoctrine(array $config, ContainerBuilder $container, XmlFileLoader $loader)
104
    {
105 4
        $container->setAlias(
106 4
            'yokai_messenger.doctrine_channel_manager',
107 4
            sprintf(
108 4
                'doctrine.orm.%s_entity_manager',
109 4
                $config['manager']
110 4
            )
111 4
        );
112 4
        $loader->load('doctrine.xml');
113
114 4
        $container->setParameter('yokai_messenger.load_doctrine_orm_mapping', true);
115 4
    }
116
117
    /**
118
     * @param array            $config
119
     * @param ContainerBuilder $container
120
     * @param XmlFileLoader    $loader
121
     */
122 4
    private function registerMobile(array $config, ContainerBuilder $container, XmlFileLoader $loader)
123
    {
124 4
        $apnsEnabled = $config['apns']['enabled'] && class_exists('Sly\NotificationPusher\Adapter\Apns');
125 4
        $gcmEnabled = $config['gcm']['enabled'] && class_exists('Sly\NotificationPusher\Adapter\Gcm');
126
127 4
        if (!$apnsEnabled && !$gcmEnabled) {
128
            return;
129
        }
130
131 4
        $container->setParameter('yokai_messenger.mobile.push_manager.environment', $config['environment']);
132
133 4
        $loader->load('mobile.xml');
134
135 4
        if ($apnsEnabled) {
136 4
            $loader->load('mobile/apns.xml');
137 4
            $container->setParameter('yokai_messenger.mobile.apns_adapter.certificate', $config['apns']['certificate']);
138 4
            $container->setParameter('yokai_messenger.mobile.apns_adapter.pass_phrase', $config['apns']['pass_phrase']);
139 4
        }
140 4
        if ($gcmEnabled) {
141 4
            $loader->load('mobile/gcm.xml');
142 4
            $container->setParameter('yokai_messenger.mobile.gcm_adapter.api_key', $config['gcm']['api_key']);
143 4
        }
144 4
    }
145
146
    /**
147
     * @param array            $config
148
     * @param ContainerBuilder $container
149
     */
150 13
    private function registerMessages(array $config, ContainerBuilder $container)
151
    {
152 13
        foreach ($config as $messageConfig) {
153 2
            MessageDefinitionFactory::create(
154 2
                $container,
155 2
                $messageConfig['id'],
156 2
                $messageConfig['channels'],
157 2
                $messageConfig['defaults'],
158 2
                $messageConfig['options']
159 2
            );
160 13
        }
161 13
    }
162
}
163