Passed
Pull Request — master (#221)
by
unknown
06:00 queued 27s
created

EkinoNewRelicExtension::load()   F

Complexity

Conditions 12
Paths 384

Size

Total Lines 89
Code Lines 51

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 51
dl 0
loc 89
rs 3.8333
c 0
b 0
f 0
cc 12
nc 384
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\AdaptiveInteractor;
20
use Ekino\NewRelicBundle\NewRelic\BlackholeInteractor;
21
use Ekino\NewRelicBundle\NewRelic\Config;
22
use Ekino\NewRelicBundle\NewRelic\LoggingInteractorDecorator;
23
use Ekino\NewRelicBundle\NewRelic\NewRelicInteractor;
24
use Ekino\NewRelicBundle\NewRelic\NewRelicInteractorInterface;
25
use Ekino\NewRelicBundle\TransactionNamingStrategy\ControllerNamingStrategy;
26
use Ekino\NewRelicBundle\TransactionNamingStrategy\RouteNamingStrategy;
27
use Ekino\NewRelicBundle\TransactionNamingStrategy\TransactionNamingStrategyInterface;
28
use Symfony\Component\Config\FileLocator;
29
use Symfony\Component\DependencyInjection\ContainerBuilder;
30
use Symfony\Component\DependencyInjection\ContainerInterface;
31
use Symfony\Component\DependencyInjection\Loader;
32
use Symfony\Component\DependencyInjection\Reference;
33
use Symfony\Component\HttpKernel\DependencyInjection\Extension;
34
35
/**
36
 * This is the class that loads and manages your bundle configuration.
37
 *
38
 * To learn more see {@link http://symfony.com/doc/current/cookbook/bundles/extension.html}
39
 */
40
class EkinoNewRelicExtension extends Extension
41
{
42
    public function load(array $configs, ContainerBuilder $container): void
43
    {
44
        $configuration = new Configuration();
45
        $config = $this->processConfiguration($configuration, $configs);
46
47
        $loader = new Loader\XmlFileLoader($container, new FileLocator(__DIR__.'/../Resources/config'));
48
        $loader->load('services.xml');
49
50
        $container->setAlias(NewRelicInteractorInterface::class, $this->getInteractorServiceId($config))->setPublic(false);
51
        $container->setAlias(TransactionNamingStrategyInterface::class, $this->getTransactionNamingServiceId($config))->setPublic(false);
52
53
        if ($config['logging']) {
54
            $container->register(LoggingInteractorDecorator::class)
55
                ->setDecoratedService(NewRelicInteractorInterface::class)
56
                ->setArguments(
57
                    [
58
                        '$interactor' => new Reference(LoggingInteractorDecorator::class.'.inner'),
59
                        '$logger' => new Reference('logger', ContainerInterface::NULL_ON_INVALID_REFERENCE),
60
                    ]
61
                )
62
                ->setPublic(false)
63
            ;
64
        }
65
66
        if (empty($config['deployment_names'])) {
67
            $config['deployment_names'] = \array_values(\array_filter(\explode(';', $config['application_name'] ?? '')));
68
        }
69
70
        $container->getDefinition(Config::class)
71
            ->setArguments(
72
                [
73
                    '$name' => $config['application_name'],
74
                    '$apiKey' => $config['api_key'],
75
                    '$licenseKey' => $config['license_key'],
76
                    '$xmit' => $config['xmit'],
77
                    '$deploymentNames' => $config['deployment_names'],
78
                ]
79
            );
80
81
        if ($config['http']['enabled']) {
82
            $loader->load('http_listener.xml');
83
            $container->getDefinition(RequestListener::class)
84
                ->setArguments(
85
                    [
86
                        '$ignoreRoutes' => $config['http']['ignored_routes'],
87
                        '$ignoredPaths' => $config['http']['ignored_paths'],
88
                        '$symfonyCache' => $config['http']['using_symfony_cache'],
89
                    ]
90
                );
91
92
            $container->getDefinition(ResponseListener::class)
93
                ->setArguments(
94
                    [
95
                        '$instrument' => $config['instrument'],
96
                        '$symfonyCache' => $config['http']['using_symfony_cache'],
97
                    ]
98
                );
99
        }
100
101
        if ($config['commands']['enabled']) {
102
            $loader->load('command_listener.xml');
103
            $container->getDefinition(CommandListener::class)
104
                ->setArguments(
105
                    [
106
                        '$ignoredCommands' => $config['commands']['ignored_commands'],
107
                    ]
108
                );
109
        }
110
111
        if ($config['exceptions']['enabled']) {
112
            $loader->load('exception_listener.xml');
113
        }
114
115
        if ($config['deprecations']['enabled']) {
116
            $loader->load('deprecation_listener.xml');
117
        }
118
119
        if ($config['twig']) {
120
            $loader->load('twig.xml');
121
        }
122
123
        if (\extension_loaded('newrelic') && $config['enabled'] && $config['monolog']['enabled']) {
124
            if (!\class_exists(\Monolog\Handler\NewRelicHandler::class)) {
125
                throw new \LogicException('The "symfony/monolog-bundle" package must be installed in order to use "monolog" option.');
126
            }
127
            $loader->load('monolog.xml');
128
            $container->setParameter('ekino.new_relic.monolog', $config['monolog'] ?? []);
129
            $container->setParameter('ekino.new_relic.application_name', $config['application_name']);
130
            $container->setAlias('ekino.new_relic.logs_handler', $config['monolog']['service'])->setPublic(false);
131
        }
132
    }
133
134
    private function getInteractorServiceId(array $config): string
135
    {
136
        if (!$config['enabled']) {
137
            return BlackholeInteractor::class;
138
        }
139
140
        if (!isset($config['interactor'])) {
141
            // Fallback on AdaptiveInteractor.
142
            return AdaptiveInteractor::class;
143
        }
144
145
        if ('auto' === $config['interactor']) {
146
            // Check if the extension is loaded or not
147
            return \extension_loaded('newrelic') ? NewRelicInteractor::class : BlackholeInteractor::class;
148
        }
149
150
        return $config['interactor'];
151
    }
152
153
    private function getTransactionNamingServiceId(array $config): string
154
    {
155
        switch ($config['http']['transaction_naming']) {
156
            case 'controller':
157
                return ControllerNamingStrategy::class;
158
            case 'route':
159
                return RouteNamingStrategy::class;
160
            case 'service':
161
                if (!isset($config['http']['transaction_naming_service'])) {
162
                    throw new \LogicException(
163
                        'When using the "service", transaction naming scheme, the "transaction_naming_service" config parameter must be set.'
164
                    );
165
                }
166
167
                return $config['http']['transaction_naming_service'];
168
            default:
169
                throw new \InvalidArgumentException(
170
                    \sprintf(
171
                        'Invalid transaction naming scheme "%s", must be "route", "controller" or "service".',
172
                        $config['http']['transaction_naming']
173
                    )
174
                );
175
        }
176
    }
177
}
178