Completed
Pull Request — master (#173)
by Jérémy
02:35
created

getTransactionNamingServiceId()   B

Complexity

Conditions 5
Paths 5

Size

Total Lines 23
Code Lines 18

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 23
rs 8.5906
c 0
b 0
f 0
eloc 18
nc 5
nop 1
cc 5
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\PhpFileLoader($container, new FileLocator(__DIR__.'/../Resources/config'));
46
        $loader->load('services.php');
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
                ->setArgument('$interactor', new Reference(LoggingInteractorDecorator::class.'.inner'))
55
                ->setPublic(false)
56
            ;
57
        }
58
59
        if (!empty($config['deployment_names'])) {
60
            $config['deployment_names'] = \array_values(\array_filter(\explode(';', $config['application_name'])));
61
        }
62
63
        $container->getDefinition(Config::class)
64
            ->setArguments([
65
                '$name' => $config['application_name'],
66
                '$apiKey' => $config['api_key'],
67
                '$licenseKey' => $config['license_key'],
68
                '$xmit' => $config['xmit'],
69
                '$deploymentNames' => $config['deployment_names'],
70
            ]);
71
72
        if ($config['http']['enabled']) {
73
            $loader->load('http_listener.php');
74
            $container->getDefinition(RequestListener::class)
75
                ->setArguments([
76
                    '$ignoreRoutes' => $config['http']['ignored_routes'],
77
                    '$ignoredPaths' => $config['http']['ignored_paths'],
78
                    '$symfonyCache' => $config['http']['using_symfony_cache'],
79
                ]);
80
81
            $container->getDefinition(ResponseListener::class)
82
                ->setArguments([
83
                    '$instrument' => $config['http']['instrument'],
84
                    '$symfonyCache' => $config['http']['using_symfony_cache'],
85
                ]);
86
        }
87
88
        if ($config['commands']['enabled']) {
89
            $loader->load('command_listener.php');
90
            $container->getDefinition(CommandListener::class)
91
                ->setArguments([
92
                    '$ignoredCommands' => $config['commands']['ignored_commands'],
93
                ]);
94
        }
95
96
        if ($config['exceptions']['enabled']) {
97
            $loader->load('exception_listener.php');
98
        }
99
100
        if ($config['deprecations']['enabled']) {
101
            $loader->load('deprecation_listener.php');
102
        }
103
104
        if ($config['twig']) {
105
            $loader->load('twig.php');
106
        }
107
108
        if (!$config['enabled']) {
109
            $config['monolog']['enabled'] = false;
110
        }
111
        if ($config['monolog']['enabled']) {
112
            if (!\class_exists(\Monolog\Handler\NewRelicHandler::class)) {
113
                throw new \LogicException('The "symfony/monolog-bundle" package must be installed in order to use "monolog" option.');
114
            }
115
            $loader->load('monolog.php');
116
            $container->setParameter('ekino.new_relic.monolog.channels', $config['monolog']['channels']);
117
            $container->setAlias('ekino.new_relic.logs_handler', $config['monolog']['service']);
118
119
            $level = $config['monolog']['level'];
120
121
            // This service is used by MonologHandlerPass to inject into Monolog Service
122
            $container->findDefinition('ekino.new_relic.logs_handler')
123
                ->setArguments([
124
                    '$level' => \is_int($level) ? $level : \constant('Monolog\Logger::'.\strtoupper($level)),
125
                    '$appName' => $config['application_name'],
126
                ]);
127
        }
128
    }
129
130
    private function getInteractorServiceId(array $config): string
131
    {
132
        if (!$config['enabled']) {
133
            return BlackholeInteractor::class;
134
        } elseif (isset($config['interactor'])) {
135
            return $config['interactor'];
136
        } else {
137
            // Fallback to see if the extension is loaded or not
138
            return \extension_loaded('newrelic') ? NewRelicInteractor::class : BlackholeInteractor::class;
139
        }
140
    }
141
    private function getTransactionNamingServiceId(array $config): string
142
    {
143
        switch ($config['http']['transaction_naming']) {
144
            case 'controller':
145
                return ControllerNamingStrategy::class;
146
                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...
147
            case 'route':
148
                return RouteNamingStrategy::class;
149
                break;
150
            case 'service':
151
                if (!isset($config['http']['transaction_naming_service'])) {
152
                    throw new \LogicException(
153
                        'When using the "service", transaction naming scheme, the "transaction_naming_service" config parameter must be set.'
154
                    );
155
                }
156
157
                return $config['http']['transaction_naming_service'];
158
                break;
159
            default:
160
                throw new \InvalidArgumentException(
161
                    \sprintf(
162
                        'Invalid transaction naming scheme "%s", must be "route", "controller" or "service".',
163
                        $config['http']['transaction_naming']
164
                    )
165
                );
166
        }
167
    }
168
}
169