createHandlerDefinition()   B
last analyzed

Complexity

Conditions 6
Paths 6

Size

Total Lines 60
Code Lines 42

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 28
CRAP Score 6

Importance

Changes 5
Bugs 0 Features 0
Metric Value
eloc 42
c 5
b 0
f 0
dl 0
loc 60
ccs 28
cts 28
cp 1
rs 8.6257
cc 6
nc 6
nop 2
crap 6

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
declare(strict_types=1);
4
5
namespace Assimtech\DislogBundle\DependencyInjection;
6
7
use Assimtech\Dislog;
8
use Assimtech\DislogBundle\Command;
9
use Symfony\Component\Config\FileLocator;
10
use Symfony\Component\DependencyInjection\ContainerBuilder;
11
use Symfony\Component\DependencyInjection\ContainerInterface;
12
use Symfony\Component\DependencyInjection\Exception\InvalidArgumentException;
13
use Symfony\Component\DependencyInjection\Loader\YamlFileLoader;
14
use Symfony\Component\DependencyInjection\Reference;
15
use Symfony\Component\HttpKernel\DependencyInjection\Extension;
16
17
class AssimtechDislogExtension extends Extension
18
{
19 4
    const API_CALL_FACTORY_ID = 'assimtech_dislog.api_call.factory';
20
    const HANDLER_ID = 'assimtech_dislog.handler';
21 4
    const LOGGER_ID = 'assimtech_dislog.logger';
22 4
23
    public function load(
24 4
        array $configs,
25 4
        ContainerBuilder $container
26
    ): void {
27
        $loader = new YamlFileLoader($container, new FileLocator(__DIR__ . '/../Resources/config'));
28 3
        $loader->load('services.yaml');
29 3
30
        $configuration = new Configuration();
31 3
        $config = $this->processConfiguration($configuration, $configs);
32
33 3
        $this
34
            ->createHandlerDefinition($config, $container)
35 3
            ->createLoggerDefinition($config, $container)
36 3
            ->configureCommands($config, $container)
37 3
        ;
38
    }
39
40 3
    protected function createHandlerDefinition(
41
        $config,
42 1
        ContainerBuilder $container
43 1
    ): self {
44 1
        $handlers = array_keys($config['handler']);
45 1
        $handlerType = $handlers[0];
46 1
        $handlerConfig = $config['handler'][$handlerType];
47
48
        switch ($handlerType) {
49 1
            case 'stream':
50 2
                $container
51
                    ->register(self::HANDLER_ID, Dislog\Handler\Stream::class)
52 1
                    ->setArguments([
53 1
                        $handlerConfig['resource'],
54 1
                        new Reference($handlerConfig['identity_generator']),
55
                        new Reference($handlerConfig['serializer']),
56
                    ])
57 1
                ;
58 1
                break;
59 1
            case 'doctrine_document_manager':
60 1
                $container
61 1
                    ->register(self::HANDLER_ID, Dislog\Handler\DoctrineDocumentManager::class)
62
                    ->setArguments([
63 1
                        new Reference($handlerConfig['document_manager']),
64
                    ])
65
                ;
66 3
                break;
67
            case 'doctrine_entity_manager':
68 3
                $container
69
                    ->register(self::HANDLER_ID, Dislog\Handler\DoctrineEntityManager::class)
70
                    ->setArguments([
71 3
                        new Reference($handlerConfig['entity_manager']),
72
                    ])
73
                ;
74 3
                break;
75 3
            case 'doctrine_object_manager':
76 3
                \trigger_error(
77 3
                    'DoctrineObjectManager is deprecated, use DoctrineDocumentManager or DoctrineEntityManager instead',
78 3
                    E_USER_DEPRECATED
79 3
                );
80
                $container
81
                    ->register(self::HANDLER_ID, Dislog\Handler\DoctrineObjectManager::class)
82
                    ->setArguments([
83 3
                        new Reference($handlerConfig['object_manager']),
84
                    ])
85 3
                ;
86
                break;
87
            case 'service':
88
                $container->setAlias(
89
                    self::HANDLER_ID,
90
                    $handlerConfig['name']
91
                );
92
                break;
93
            default:
94
                throw new InvalidArgumentException('Unsupported handler type: ' . $handlerType);
95
        }
96
97
        $container->setAlias(Dislog\Handler\HandlerInterface::class, self::HANDLER_ID);
98
99
        return $this;
100
    }
101
102
    protected function createLoggerDefinition(
103
        $config,
104
        ContainerBuilder $container
105
    ): self {
106
        $container
107
            ->register(self::LOGGER_ID, Dislog\ApiCallLogger::class)
108
            ->setArguments([
109
                new Reference(self::API_CALL_FACTORY_ID),
110
                new Reference(self::HANDLER_ID),
111
                $config['preferences'],
112
                new Reference($config['psr_logger'], ContainerInterface::IGNORE_ON_INVALID_REFERENCE),
113
            ])
114
        ;
115
116
        $container->setAlias(Dislog\ApiCallLoggerInterface::class, self::LOGGER_ID);
117
118
        return $this;
119
    }
120
121
    protected function configureCommands(
122
        $config,
123
        ContainerBuilder $container
124
    ): self {
125
        $container->getDefinition('assimtech_dislog.command.remove')
126
            ->setArgument('$maxAge', $config['max_age'])
127
        ;
128
129
        return $this;
130
    }
131
}
132