NijensOpenapiExtension   A
last analyzed

Complexity

Total Complexity 10

Size/Duplication

Total Lines 62
Duplicated Lines 0 %

Importance

Changes 3
Bugs 0 Features 0
Metric Value
eloc 25
dl 0
loc 62
rs 10
c 3
b 0
f 0
wmc 10

5 Methods

Rating   Name   Duplication   Size   Complexity  
A registerValidationConfiguration() 0 8 3
A registerRoutingConfiguration() 0 4 1
A load() 0 17 3
A loadDeprecatedServices() 0 3 1
A registerExceptionHandlingConfiguration() 0 11 2
1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * This file is part of the OpenapiBundle package.
7
 *
8
 * (c) Niels Nijens <[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 Nijens\OpenapiBundle\DependencyInjection;
15
16
use Nijens\OpenapiBundle\ExceptionHandling\EventSubscriber\ProblemExceptionToJsonResponseSubscriber;
17
use Nijens\OpenapiBundle\ExceptionHandling\EventSubscriber\ThrowableToProblemExceptionSubscriber;
18
use Nijens\OpenapiBundle\ExceptionHandling\ThrowableToProblemExceptionTransformer;
19
use Nijens\OpenapiBundle\Routing\RouteLoader;
20
use Nijens\OpenapiBundle\Validation\EventSubscriber\RequestValidationSubscriber;
21
use Nijens\OpenapiBundle\Validation\RequestValidator\RequestParameterValidator;
22
use Symfony\Component\Config\FileLocator;
23
use Symfony\Component\DependencyInjection\ContainerBuilder;
24
use Symfony\Component\DependencyInjection\Extension\Extension;
25
use Symfony\Component\DependencyInjection\Loader\XmlFileLoader;
26
27
/**
28
 * Loads and manages the bundle configuration and services.
29
 *
30
 * @author Niels Nijens <[email protected]>
31
 */
32
class NijensOpenapiExtension extends Extension
33
{
34
    /**
35
     * {@inheritdoc}
36
     */
37
    public function load(array $configs, ContainerBuilder $container): void
38
    {
39
        $loader = new XmlFileLoader($container, new FileLocator(__DIR__.'/../Resources/config'));
40
        $loader->load('services.xml');
41
42
        $this->loadDeprecatedServices($loader);
43
44
        $configuration = new Configuration();
45
        $config = $this->processConfiguration($configuration, $configs);
46
47
        $this->registerRoutingConfiguration($config['routing'], $container);
48
        $this->registerValidationConfiguration($config['validation'], $container);
49
        $this->registerExceptionHandlingConfiguration($config['exception_handling'], $container);
50
51
        if ($config['validation']['enabled'] === true && $config['exception_handling']['enabled'] !== true) {
52
            trigger_error(
53
                'Enabling the validation component without the exception handling component might cause unexpected results.',
54
            );
55
        }
56
    }
57
58
    /**
59
     * Loads the deprecated services file.
60
     */
61
    private function loadDeprecatedServices(XmlFileLoader $loader): void
62
    {
63
        $loader->load('services_deprecated.xml');
64
    }
65
66
    private function registerRoutingConfiguration(array $config, ContainerBuilder $container): void
67
    {
68
        $definition = $container->getDefinition(RouteLoader::class);
69
        $definition->replaceArgument(2, $config['operation_id_as_route_name']);
70
    }
71
72
    private function registerValidationConfiguration(array $config, ContainerBuilder $container): void
73
    {
74
        if ($config['enabled'] !== true) {
75
            $container->removeDefinition(RequestValidationSubscriber::class);
76
        }
77
78
        if ($config['parameter_validation'] === false) {
79
            $container->removeDefinition(RequestParameterValidator::class);
80
        }
81
    }
82
83
    private function registerExceptionHandlingConfiguration(array $config, ContainerBuilder $container): void
84
    {
85
        $definition = $container->getDefinition(ThrowableToProblemExceptionTransformer::class);
86
        $definition->replaceArgument(
87
            0,
88
            array_replace_recursive(Configuration::DEFAULT_EXCEPTION_HANDLING_EXCEPTIONS, $config['exceptions'])
89
        );
90
91
        if ($config['enabled'] !== true) {
92
            $container->removeDefinition(ThrowableToProblemExceptionSubscriber::class);
93
            $container->removeDefinition(ProblemExceptionToJsonResponseSubscriber::class);
94
        }
95
    }
96
}
97