Passed
Push — master ( c2b2ef...c33329 )
by DELEVOYE
02:48
created

SwiftmailerExtension::load()   C

Complexity

Conditions 16
Paths 6

Size

Total Lines 67
Code Lines 42

Duplication

Lines 14
Ratio 20.9 %

Code Coverage

Tests 27
CRAP Score 35.7214

Importance

Changes 0
Metric Value
dl 14
loc 67
ccs 27
cts 47
cp 0.5745
rs 5.7851
c 0
b 0
f 0
cc 16
eloc 42
nc 6
nop 2
crap 35.7214

How to fix   Long Method    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
/*
4
 * This file is part of the MindbazBundle package.
5
 *
6
 * (c) David DELEVOYE <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Kozikaza\MindbazBundle\DependencyInjection;
13
14
use Symfony\Bundle\SwiftmailerBundle\DependencyInjection\SwiftmailerExtension as BaseSwiftmailerExtension;
15
use Symfony\Component\DependencyInjection\ContainerBuilder;
16
use Symfony\Component\DependencyInjection\Extension\ExtensionInterface;
17
use Symfony\Component\HttpKernel\DependencyInjection\Extension;
18
19
/**
20
 * Swiftmailer bridge: declare Mindbaz configuration through Swiftmailer configuration.
21
 *
22
 * @author Vincent Chalamon <[email protected]>
23
 */
24
class SwiftmailerExtension extends Extension
25
{
26
    /**
27
     * @var ExtensionInterface|BaseSwiftmailerExtension
28
     */
29
    protected $extension;
30
31
    /**
32
     * @param ExtensionInterface|BaseSwiftmailerExtension $extension
33
     */
34 2
    public function __construct($extension)
35
    {
36 2
        $this->extension = $extension;
37 2
    }
38
39
    /**
40
     * {@inheritdoc}
41
     */
42
    public function getAlias()
43
    {
44
        return $this->extension->getAlias();
45
    }
46
47
    /**
48
     * {@inheritdoc}
49
     */
50
    public function getNamespace()
51
    {
52
        return $this->extension->getNamespace();
53
    }
54
55
    /**
56
     * @return bool|string
57
     */
58
    public function getXsdValidationBasePath()
59
    {
60
        return $this->extension->getXsdValidationBasePath();
61
    }
62
63
    /**
64
     * {@inheritdoc}
65
     */
66 1
    public function load(array $configs, ContainerBuilder $container)
67
    {
68 1
        foreach ($configs as $key => $config) {
69
            // Only 1 mailer configured
70 1
            if (!isset($config['mailers'])) {
71
                // It's not a Mindbaz transport
72
                if (!isset($config['transport']) || 'mindbaz' !== $config['transport']) {
73
                    continue;
74
                }
75
                // Mindbaz parameters are not set through Swiftmailer configuration
76 View Code Duplication
                if (!array_key_exists('id_site', $config) ||
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
77
                    !array_key_exists('username', $config) ||
78
                    !array_key_exists('password', $config) ||
79
                    !array_key_exists('campaigns', $config)
80
                ) {
81
                    continue;
82
                }
83
                // Set/erase Mindbaz configuration
84
                $container->prependExtensionConfig('mindbaz', [
85
                    'credentials' => [
86
                        'idSite'   => $config['id_site'],
87
                        'login'    => $config['username'],
88
                        'password' => $config['password'],
89
                    ],
90
                    'campaigns'   => $config['campaigns'],
91
                ]);
92
                unset(
93
                    $configs[$key]['id_site'],
94
                    $configs[$key]['username'],
95
                    $configs[$key]['password'],
96
                    $configs[$key]['campaigns']
97
                );
98
                continue;
99
            }
100
            // Multiple mailers configured
101 1
            foreach ($config['mailers'] as $name => $mailer) {
102
                // It's not a Mindbaz transport
103 1
                if (!isset($mailer['transport']) || 'mindbaz' !== $mailer['transport']) {
104 1
                    continue;
105
                }
106
                // Mindbaz parameters are not set through Swiftmailer configuration
107 1 View Code Duplication
                if (!array_key_exists('id_site', $mailer) ||
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
108 1
                    !array_key_exists('username', $mailer) ||
109 1
                    !array_key_exists('password', $mailer) ||
110 1
                    !array_key_exists('campaigns', $mailer)
111 1
                ) {
112 1
                    continue;
113
                }
114
                // Set/erase Mindbaz configuration
115 1
                $container->prependExtensionConfig('mindbaz', [
116
                    'credentials' => [
117 1
                        'idSite'   => $mailer['id_site'],
118 1
                        'login'    => $mailer['username'],
119 1
                        'password' => $mailer['password'],
120 1
                    ],
121 1
                    'campaigns'   => $mailer['campaigns'],
122 1
                ]);
123
                unset(
124 1
                    $configs[$key]['mailers'][$name]['id_site'],
125 1
                    $configs[$key]['mailers'][$name]['username'],
126 1
                    $configs[$key]['mailers'][$name]['password'],
127 1
                    $configs[$key]['mailers'][$name]['campaigns']
128
                );
129 1
            }
130 1
        }
131 1
        $this->extension->load($configs, $container);
132 1
    }
133
}
134