Completed
Pull Request — master (#4)
by Adrien
02:50
created

RenderSubscriber::rollbackTwigLexer()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 5
rs 9.4285
cc 1
eloc 3
nc 1
nop 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
use Twig_Environment;
11
12
class RenderSubscriber implements EventSubscriberInterface
13
{
14
    /**
15
     * @var LexerManager
16
     */
17
    protected $twigLexerManager;
18
19
    /**
20
     * @var bool
21
     */
22
    protected $stopPropagation = false;
23
24
    /**
25
     * RenderSubscriber constructor.
26
     *
27
     * @param LexerManager $twigLexerManager
28
     */
29
    public function __construct(LexerManager $twigLexerManager)
30
    {
31
        $this->twigLexerManager = $twigLexerManager;
32
    }
33
34
    /**
35
     * {@inheritdoc}
36
     */
37
    public static function getSubscribedEvents()
38
    {
39
        return [
40
            'kernel.request'   => ['updateTwigLexer'],
41
            'kernel.response'  => ['rollbackTwigLexer'],
42
            'kernel.exception' => ['onKernelException'],
43
        ];
44
    }
45
46
    /**
47
     * Set the custom twig lexer to display custom tags on the front
48
     *
49
     * @param GetResponseEvent $event
50
     */
51
    public function updateTwigLexer(GetResponseEvent $event)
52
    {
53
        if (false === $this->stopPropagation && $event->isMasterRequest() && !$event->getRequest()->isXmlHttpRequest()) {
54
            $this->twigLexerManager->updateLexer();
55
            $this->stopPropagation = true;
56
        }
57
    }
58
59
    /**
60
     * Set the custom twig lexer to display custom tags on the front
61
     *
62
     * @param FilterResponseEvent $event
63
     */
64
    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...
65
    {
66
        $this->twigLexerManager->rollbackLexer();
67
        $this->stopPropagation = true;
68
    }
69
70
    /**
71
     * Set the default twig lexer to display default tags on exceptions
72
     *
73
     * @param GetResponseForExceptionEvent $event
74
     */
75
    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...
76
    {
77
        $this->twigLexerManager->rollbackLexer();
78
        $this->stopPropagation = true;
79
    }
80
}
81