Passed
Branch feature/debugToolbar (8daf09)
by Christophe
16:35 queued 13:03
created

RenderSubscriber   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 69
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 100%

Importance

Changes 3
Bugs 1 Features 1
Metric Value
wmc 8
c 3
b 1
f 1
lcom 1
cbo 3
dl 0
loc 69
ccs 22
cts 22
cp 1
rs 10

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A getSubscribedEvents() 0 8 1
A updateTwigLexer() 0 7 4
A rollbackTwigLexer() 0 5 1
A onKernelException() 0 5 1
1
<?php
2
3
namespace Chris\Bundle\FrontRenderBundle\Subscriber;
4
5
use Chris\Bundle\FrontRenderBundle\Twig\LexerManager;
6
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
7
use Symfony\Component\HttpKernel\Event\FilterResponseEvent;
8
use Symfony\Component\HttpKernel\Event\GetResponseEvent;
9
use Symfony\Component\HttpKernel\Event\GetResponseForExceptionEvent;
10
11
class RenderSubscriber implements EventSubscriberInterface
12
{
13
    /**
14
     * @var LexerManager
15
     */
16
    protected $twigLexerManager;
17
18
    /**
19
     * @var bool
20
     */
21
    protected $stopPropagation = false;
22
23
    /**
24
     * RenderSubscriber constructor.
25
     *
26
     * @param LexerManager $twigLexerManager
27
     */
28 12
    public function __construct(LexerManager $twigLexerManager)
29
    {
30 12
        $this->twigLexerManager = $twigLexerManager;
31 12
    }
32
33
    /**
34
     * {@inheritdoc}
35
     */
36 1
    public static function getSubscribedEvents()
37
    {
38
        return [
39 1
            'kernel.request'   => ['updateTwigLexer'],
40 1
            'kernel.response'  => ['rollbackTwigLexer'],
41 1
            'kernel.exception' => ['onKernelException'],
42 1
        ];
43
    }
44
45
    /**
46
     * Set the custom twig lexer to display custom tags on the front
47
     *
48
     * @param GetResponseEvent $event
49
     */
50 3
    public function updateTwigLexer(GetResponseEvent $event)
51
    {
52 3
        if (false === $this->stopPropagation && $event->isMasterRequest() && !$event->getRequest()->isXmlHttpRequest()) {
53 1
            $this->twigLexerManager->updateLexer();
54 1
            $this->stopPropagation = true;
55 1
        }
56 3
    }
57
58
    /**
59
     * Set the custom twig lexer to display custom tags on the front
60
     *
61
     * @param FilterResponseEvent $event
62
     */
63 1
    public function rollbackTwigLexer(FilterResponseEvent $event)
0 ignored issues
show
Unused Code introduced by
The parameter $event is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
64
    {
65 1
        $this->twigLexerManager->rollbackLexer();
66 1
        $this->stopPropagation = true;
67 1
    }
68
69
    /**
70
     * Set the default twig lexer to display default tags on exceptions
71
     *
72
     * @param GetResponseForExceptionEvent $event
73
     */
74 1
    public function onKernelException(GetResponseForExceptionEvent $event)
0 ignored issues
show
Unused Code introduced by
The parameter $event is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
75
    {
76 1
        $this->twigLexerManager->rollbackLexer();
77 1
        $this->stopPropagation = true;
78 1
    }
79
}
80