InnmindProvisionerExtension   A
last analyzed

Complexity

Total Complexity 9

Size/Duplication

Total Lines 130
Duplicated Lines 30.77 %

Coupling/Cohesion

Components 0
Dependencies 8

Test Coverage

Coverage 100%

Importance

Changes 7
Bugs 2 Features 2
Metric Value
wmc 9
c 7
b 2
f 2
lcom 0
cbo 8
dl 40
loc 130
ccs 94
cts 94
cp 1
rs 10

1 Method

Rating   Name   Duplication   Size   Complexity  
D load() 40 124 9

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
namespace Innmind\ProvisionerBundle\DependencyInjection;
4
5
use Symfony\Component\DependencyInjection\ContainerBuilder;
6
use Symfony\Component\Config\FileLocator;
7
use Symfony\Component\HttpKernel\DependencyInjection\Extension;
8
use Symfony\Component\DependencyInjection\Loader;
9
use Symfony\Component\DependencyInjection\Reference;
10
use Innmind\ProvisionerBundle\Voter\StandardVoter;
11
12
/**
13
 * This is the class that loads and manages your bundle configuration
14
 *
15
 * To learn more see {@link http://symfony.com/doc/current/cookbook/bundles/extension.html}
16
 */
17
class InnmindProvisionerExtension extends Extension
18
{
19
    /**
20
     * {@inheritdoc}
21
     */
22 42
    public function load(array $configs, ContainerBuilder $container)
23
    {
24 42
        $configuration = new Configuration();
25 42
        $config = $this->processConfiguration($configuration, $configs);
26
27 42
        $loader = new Loader\YamlFileLoader($container, new FileLocator(__DIR__.'/../Resources/config'));
28 42
        $loader->load('services.yml');
29
30 42
        $container->setParameter('innmind_provisioner', $config);
31
32 42
        $triggerManager = $container->getDefinition('innmind_provisioner.trigger_manager');
33
34 42
        $triggerManager->replaceArgument(0, $config['trigger_manager']['strategy']);
35 42
        $triggerManager->replaceArgument(1, $config['trigger_manager']['allow_if_equal_granted_denied']);
36 42
        $triggerManager->replaceArgument(2, $config['trigger_manager']['allow_if_all_abstain']);
37 42
        $triggerManager->addMethodCall('addVoter', [new StandardVoter($config['triggers'])]);
38
39
        $container
40 42
            ->getDefinition('innmind_provisioner.decision_manager')
41 42
            ->addMethodCall(
42 42
                'setCpuThreshold',
43 42
                [$config['threshold']['cpu']['max']]
44 42
            );
45
46 3
        $container
47 42
            ->getDefinition('innmind_provisioner.rabbitmq.queue_history')
48 42
            ->addMethodCall(
49 42
                'setHistoryLength',
50 42
                [$config['rabbitmq']['queue_depth']['history_length']]
51 42
            );
52
53
        $alert = $container
54 42
            ->getDefinition('innmind_provisioner.listener.alert')
55 42
            ->addMethodCall(
56 42
                'setCpuThresholds',
57
                [
58 42
                    $config['threshold']['cpu']['min'],
59 42
                    $config['threshold']['cpu']['max'],
60
                ]
61 42
            )
62 42
            ->addMethodCall(
63 42
                'setLoadAverageThresholds',
64
                [
65 42
                    $config['threshold']['load_average']['min'],
66 42
                    $config['threshold']['load_average']['max'],
67
                ]
68 42
            );
69
70 42
        if (isset($config['alerting']['email'])) {
71 3
            $alert->addMethodCall(
72 3
                'addAlerter',
73 3
                [new Reference('innmind_provisioner.alerter.email')]
74 3
            );
75
            $container
76 3
                ->getDefinition('innmind_provisioner.alerter.email')
77 3
                ->addMethodCall(
78 3
                    'setRecipient',
79 3
                    [$config['alerting']['email']]
80 3
                )
81 3
                ->addMethodCall(
82 3
                    'setMailer',
83 3
                    [new Reference('mailer')]
84 3
                );
85 3
        }
86
        if (
87 42
            isset($config['alerting']['webhook']) &&
88 12
            !empty($config['alerting']['webhook'])
89 42
        ) {
90 3
            $alert->addMethodCall(
91 3
                'addAlerter',
92 3
                [new Reference('innmind_provisioner.alerter.webhook')]
93 3
            );
94 3
            $webhook = $container->getDefinition('innmind_provisioner.alerter.webhook');
95
96 3
            foreach ($config['alerting']['webhook'] as $uri) {
97 3
                $webhook->addMethodCall(
98 3
                    'addUri',
99 3
                    [$uri]
100 3
                );
101 3
            }
102 3
        }
103
104 View Code Duplication
        if (
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...
105 42
            isset($config['alerting']['hipchat']) &&
106 3
            !empty($config['alerting']['hipchat'])
107 42
        ) {
108 3
            $alert->addMethodCall(
109 3
                'addAlerter',
110 3
                [new Reference('innmind_provisioner.alerter.hipchat')]
111 3
            );
112
113
            $container
114 3
                ->getDefinition('innmind_provisioner.alerter.hipchat.oauth')
115 3
                ->replaceArgument(0, $config['alerting']['hipchat']['token']);
116
117
            $container
118 3
                ->getDefinition('innmind_provisioner.alerter.hipchat')
119 3
                ->addMethodCall(
120 3
                    'setRoom',
121 3
                    [$config['alerting']['hipchat']['room']]
122 3
                );
123 3
        }
124
125 View Code Duplication
        if (
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...
126 42
            isset($config['alerting']['slack']) &&
127 3
            !empty($config['alerting']['slack'])
128 42
        ) {
129 3
            $alert->addMethodCall(
130 3
                'addAlerter',
131 3
                [new Reference('innmind_provisioner.alerter.slack')]
132 3
            );
133
134
            $container
135 3
                ->getDefinition('innmind_provisioner.alerter.slack.commander')
136 3
                ->replaceArgument(0, $config['alerting']['slack']['token']);
137
138
            $container
139 3
                ->getDefinition('innmind_provisioner.alerter.slack')
140 3
                ->addMethodCall(
141 3
                    'setChannel',
142 3
                    [$config['alerting']['slack']['channel']]
143 3
                );
144 3
        }
145 42
    }
146
}
147