Completed
Push — master ( 456ca5...709ceb )
by Yann
05:58
created

YokaiMessengerExtension::getAlias()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 0
crap 1
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
     * @inheritdoc
18
     */
19 13
    public function load(array $configs, ContainerBuilder $container)
20
    {
21 13
        $config = $this->processConfiguration(
22 13
            $this->getConfiguration($configs, $container),
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...
23
            $configs
24 13
        );
25
26 13
        $container->setParameter(
27 13
            'yokai_messenger.content_builder_defaults',
28 13
            $config['content_builder']
29 13
        );
30 13
        $container->setParameter(
31 13
            'yokai_messenger.logging_channel',
32 13
            $config['logging_channel']
33 13
        );
34
35 13
        $loader = new XmlFileLoader($container, new FileLocator(__DIR__ . '/../Resources/config'));
36 13
        $loader->load('services.xml');
37
38 13
        $swiftmailerEnabled = $config['channels']['swiftmailer']['enabled'] &&
39 13
                              class_exists('Symfony\Bundle\SwiftmailerBundle\SwiftmailerBundle');
40 13
        $doctrineEnabled = $config['channels']['doctrine']['enabled'] &&
41 13
                           class_exists('Doctrine\Bundle\DoctrineBundle\DoctrineBundle');
42 13
        $mobileEnabled = $config['channels']['mobile']['enabled'] &&
43 13
                         class_exists('Sly\NotificationPusher\NotificationPusher');
44
45 13
        $container->setParameter('yokai_messenger.swiftmailer_enabled', $swiftmailerEnabled);
46 13
        $container->setParameter('yokai_messenger.doctrine_enabled', $doctrineEnabled);
47 13
        $container->setParameter('yokai_messenger.mobile_enabled', $mobileEnabled);
48
49 13
        if ($swiftmailerEnabled) {
50 4
            $this->registerSwiftmailer($config['channels']['swiftmailer'], $container, $loader);
51 4
        }
52 13
        if ($doctrineEnabled) {
53 4
            $this->registerDoctrine($config['channels']['doctrine'], $container, $loader);
54 4
        }
55 13
        if ($mobileEnabled) {
56 4
            $this->registerMobile($config['channels']['mobile'], $container, $loader);
57 4
        }
58
59 13
        $this->registerMessages($config['messages'], $container);
60 13
    }
61
62
    /**
63
     * @param array            $config
64
     * @param ContainerBuilder $container
65
     * @param XmlFileLoader    $loader
66
     */
67 4
    private function registerSwiftmailer(array $config, ContainerBuilder $container, XmlFileLoader $loader)
68
    {
69 4
        $container->setParameter(
70 4
            'yokai_messenger.swiftmailer_channel_defaults',
71
            [
72 4
                'from' => $config['from'],
73 4
                'translator_catalog' => $config['translator_catalog'],
74
            ]
75 4
        );
76 4
        $loader->load('swiftmailer.xml');
77 4
    }
78
79
    /**
80
     * @param array            $config
81
     * @param ContainerBuilder $container
82
     * @param XmlFileLoader    $loader
83
     */
84 4
    private function registerDoctrine(array $config, ContainerBuilder $container, XmlFileLoader $loader)
85
    {
86 4
        $container->setAlias(
87 4
            'yokai_messenger.doctrine_channel_manager',
88 4
            sprintf(
89 4
                'doctrine.orm.%s_entity_manager',
90 4
                $config['manager']
91 4
            )
92 4
        );
93 4
        $loader->load('doctrine.xml');
94 4
    }
95
96
    /**
97
     * @param array            $config
98
     * @param ContainerBuilder $container
99
     * @param XmlFileLoader    $loader
100
     */
101 4
    private function registerMobile(array $config, ContainerBuilder $container, XmlFileLoader $loader)
102
    {
103 4
        $apnsEnabled = $config['apns']['enabled'] && class_exists('Sly\NotificationPusher\Adapter\Apns');
104 4
        $gcmEnabled = $config['gcm']['enabled'] && class_exists('Sly\NotificationPusher\Adapter\Gcm');
105
106 4
        if (!$apnsEnabled && !$gcmEnabled) {
107
            return;
108
        }
109
110 4
        $container->setParameter('yokai_messenger.mobile.push_manager.environment', $config['environment']);
111
112 4
        $loader->load('mobile.xml');
113
114 4
        if ($apnsEnabled) {
115 4
            $loader->load('mobile/apns.xml');
116 4
            $container->setParameter('yokai_messenger.mobile.apns_adapter.certificate', $config['apns']['certificate']);
117 4
            $container->setParameter('yokai_messenger.mobile.apns_adapter.pass_phrase', $config['apns']['pass_phrase']);
118 4
        }
119 4
        if ($gcmEnabled) {
120 4
            $loader->load('mobile/gcm.xml');
121 4
            $container->setParameter('yokai_messenger.mobile.gcm_adapter.api_key', $config['gcm']['api_key']);
122 4
        }
123 4
    }
124
125
    /**
126
     * @param array            $config
127
     * @param ContainerBuilder $container
128
     */
129 13
    private function registerMessages(array $config, ContainerBuilder $container)
130
    {
131 13
        foreach ($config as $messageConfig) {
132 2
            MessageDefinitionFactory::create(
133 2
                $container,
134 2
                $messageConfig['id'],
135 2
                $messageConfig['channels'],
136 2
                $messageConfig['defaults'],
137 2
                $messageConfig['options']
138 2
            );
139 13
        }
140 13
    }
141
}
142