MonologHandlerPass   A
last analyzed

Complexity

Total Complexity 16

Size/Duplication

Total Lines 49
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 16
eloc 30
dl 0
loc 49
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
C process() 0 30 12
A getChannels() 0 14 4
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\Compiler;
15
16
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
17
use Symfony\Component\DependencyInjection\ContainerBuilder;
18
use Symfony\Component\DependencyInjection\Exception\InvalidArgumentException;
19
use Symfony\Component\DependencyInjection\Reference;
20
21
class MonologHandlerPass implements CompilerPassInterface
22
{
23
    public function process(ContainerBuilder $container): void
24
    {
25
        if (!$container->hasParameter('ekino.new_relic.monolog') || !$container->hasDefinition('monolog.logger')) {
26
            return;
27
        }
28
29
        $configuration = $container->getParameter('ekino.new_relic.monolog');
30
        if ($container->hasDefinition('ekino.new_relic.logs_handler') && $container->hasParameter('ekino.new_relic.application_name')) {
31
            $container->findDefinition('ekino.new_relic.logs_handler')
32
                ->setArgument('$level', \is_int($configuration['level']) ? $configuration['level'] : \constant('Monolog\Logger::'.\strtoupper($configuration['level'])))
33
                ->setArgument('$bubble', true)
34
                ->setArgument('$appName', $container->getParameter('ekino.new_relic.application_name'));
35
        }
36
37
        if (!isset($configuration['channels'])) {
38
            $channels = $this->getChannels($container);
39
        } elseif ('inclusive' === $configuration['channels']['type']) {
40
            $channels = $configuration['channels']['elements'] ?: $this->getChannels($container);
41
        } else {
42
            $channels = \array_diff($this->getChannels($container), $configuration['channels']['elements']);
43
        }
44
45
        foreach ($channels as $channel) {
46
            try {
47
                $def = $container->getDefinition('app' === $channel ? 'monolog.logger' : 'monolog.logger.'.$channel);
48
            } catch (InvalidArgumentException $e) {
49
                $msg = 'NewRelicBundle configuration error: The logging channel "'.$channel.'" does not exist.';
50
                throw new \InvalidArgumentException($msg, 0, $e);
51
            }
52
            $def->addMethodCall('pushHandler', [new Reference('ekino.new_relic.logs_handler')]);
53
        }
54
    }
55
56
    private function getChannels(ContainerBuilder $container)
57
    {
58
        $channels = [];
59
        foreach ($container->getDefinitions() as $id => $definition) {
60
            if ('monolog.logger' === $id) {
61
                $channels[] = 'app';
62
                continue;
63
            }
64
            if (0 === \strpos($id, 'monolog.logger.')) {
65
                $channels[] = \substr($id, 15);
66
            }
67
        }
68
69
        return $channels;
70
    }
71
}
72