Completed
Push — 2.x ( 4535d1...92019f )
by Christian
06:10 queued 04:22
created

TwigExceptionPass   A

Complexity

Total Complexity 15

Size/Duplication

Total Lines 35
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 2

Test Coverage

Coverage 94.12%

Importance

Changes 0
Metric Value
wmc 15
lcom 0
cbo 2
dl 0
loc 35
ccs 16
cts 17
cp 0.9412
rs 10
c 0
b 0
f 0

1 Method

Rating   Name   Duplication   Size   Complexity  
C process() 0 32 15
1
<?php
2
3
/*
4
 * This file is part of the FOSRestBundle package.
5
 *
6
 * (c) FriendsOfSymfony <http://friendsofsymfony.github.com/>
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 FOS\RestBundle\DependencyInjection\Compiler;
13
14
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
15
use Symfony\Component\DependencyInjection\ContainerBuilder;
16
use Symfony\Component\HttpKernel\Kernel;
17
18
/**
19
 * Remove the 'fos_rest.exception.twig_controller' service if templating is not enabled and configure default exception controller.
20
 *
21
 * @internal
22
 */
23
final class TwigExceptionPass implements CompilerPassInterface
24
{
25 22
    public function process(ContainerBuilder $container)
26
    {
27 22
        if ($container->hasDefinition('fos_rest.exception.codes_map') // is config exception.enabled true
28 22
            && $container->hasParameter('twig.exception_listener.controller') // is twig-bundle 4.4 installed
29 22
            && $container->getParameter('twig.exception_listener.controller') // is twig-bundle deprecated controller set
30 22
            && !$container->hasDefinition('fos_rest.exception_listener') // is deprecated exception_listener disabled
31
        ) {
32
            throw new \InvalidArgumentException('You can not disable the "fos_rest.exception.exception_listener" and still have the "twig.exception_controller" enabled.');
33
        }
34
35
        // when no custom exception controller has been set
36 22
        if ($container->hasDefinition('fos_rest.error_listener') &&
37 22
            null === $container->getDefinition('fos_rest.error_listener')->getArgument(0)
38
        ) {
39 2
            if (isset($container->getParameter('kernel.bundles')['TwigBundle']) && ($container->has('templating.engine.twig') || $container->has('twig'))) {
40
                // only use this when TwigBundle is enabled and the deprecated SF templating integration is used
41 1
                $controller = Kernel::VERSION_ID >= 40100 ? 'fos_rest.exception.twig_controller::showAction' : 'fos_rest.exception.twig_controller:showAction';
42
            } else {
43 1
                $controller = Kernel::VERSION_ID >= 40100 ? 'fos_rest.exception.controller::showAction' : 'fos_rest.exception.controller:showAction';
44
            }
45
46 2
            $container->getDefinition('fos_rest.error_listener')->replaceArgument(0, $controller);
47
        }
48
49 22
        if (!$container->has('templating.engine.twig')) {
50 20
            if ($container->has('twig') && $container->has('fos_rest.exception.twig_controller')) {
51 4
                $container->findDefinition('fos_rest.exception.twig_controller')->replaceArgument(3, $container->findDefinition('twig'));
52
            } else {
53 16
                $container->removeDefinition('fos_rest.exception.twig_controller');
54
            }
55
        }
56 22
    }
57
}
58