Completed
Push — master ( 366e1c...39d38f )
by Yann
06:12
created

YokaiMessengerExtension::load()   C

Complexity

Conditions 7
Paths 64

Size

Total Lines 42
Code Lines 28

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 28
CRAP Score 7

Importance

Changes 5
Bugs 0 Features 1
Metric Value
c 5
b 0
f 1
dl 0
loc 42
rs 6.7272
ccs 28
cts 28
cp 1
cc 7
eloc 28
nc 64
nop 2
crap 7
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 string
18
     */
19
    private $name;
20
21
    /**
22
     * @param string $name
23
     */
24 12
    public function __construct($name)
25
    {
26 12
        $this->name = $name;
27 12
    }
28
29
    /**
30
     * @inheritdoc
31
     */
32 11
    public function load(array $configs, ContainerBuilder $container)
33
    {
34 11
        $config = $this->processConfiguration(
35 11
            $this->getConfiguration($configs, $container),
0 ignored issues
show
Bug introduced by
It seems like $this->getConfiguration($configs, $container) can be null; however, processConfiguration() does not accept null, maybe add an additional type check?

Unless you are absolutely sure that the expression can never be null because of other conditions, we strongly recommend to add an additional type check to your code:

/** @return stdClass|null */
function mayReturnNull() { }

function doesNotAcceptNull(stdClass $x) { }

// With potential error.
function withoutCheck() {
    $x = mayReturnNull();
    doesNotAcceptNull($x); // Potential error here.
}

// Safe - Alternative 1
function withCheck1() {
    $x = mayReturnNull();
    if ( ! $x instanceof stdClass) {
        throw new \LogicException('$x must be defined.');
    }
    doesNotAcceptNull($x);
}

// Safe - Alternative 2
function withCheck2() {
    $x = mayReturnNull();
    if ($x instanceof stdClass) {
        doesNotAcceptNull($x);
    }
}
Loading history...
36
            $configs
37 11
        );
38
39 11
        $container->setParameter(
40 11
            'yokai_messenger.content_builder_defaults',
41 11
            $config['content_builder']
42 11
        );
43 11
        $container->setParameter(
44 11
            'yokai_messenger.logging_channel',
45 11
            $config['logging_channel']
46 11
        );
47
48 11
        $loader = new XmlFileLoader($container, new FileLocator(__DIR__ . '/../Resources/config'));
49 11
        $loader->load('services.xml');
50
51 11
        $swiftmailerEnabled = $config['channels']['swiftmailer']['enabled'] &&
52 11
                              class_exists('Symfony\Bundle\SwiftmailerBundle\SwiftmailerBundle');
53
        $doctrineEnabled = $config['channels']['doctrine']['enabled'] &&
54 11
                           class_exists('Doctrine\Bundle\DoctrineBundle\DoctrineBundle');
55 11
        $mobileEnabled = $config['channels']['mobile']['enabled'] &&
56
                         class_exists('Sly\NotificationPusher\NotificationPusher');
57 11
58 3
        $container->setParameter('yokai_messenger.swiftmailer_enabled', $swiftmailerEnabled);
59 3
        $container->setParameter('yokai_messenger.doctrine_enabled', $doctrineEnabled);
60 11
        $container->setParameter('yokai_messenger.mobile_enabled', $mobileEnabled);
61 3
62 3
        if ($swiftmailerEnabled) {
63
            $this->registerSwiftmailer($config['channels']['swiftmailer'], $container, $loader);
64 11
        }
65 11
        if ($doctrineEnabled) {
66
            $this->registerDoctrine($config['channels']['doctrine'], $container, $loader);
67
        }
68
        if ($mobileEnabled) {
69
            $this->registerMobile($config['channels']['mobile'], $container, $loader);
70 11
        }
71
72 11
        $this->registerMessages($config['messages'], $container);
73
    }
74
75
    /**
76
     * @inheritdoc
77
     */
78 12
    public function getConfiguration(array $config, ContainerBuilder $container)
79
    {
80 12
        return new Configuration($this->name);
81
    }
82
83
    /**
84
     * @inheritdoc
85
     */
86
    public function getAlias()
87
    {
88 4
        return $this->name;
89
    }
90 3
91 3
    /**
92
     * @param array            $config
93 3
     * @param ContainerBuilder $container
94 3
     * @param XmlFileLoader    $loader
95
     */
96 3
    private function registerSwiftmailer(array $config, ContainerBuilder $container, XmlFileLoader $loader)
97 4
    {
98 3
        $container->setParameter(
99
            'yokai_messenger.swiftmailer_channel_defaults',
100
            [
101
                'from' => $config['from_addr'],
102
                'translator_catalog' => $config['translator_catalog'],
103
            ]
104
        );
105 3
        $loader->load('swiftmailer.xml');
106
    }
107 3
108 3
    /**
109 3
     * @param array            $config
110 3
     * @param ContainerBuilder $container
111 3
     * @param XmlFileLoader    $loader
112 3
     */
113 3
    private function registerDoctrine(array $config, ContainerBuilder $container, XmlFileLoader $loader)
114 3
    {
115 3
        $container->setAlias(
116
            'yokai_messenger.doctrine_channel_manager',
117
            sprintf(
118
                'doctrine.orm.%s_entity_manager',
119
                $config['manager']
120
            )
121 11
        );
122
        $loader->load('doctrine.xml');
123 11
    }
124 1
125 1
    /**
126 1
     * @param array            $config
127 1
     * @param ContainerBuilder $container
128 1
     * @param XmlFileLoader    $loader
129 1
     */
130 1
    private function registerMobile(array $config, ContainerBuilder $container, XmlFileLoader $loader)
131 11
    {
132 11
        $apnsEnabled = $config['apns']['enabled'] && class_exists('Sly\NotificationPusher\Adapter\Apns');
133
        $gcmEnabled = $config['gcm']['enabled'] && class_exists('Sly\NotificationPusher\Adapter\Gcm');
134
135
        if (!$apnsEnabled && !$gcmEnabled) {
136
            return;
137
        }
138
139
        $container->setParameter('yokai_messenger.mobile.push_manager.environment', $config['environment']);
140
141
        $loader->load('mobile.xml');
142
143
        if ($apnsEnabled) {
144
            $loader->load('mobile/apns.xml');
145
            $container->setParameter('yokai_messenger.mobile.apns_adapter.certificate', $config['apns']['certificate']);
146
            $container->setParameter('yokai_messenger.mobile.apns_adapter.pass_phrase', $config['apns']['pass_phrase']);
147
        }
148
        if ($gcmEnabled) {
149
            $loader->load('mobile/gcm.xml');
150
            $container->setParameter('yokai_messenger.mobile.gcm_adapter.api_key', $config['gcm']['api_key']);
151
        }
152
    }
153
154
    /**
155
     * @param array            $config
156
     * @param ContainerBuilder $container
157
     */
158
    private function registerMessages(array $config, ContainerBuilder $container)
159
    {
160
        foreach ($config as $messageConfig) {
161
            MessageDefinitionFactory::create(
162
                $container,
163
                $messageConfig['id'],
164
                $messageConfig['channels'],
165
                $messageConfig['defaults'],
166
                $messageConfig['options']
167
            );
168
        }
169
    }
170
}
171