Completed
Pull Request — master (#15)
by Yann
02:46
created

YokaiMessengerExtension::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 3
cts 3
cp 1
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 1
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
     * @var string
18
     */
19
    private $name;
20
21
    /**
22
     * @param string $name
23
     */
24 14
    public function __construct($name)
25
    {
26 14
        $this->name = $name;
27 14
    }
28
29
    /**
30
     * @inheritdoc
31
     */
32 13
    public function load(array $configs, ContainerBuilder $container)
33
    {
34 13
        $config = $this->processConfiguration(
35 13
            $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
        );
38
39 13
        $container->setParameter(
40 13
            'yokai_messenger.content_builder_defaults',
41 13
            $config['content_builder']
42
        );
43 13
        $container->setParameter(
44 13
            'yokai_messenger.logging_channel',
45 13
            $config['logging_channel']
46
        );
47
48 13
        $loader = new XmlFileLoader($container, new FileLocator(__DIR__ . '/../Resources/config'));
49 13
        $loader->load('services.xml');
50
51 13
        $swiftmailerEnabled = $config['channels']['swiftmailer']['enabled'] &&
52 13
                              class_exists('Symfony\Bundle\SwiftmailerBundle\SwiftmailerBundle');
53 13
        $doctrineEnabled = $config['channels']['doctrine']['enabled'] &&
54 13
                           class_exists('Doctrine\Bundle\DoctrineBundle\DoctrineBundle');
55 13
        $mobileEnabled = $config['channels']['mobile']['enabled'] &&
56 13
                         class_exists('Sly\NotificationPusher\NotificationPusher');
57
58 13
        $container->setParameter('yokai_messenger.swiftmailer_enabled', $swiftmailerEnabled);
59 13
        $container->setParameter('yokai_messenger.doctrine_enabled', $doctrineEnabled);
60 13
        $container->setParameter('yokai_messenger.mobile_enabled', $mobileEnabled);
61
62 13
        if ($swiftmailerEnabled) {
63 4
            $this->registerSwiftmailer($config['channels']['swiftmailer'], $container, $loader);
64
        }
65 13
        if ($doctrineEnabled) {
66 4
            $this->registerDoctrine($config['channels']['doctrine'], $container, $loader);
67
        }
68 13
        if ($mobileEnabled) {
69 4
            $this->registerMobile($config['channels']['mobile'], $container, $loader);
70
        }
71
72 13
        $this->registerMessages($config['messages'], $container);
73 13
    }
74
75
    /**
76
     * @inheritdoc
77
     */
78 13
    public function getConfiguration(array $config, ContainerBuilder $container)
79
    {
80 13
        return new Configuration($this->name);
81
    }
82
83
    /**
84
     * @inheritdoc
85
     */
86 14
    public function getAlias()
87
    {
88 14
        return $this->name;
89
    }
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 registerDoctrine(array $config, ContainerBuilder $container, XmlFileLoader $loader)
114
    {
115 4
        $container->setAlias(
116 4
            'yokai_messenger.doctrine_channel_manager',
117
            sprintf(
118 4
                'doctrine.orm.%s_entity_manager',
119 4
                $config['manager']
120
            )
121
        );
122 4
        $loader->load('doctrine.xml');
123 4
    }
124
125
    /**
126
     * @param array            $config
127
     * @param ContainerBuilder $container
128
     * @param XmlFileLoader    $loader
129
     */
130 4
    private function registerMobile(array $config, ContainerBuilder $container, XmlFileLoader $loader)
131
    {
132 4
        $apnsEnabled = $config['apns']['enabled'] && class_exists('Sly\NotificationPusher\Adapter\Apns');
133 4
        $gcmEnabled = $config['gcm']['enabled'] && class_exists('Sly\NotificationPusher\Adapter\Gcm');
134
135 4
        if (!$apnsEnabled && !$gcmEnabled) {
136
            return;
137
        }
138
139 4
        $container->setParameter('yokai_messenger.mobile.push_manager.environment', $config['environment']);
140
141 4
        $loader->load('mobile.xml');
142
143 4
        if ($apnsEnabled) {
144 4
            $loader->load('mobile/apns.xml');
145 4
            $container->setParameter('yokai_messenger.mobile.apns_adapter.certificate', $config['apns']['certificate']);
146 4
            $container->setParameter('yokai_messenger.mobile.apns_adapter.pass_phrase', $config['apns']['pass_phrase']);
147
        }
148 4
        if ($gcmEnabled) {
149 4
            $loader->load('mobile/gcm.xml');
150 4
            $container->setParameter('yokai_messenger.mobile.gcm_adapter.api_key', $config['gcm']['api_key']);
151
        }
152 4
    }
153
154
    /**
155
     * @param array            $config
156
     * @param ContainerBuilder $container
157
     */
158 13
    private function registerMessages(array $config, ContainerBuilder $container)
159
    {
160 13
        foreach ($config as $messageConfig) {
161 2
            MessageDefinitionFactory::create(
162
                $container,
163 2
                $messageConfig['id'],
164 2
                $messageConfig['channels'],
165 2
                $messageConfig['defaults'],
166 2
                $messageConfig['options']
167
            );
168
        }
169 13
    }
170
}
171