Passed
Push — master ( da9ebc...eab3b6 )
by Tobias
02:14
created

EkinoNewRelicExtension::load()   F

Complexity

Conditions 14
Paths 1536

Size

Total Lines 80
Code Lines 54

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 80
rs 2.0666
c 0
b 0
f 0
cc 14
eloc 54
nc 1536
nop 2

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
declare(strict_types=1);
4
5
/*
6
 * This file is part of Ekino New Relic bundle.
7
 *
8
 * (c) Ekino - Thomas Rabaix <[email protected]>
9
 *
10
 * For the full copyright and license information, please view the LICENSE
11
 * file that was distributed with this source code.
12
 */
13
14
namespace  Ekino\NewRelicBundle\DependencyInjection;
15
16
use Ekino\NewRelicBundle\Listener\CommandListener;
17
use Ekino\NewRelicBundle\Listener\RequestListener;
18
use Ekino\NewRelicBundle\Listener\ResponseListener;
19
use Ekino\NewRelicBundle\NewRelic\BlackholeInteractor;
20
use Ekino\NewRelicBundle\NewRelic\Config;
21
use Ekino\NewRelicBundle\NewRelic\LoggingInteractorDecorator;
22
use Ekino\NewRelicBundle\NewRelic\NewRelicInteractor;
23
use Ekino\NewRelicBundle\TransactionNamingStrategy\ControllerNamingStrategy;
24
use Ekino\NewRelicBundle\TransactionNamingStrategy\RouteNamingStrategy;
25
use Symfony\Component\Config\FileLocator;
26
use Symfony\Component\DependencyInjection\ContainerBuilder;
27
use Symfony\Component\DependencyInjection\Loader;
28
use Symfony\Component\DependencyInjection\Reference;
29
use Symfony\Component\HttpKernel\DependencyInjection\Extension;
30
31
/**
32
 * This is the class that loads and manages your bundle configuration.
33
 *
34
 * To learn more see {@link http://symfony.com/doc/current/cookbook/bundles/extension.html}
35
 */
36
class EkinoNewRelicExtension extends Extension
37
{
38
    public function load(array $configs, ContainerBuilder $container): void
39
    {
40
        $configuration = new Configuration();
41
        $config = $this->processConfiguration($configuration, $configs);
42
43
        $loader = new Loader\XmlFileLoader($container, new FileLocator(__DIR__.'/../Resources/config'));
44
        $loader->load('services.xml');
45
46
        if (!$config['enabled']) {
47
            $config['monolog']['enabled'] = false;
48
            $interactor = BlackholeInteractor::class;
49
        } elseif (isset($config['interactor'])) {
50
            $interactor = $config['interactor'];
51
        } else {
52
            // Fallback to see if the extension is loaded or not
53
            $interactor = \extension_loaded('newrelic') ? NewRelicInteractor::class : BlackholeInteractor::class;
54
        }
55
56
        if ($config['logging']) {
57
            $container->getDefinition(LoggingInteractorDecorator::class)
58
                ->replaceArgument(0, new Reference($interactor));
59
            $interactor = LoggingInteractorDecorator::class;
60
        }
61
        $container->setAlias('ekino.new_relic.interactor', $interactor);
62
63
        if (!empty($config['deployment_names'])) {
64
            $config['deployment_names'] = \array_values(\array_filter(\explode(';', $config['application_name'])));
65
        }
66
67
        $container->getDefinition(Config::class)
68
            ->replaceArgument(0, $config['application_name'])
69
            ->replaceArgument(1, $config['api_key'])
70
            ->replaceArgument(2, $config['license_key'])
71
            ->replaceArgument(3, $config['xmit'])
72
            ->replaceArgument(4, $config['deployment_names'])
73
        ;
74
75
        if ($config['http']['enabled']) {
76
            $loader->load('http_listener.xml');
77
            $container->getDefinition(RequestListener::class)
78
                ->replaceArgument(2, $config['http']['ignored_routes'])
79
                ->replaceArgument(3, $config['http']['ignored_paths'])
80
                ->replaceArgument(4, $this->getTransactionNamingService($config))
81
                ->replaceArgument(5, $config['http']['using_symfony_cache']);
82
83
            $container->getDefinition(ResponseListener::class)
84
                ->replaceArgument(2, $config['http']['instrument'])
85
                ->replaceArgument(3, $config['http']['using_symfony_cache']);
86
        }
87
88
        if ($config['commands']['enabled']) {
89
            $loader->load('command_listener.xml');
90
            $container->getDefinition(CommandListener::class)
91
                ->replaceArgument(2, $config['commands']['ignored_commands']);
92
        }
93
94
        if ($config['exceptions']['enabled']) {
95
            $loader->load('exception_listener.xml');
96
        }
97
98
        if ($config['deprecations']['enabled']) {
99
            $loader->load('deprecation_listener.xml');
100
        }
101
102
        if ($config['twig']) {
103
            $loader->load('twig.xml');
104
        }
105
106
        if ($config['monolog']['enabled']) {
107
            if (!\class_exists(\Monolog\Handler\NewRelicHandler::class)) {
108
                throw new \LogicException('The "symfony/monolog-bundle" package must be installed in order to use "monolog" option.');
109
            }
110
            $loader->load('monolog.xml');
111
            $container->setParameter('ekino.new_relic.monolog.channels', $config['monolog']['channels']);
112
            $container->setAlias('ekino.new_relic.logs_handler', $config['monolog']['service']);
113
114
            $level = $config['monolog']['level'];
115
            $container->findDefinition('ekino.new_relic.logs_handler')
116
                ->replaceArgument(0, \is_int($level) ? $level : \constant('Monolog\Logger::'.\strtoupper($level)))
117
                ->replaceArgument(2, $config['application_name']);
118
        }
119
    }
120
121
    private function getTransactionNamingService(array $config): Reference
122
    {
123
        switch ($config['http']['transaction_naming']) {
124
            case 'controller':
125
                $serviceId = new Reference(ControllerNamingStrategy::class);
126
                break;
127
            case 'route':
128
                $serviceId = new Reference(RouteNamingStrategy::class);
129
                break;
130
            case 'service':
131
                if (!isset($config['http']['transaction_naming_service'])) {
132
                    throw new \LogicException(
133
                        'When using the "service", transaction naming scheme, the "transaction_naming_service" config parameter must be set.'
134
                    );
135
                }
136
137
                $serviceId = new Reference($config['http']['transaction_naming_service']);
138
                break;
139
            default:
140
                throw new \InvalidArgumentException(
141
                    \sprintf(
142
                        'Invalid transaction naming scheme "%s", must be "route", "controller" or "service".',
143
                        $config['http']['transaction_naming']
144
                    )
145
                );
146
        }
147
148
        return $serviceId;
149
    }
150
}
151