YokaiMessengerExtension   A
last analyzed

Complexity

Total Complexity 23

Size/Duplication

Total Lines 176
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 5

Test Coverage

Coverage 96.15%

Importance

Changes 0
Metric Value
wmc 23
lcom 1
cbo 5
dl 0
loc 176
ccs 75
cts 78
cp 0.9615
rs 10
c 0
b 0
f 0

6 Methods

Rating   Name   Duplication   Size   Complexity  
C load() 0 48 11
A registerSwiftmailer() 0 11 1
A registerTwilio() 0 12 1
A registerDoctrine() 0 13 1
B registerMobile() 0 23 7
A registerMessages() 0 12 2
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
use Yokai\MessengerBundle\Sender\SenderInterface;
11
12
/**
13
 * @author Yann Eugoné <[email protected]>
14
 */
15
class YokaiMessengerExtension extends Extension
16
{
17
    /**
18
     * @var array
19
     */
20
    private $enabledChannelMap = [
21
        'swiftmailer' => [
22
            'class' => 'Symfony\Bundle\SwiftmailerBundle\SwiftmailerBundle',
23
            'bundle' => true,
24
        ],
25
        'twilio' => [
26
            'class' => 'Twilio\Rest\Api',
27
            'bundle' => false,
28
        ],
29
        'doctrine' => [
30
            'class' => 'Doctrine\Bundle\DoctrineBundle\DoctrineBundle',
31
            'bundle' => true,
32
        ],
33
        'mobile' => [
34
            'class' => 'Sly\NotificationPusher\NotificationPusher',
35
            'bundle' => false,
36
        ],
37
    ];
38
39
    /**
40
     * @inheritdoc
41
     */
42 14
    public function load(array $configs, ContainerBuilder $container)
43
    {
44 14
        $config = $this->processConfiguration($this->getConfiguration($configs, $container), $configs);
0 ignored issues
show
Documentation introduced by
$this->getConfiguration($configs, $container) is of type null|object, 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...
45
46 14
        $container->setParameter('yokai_messenger.content_builder_defaults', $config['content_builder']);
47 14
        $container->setParameter('yokai_messenger.logging_channel', $config['logging_channel']);
48
49 14
        $loader = new XmlFileLoader($container, new FileLocator(__DIR__ . '/../Resources/config'));
50 14
        $loader->load('services.xml');
51
52 14
        $bundles = $container->getParameter('kernel.bundles');
53 14
        $enabledChannels = [];
54 14
        foreach ($this->enabledChannelMap as $channel => $enableConfig) {
55 14
            $enabled = true;
56 14
            if (!$config['channels'][$channel]['enabled']) {
57 11
                $enabled = false;
58
            }
59 14
            if (!class_exists($enableConfig['class'])) {
60
                $enabled = false;
61
            }
62 14
            if ($enableConfig['bundle'] && !in_array($enableConfig['class'], $bundles)) {
63
                $enabled = false;
64
            }
65
66 14
            $enabledChannels[$channel] = $enabled;
67
        }
68
69 14
        foreach ($enabledChannels as $channel => $enabled) {
70 14
            $container->setParameter('yokai_messenger.'.$channel.'_enabled', $enabled);
71
        }
72
73 14
        if ($enabledChannels['swiftmailer']) {
74 4
            $this->registerSwiftmailer($config['channels']['swiftmailer'], $container, $loader);
75
        }
76 14
        if ($enabledChannels['twilio']) {
77 4
            $this->registerTwilio($config['channels']['twilio'], $container, $loader);
78
        }
79 14
        if ($enabledChannels['doctrine']) {
80 4
            $this->registerDoctrine($config['channels']['doctrine'], $container, $loader);
81
        }
82 14
        if ($enabledChannels['mobile']) {
83 4
            $this->registerMobile($config['channels']['mobile'], $container, $loader);
84
        }
85
86 14
        $this->registerMessages($config['messages'], $container);
87
88 14
        $container->setAlias(SenderInterface::class, 'yokai_messenger.sender');
89 14
    }
90
91
    /**
92
     * @param array            $config
93
     * @param ContainerBuilder $container
94
     * @param XmlFileLoader    $loader
95
     */
96 4
    private function registerSwiftmailer(array $config, ContainerBuilder $container, XmlFileLoader $loader)
97
    {
98 4
        $container->setParameter(
99 4
            'yokai_messenger.swiftmailer_channel_defaults',
100
            [
101 4
                'from' => $config['from'],
102 4
                'translator_catalog' => $config['translator_catalog'],
103
            ]
104
        );
105 4
        $loader->load('swiftmailer.xml');
106 4
    }
107
108
    /**
109
     * @param array            $config
110
     * @param ContainerBuilder $container
111
     * @param XmlFileLoader    $loader
112
     */
113 4
    private function registerTwilio(array $config, ContainerBuilder $container, XmlFileLoader $loader)
114
    {
115 4
        $container->setParameter(
116 4
            'yokai_messenger.twilio_channel_defaults',
117
            [
118 4
                'from' => $config['from'],
119 4
                'api_id' => $config['api_id'],
120 4
                'api_token' => $config['api_token'],
121
            ]
122
        );
123 4
        $loader->load('twilio.xml');
124 4
    }
125
126
    /**
127
     * @param array            $config
128
     * @param ContainerBuilder $container
129
     * @param XmlFileLoader    $loader
130
     */
131 4
    private function registerDoctrine(array $config, ContainerBuilder $container, XmlFileLoader $loader)
132
    {
133 4
        $container->setAlias(
134 4
            'yokai_messenger.doctrine_channel_manager',
135 4
            sprintf(
136 4
                'doctrine.orm.%s_entity_manager',
137 4
                $config['manager']
138
            )
139
        );
140 4
        $loader->load('doctrine.xml');
141
142 4
        $container->setParameter('yokai_messenger.load_doctrine_orm_mapping', true);
143 4
    }
144
145
    /**
146
     * @param array            $config
147
     * @param ContainerBuilder $container
148
     * @param XmlFileLoader    $loader
149
     */
150 4
    private function registerMobile(array $config, ContainerBuilder $container, XmlFileLoader $loader)
151
    {
152 4
        $apnsEnabled = $config['apns']['enabled'] && class_exists('Sly\NotificationPusher\Adapter\Apns');
153 4
        $gcmEnabled = $config['gcm']['enabled'] && class_exists('Sly\NotificationPusher\Adapter\Gcm');
154
155 4
        if (!$apnsEnabled && !$gcmEnabled) {
156
            return;
157
        }
158
159 4
        $container->setParameter('yokai_messenger.mobile.push_manager.environment', $config['environment']);
160
161 4
        $loader->load('mobile.xml');
162
163 4
        if ($apnsEnabled) {
164 4
            $loader->load('mobile/apns.xml');
165 4
            $container->setParameter('yokai_messenger.mobile.apns_adapter.certificate', $config['apns']['certificate']);
166 4
            $container->setParameter('yokai_messenger.mobile.apns_adapter.pass_phrase', $config['apns']['pass_phrase']);
167
        }
168 4
        if ($gcmEnabled) {
169 4
            $loader->load('mobile/gcm.xml');
170 4
            $container->setParameter('yokai_messenger.mobile.gcm_adapter.api_key', $config['gcm']['api_key']);
171
        }
172 4
    }
173
174
    /**
175
     * @param array            $config
176
     * @param ContainerBuilder $container
177
     */
178 14
    private function registerMessages(array $config, ContainerBuilder $container)
179
    {
180 14
        foreach ($config as $messageConfig) {
181 2
            MessageDefinitionFactory::create(
182 2
                $container,
183 2
                $messageConfig['id'],
184 2
                $messageConfig['channels'],
185 2
                $messageConfig['defaults'],
186 2
                $messageConfig['options']
187
            );
188
        }
189 14
    }
190
}
191