Passed
Pull Request — master (#155)
by Tobias
02:14
created

getTransactionNamingService()   B

Complexity

Conditions 5
Paths 5

Size

Total Lines 28
Code Lines 19

Duplication

Lines 0
Ratio 0 %

Importance

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