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

TwigExceptionPass::process()   C

Complexity

Conditions 15
Paths 16

Size

Total Lines 32

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 16
CRAP Score 15.0457

Importance

Changes 0
Metric Value
dl 0
loc 32
ccs 16
cts 17
cp 0.9412
rs 5.9166
c 0
b 0
f 0
cc 15
nc 16
nop 1
crap 15.0457

How to fix   Complexity   

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
/*
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