EkinoNewRelicExtension::load()   D
last analyzed

Complexity

Conditions 11
Paths 384

Size

Total Lines 90
Code Lines 52

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 52
c 1
b 0
f 0
dl 0
loc 90
rs 4.3939
cc 11
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
                    '$apiHost' => $config['api_host'],
79
                ]
80
            );
81
82
        if ($config['http']['enabled']) {
83
            $loader->load('http_listener.xml');
84
            $container->getDefinition(RequestListener::class)
85
                ->setArguments(
86
                    [
87
                        '$ignoreRoutes' => $config['http']['ignored_routes'],
88
                        '$ignoredPaths' => $config['http']['ignored_paths'],
89
                        '$symfonyCache' => $config['http']['using_symfony_cache'],
90
                    ]
91
                );
92
93
            $container->getDefinition(ResponseListener::class)
94
                ->setArguments(
95
                    [
96
                        '$instrument' => $config['instrument'],
97
                        '$symfonyCache' => $config['http']['using_symfony_cache'],
98
                    ]
99
                );
100
        }
101
102
        if ($config['commands']['enabled']) {
103
            $loader->load('command_listener.xml');
104
            $container->getDefinition(CommandListener::class)
105
                ->setArguments(
106
                    [
107
                        '$ignoredCommands' => $config['commands']['ignored_commands'],
108
                    ]
109
                );
110
        }
111
112
        if ($config['exceptions']['enabled']) {
113
            $loader->load('exception_listener.xml');
114
        }
115
116
        if ($config['deprecations']['enabled']) {
117
            $loader->load('deprecation_listener.xml');
118
        }
119
120
        if ($config['twig']) {
121
            $loader->load('twig.xml');
122
        }
123
124
        if ($config['enabled'] && $config['monolog']['enabled']) {
125
            if (!\class_exists(\Monolog\Handler\NewRelicHandler::class)) {
126
                throw new \LogicException('The "symfony/monolog-bundle" package must be installed in order to use "monolog" option.');
127
            }
128
            $loader->load('monolog.xml');
129
            $container->setParameter('ekino.new_relic.monolog', $config['monolog'] ?? []);
130
            $container->setParameter('ekino.new_relic.application_name', $config['application_name']);
131
            $container->setAlias('ekino.new_relic.logs_handler', $config['monolog']['service'])->setPublic(false);
132
        }
133
    }
134
135
    private function getInteractorServiceId(array $config): string
136
    {
137
        if (!$config['enabled']) {
138
            return BlackholeInteractor::class;
139
        }
140
141
        if (!isset($config['interactor'])) {
142
            // Fallback on AdaptiveInteractor.
143
            return AdaptiveInteractor::class;
144
        }
145
146
        if ('auto' === $config['interactor']) {
147
            // Check if the extension is loaded or not
148
            return \extension_loaded('newrelic') ? NewRelicInteractor::class : BlackholeInteractor::class;
149
        }
150
151
        return $config['interactor'];
152
    }
153
154
    private function getTransactionNamingServiceId(array $config): string
155
    {
156
        switch ($config['http']['transaction_naming']) {
157
            case 'controller':
158
                return ControllerNamingStrategy::class;
159
            case 'route':
160
                return RouteNamingStrategy::class;
161
            case 'service':
162
                if (!isset($config['http']['transaction_naming_service'])) {
163
                    throw new \LogicException('When using the "service", transaction naming scheme, the "transaction_naming_service" config parameter must be set.');
164
                }
165
166
                return $config['http']['transaction_naming_service'];
167
            default:
168
                throw new \InvalidArgumentException(\sprintf('Invalid transaction naming scheme "%s", must be "route", "controller" or "service".', $config['http']['transaction_naming']));
169
        }
170
    }
171
}
172