Completed
Pull Request — 2.x (#2161)
by Christian
03:47
created

TwigExceptionPass   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 27
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 2

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 11
lcom 0
cbo 2
dl 0
loc 27
ccs 12
cts 12
cp 1
rs 10
c 0
b 0
f 0

1 Method

Rating   Name   Duplication   Size   Complexity  
B process() 0 24 11
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
        // when no custom exception controller has been set
28 22
        if ($container->hasDefinition('fos_rest.error_listener') &&
29 22
            null === $container->getDefinition('fos_rest.error_listener')->getArgument(0)
30
        ) {
31 2
            if (isset($container->getParameter('kernel.bundles')['TwigBundle']) && ($container->has('templating.engine.twig') || $container->has('twig'))) {
32
                // only use this when TwigBundle is enabled and the deprecated SF templating integration is used
33 1
                $controller = Kernel::VERSION_ID >= 40100 ? 'fos_rest.exception.twig_controller::showAction' : 'fos_rest.exception.twig_controller:showAction';
34
            } else {
35 1
                $controller = Kernel::VERSION_ID >= 40100 ? 'fos_rest.exception.controller::showAction' : 'fos_rest.exception.controller:showAction';
36
            }
37
38 2
            $container->getDefinition('fos_rest.error_listener')->replaceArgument(0, $controller);
39
        }
40
41 22
        if (!$container->has('templating.engine.twig')) {
42 20
            if ($container->has('twig') && $container->has('fos_rest.exception.twig_controller')) {
43 5
                $container->findDefinition('fos_rest.exception.twig_controller')->replaceArgument(3, $container->findDefinition('twig'));
44
            } else {
45 15
                $container->removeDefinition('fos_rest.exception.twig_controller');
46
            }
47
        }
48 22
    }
49
}
50