Completed
Push — master ( 419f51...37bf86 )
by Michał
08:39
created

MonologSentryExtension   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 86
Duplicated Lines 0 %

Coupling/Cohesion

Dependencies 5

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 9
cbo 5
dl 0
loc 86
rs 10
c 0
b 0
f 0
ccs 0
cts 58
cp 0

1 Method

Rating   Name   Duplication   Size   Complexity  
C load() 0 77 9
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Dziki\MonologSentryBundle\DependencyInjection;
6
7
use Dziki\MonologSentryBundle\Processor\TagAppending;
8
use Dziki\MonologSentryBundle\SubscribedProcessor\BrowserDataAppending;
9
use Dziki\MonologSentryBundle\SubscribedProcessor\UserDataAppending;
10
use Dziki\MonologSentryBundle\UserAgent\CachedParser;
11
use Dziki\MonologSentryBundle\UserAgent\NativeParser;
12
use Dziki\MonologSentryBundle\UserAgent\PhpUserAgentParser;
13
use Symfony\Component\DependencyInjection\ContainerBuilder;
14
use Symfony\Component\DependencyInjection\Definition;
15
use Symfony\Component\DependencyInjection\Extension\Extension;
16
use Symfony\Component\DependencyInjection\Reference;
17
use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
18
19
class MonologSentryExtension extends Extension
20
{
21
22
    /**
23
     * Loads a specific configuration.
24
     *
25
     * @throws \InvalidArgumentException When provided tag is not defined in this extension
26
     */
27
    public function load(array $configs, ContainerBuilder $container): void
28
    {
29
        $configs = $this->processConfiguration(new Configuration(), $configs);
0 ignored issues
show
Coding Style introduced by
Consider using a different name than the parameter $configs. This often makes code more readable.
Loading history...
30
31
        if ($configs['user_context']) {
32
            $container->setDefinition(
33
                'dziki.monolog_sentry_bundle.user_data_appending_subscribed_processor',
34
                new Definition(UserDataAppending::class, [new Reference(TokenStorageInterface::class)])
35
            )
36
                      ->addTag('kernel.event_subscriber')
37
                      ->addTag('monolog.processor')
38
            ;
39
        }
40
41
        if ($configs['user_agent_parser']) {
42
            $handlerName = $configs['user_agent_parser'];
43
            switch ($handlerName) {
44
                case 'native':
45
                    $parserClass = NativeParser::class;
46
                    $container->setDefinition($parserClass, new Definition($parserClass))
47
                              ->setPrivate(true)
48
                    ;
49
                    break;
50
                case 'phpuseragent':
51
                    $parserClass = PhpUserAgentParser::class;
52
                    $container->setDefinition($parserClass, new Definition($parserClass))
53
                              ->setPrivate(true)
54
                    ;
55
                    break;
56
                default:
57
                    $parserClass = $handlerName;
58
            }
59
60
            if ($configs['cache']) {
61
                $container->setDefinition(
62
                    CachedParser::class,
63
                    new Definition(
64
                        CachedParser::class,
65
                        [
66
                            new Reference($configs['cache']),
67
                            new Reference($parserClass),
68
                        ]
69
                    )
70
                );
71
72
                $parserClass = CachedParser::class;
73
            }
74
75
            $container->setDefinition(
76
                'dziki.monolog_sentry_bundle.browser_data_appending_subscribed_processor',
77
                new Definition(BrowserDataAppending::class, [new Reference($parserClass)])
78
            )
79
                      ->setPrivate(true)
80
                      ->addTag('kernel.event_subscriber')
81
                      ->addTag('monolog.processor')
82
            ;
83
        }
84
85
        if (\is_array($configs['tags'])) {
86
            foreach ($configs['tags'] as $tag => ['value' => $value, 'name' => $name]) {
87
                $tagName = $name ?: (string)$tag;
88
                $container->setDefinition(
89
                    "dziki.monolog_sentry_bundle.{$tag}_appending_processor",
90
                    new Definition(
91
                        TagAppending::class,
92
                        [
93
                            $tagName,
94
                            $value,
95
                        ]
96
                    )
97
                )
98
                          ->setPrivate(true)
99
                          ->addTag('monolog.processor')
100
                ;
101
            }
102
        }
103
    }
104
}
105