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

SwiftmailerExtension   A

Complexity

Total Complexity 20

Size/Duplication

Total Lines 110
Duplicated Lines 12.73 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 53.56%

Importance

Changes 0
Metric Value
wmc 20
lcom 1
cbo 2
dl 14
loc 110
ccs 30
cts 56
cp 0.5356
rs 10
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A getAlias() 0 4 1
A getNamespace() 0 4 1
A getXsdValidationBasePath() 0 4 1
C load() 14 67 16

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

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