Passed
Pull Request — master (#175)
by Jérémy
02:34
created

EkinoNewRelicExtension::getInteractorServiceId()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 12
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 12
rs 9.2
c 0
b 0
f 0
cc 4
eloc 5
nc 4
nop 1
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\NewRelic\NewRelicInteractorInterface;
24
use Ekino\NewRelicBundle\TransactionNamingStrategy\ControllerNamingStrategy;
25
use Ekino\NewRelicBundle\TransactionNamingStrategy\RouteNamingStrategy;
26
use Ekino\NewRelicBundle\TransactionNamingStrategy\TransactionNamingStrategyInterface;
27
use Symfony\Component\Config\FileLocator;
28
use Symfony\Component\DependencyInjection\ContainerBuilder;
29
use Symfony\Component\DependencyInjection\Loader;
30
use Symfony\Component\DependencyInjection\Reference;
31
use Symfony\Component\HttpKernel\DependencyInjection\Extension;
32
33
/**
34
 * This is the class that loads and manages your bundle configuration.
35
 *
36
 * To learn more see {@link http://symfony.com/doc/current/cookbook/bundles/extension.html}
37
 */
38
class EkinoNewRelicExtension extends Extension
39
{
40
    public function load(array $configs, ContainerBuilder $container): void
41
    {
42
        $configuration = new Configuration();
43
        $config = $this->processConfiguration($configuration, $configs);
44
45
        $loader = new Loader\XmlFileLoader($container, new FileLocator(__DIR__.'/../Resources/config'));
46
        $loader->load('services.xml');
47
48
        $container->setAlias(NewRelicInteractorInterface::class, $this->getInteractorServiceId($config));
49
        $container->setAlias(TransactionNamingStrategyInterface::class, $this->getTransactionNamingServiceId($config));
50
51
        if ($config['logging']) {
52
            $container->register(LoggingInteractorDecorator::class)
53
                 ->setDecoratedService(NewRelicInteractorInterface::class)
54
                ->setArguments(
55
                    [
56
                        '$interactor' => new Reference(LoggingInteractorDecorator::class.'.inner'),
57
                        '$logger' => new Reference('logger'),
58
                    ]
59
                )
60
                ->setPublic(false)
61
            ;
62
        }
63
64
        if (!empty($config['deployment_names'])) {
65
            $config['deployment_names'] = \array_values(\array_filter(\explode(';', $config['application_name'])));
66
        }
67
68
        $container->getDefinition(Config::class)
69
            ->setArguments(
70
                [
71
                    '$name' => $config['application_name'],
72
                    '$apiKey' => $config['api_key'],
73
                    '$licenseKey' => $config['license_key'],
74
                    '$xmit' => $config['xmit'],
75
                    '$deploymentNames' => $config['deployment_names'],
76
                ]
77
            );
78
79
        if ($config['http']['enabled']) {
80
            $loader->load('http_listener.xml');
81
            $container->getDefinition(RequestListener::class)
82
                ->setArguments(
83
                    [
84
                        '$ignoreRoutes' => $config['http']['ignored_routes'],
85
                        '$ignoredPaths' => $config['http']['ignored_paths'],
86
                        '$symfonyCache' => $config['http']['using_symfony_cache'],
87
                    ]
88
                );
89
90
            $container->getDefinition(ResponseListener::class)
91
                ->setArguments(
92
                    [
93
                        '$instrument' => $config['http']['instrument'],
94
                        '$symfonyCache' => $config['http']['using_symfony_cache'],
95
                    ]
96
                );
97
        }
98
99
        if ($config['commands']['enabled']) {
100
            $loader->load('command_listener.xml');
101
            $container->getDefinition(CommandListener::class)
102
                ->setArguments(
103
                    [
104
                        '$ignoredCommands' => $config['commands']['ignored_commands'],
105
                    ]
106
                );
107
        }
108
109
        if ($config['exceptions']['enabled']) {
110
            $loader->load('exception_listener.xml');
111
        }
112
113
        if ($config['deprecations']['enabled']) {
114
            $loader->load('deprecation_listener.xml');
115
        }
116
117
        if ($config['twig']) {
118
            $loader->load('twig.xml');
119
        }
120
121
        if (!$config['enabled']) {
122
            $config['monolog']['enabled'] = false;
123
        }
124
        if ($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.channels', $config['monolog']['channels']);
130
            $container->setAlias('ekino.new_relic.logs_handler', $config['monolog']['service']);
131
132
            $level = $config['monolog']['level'];
133
            // This service is used by MonologHandlerPass to inject into Monolog Service
134
            $container->findDefinition('ekino.new_relic.logs_handler')
135
                ->setArguments(
136
                    [
137
                        '$level' => \is_int($level) ? $level : \constant('Monolog\Logger::'.\strtoupper($level)),
138
                        '$appName' => $config['application_name'],
139
                    ]
140
                );
141
        }
142
    }
143
    private function getInteractorServiceId(array $config): string
144
    {
145
        if (!$config['enabled']) {
146
            return BlackholeInteractor::class;
147
        }
148
149
        if (isset($config['interactor'])) {
150
            return $config['interactor'];
151
        }
152
153
        // Fallback to see if the extension is loaded or not
154
        return \extension_loaded('newrelic') ? NewRelicInteractor::class : BlackholeInteractor::class;
155
    }
156
157
    private function getTransactionNamingServiceId(array $config): string
158
    {
159
        switch ($config['http']['transaction_naming']) {
160
            case 'controller':
161
                return ControllerNamingStrategy::class;
162
                break;
0 ignored issues
show
Unused Code introduced by
break is not strictly necessary here and could be removed.

The break statement is not necessary if it is preceded for example by a return statement:

switch ($x) {
    case 1:
        return 'foo';
        break; // This break is not necessary and can be left off.
}

If you would like to keep this construct to be consistent with other case statements, you can safely mark this issue as a false-positive.

Loading history...
163
            case 'route':
164
                return RouteNamingStrategy::class;
165
                break;
166
            case 'service':
167
                if (!isset($config['http']['transaction_naming_service'])) {
168
                    throw new \LogicException(
169
                        'When using the "service", transaction naming scheme, the "transaction_naming_service" config parameter must be set.'
170
                    );
171
                }
172
173
                return $config['http']['transaction_naming_service'];
174
                break;
175
            default:
176
                throw new \InvalidArgumentException(
177
                    \sprintf(
178
                        'Invalid transaction naming scheme "%s", must be "route", "controller" or "service".',
179
                        $config['http']['transaction_naming']
180
                    )
181
                );
182
        }
183
    }
184
}
185