Passed
Push — master ( 3ee72e...29bd92 )
by Christophe
55:26 queued 21:34
created

RenderSubscriber::updateTwigLexer()   A

Complexity

Conditions 4
Paths 2

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 4

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 7
ccs 6
cts 6
cp 1
rs 9.2
cc 4
eloc 4
nc 2
nop 1
crap 4
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 Symfony\Component\HttpKernel\KernelEvents;
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 12
    public function __construct(LexerManager $twigLexerManager)
30
    {
31 12
        $this->twigLexerManager = $twigLexerManager;
32 12
    }
33
34
    /**
35
     * {@inheritdoc}
36
     */
37 1
    public static function getSubscribedEvents()
38
    {
39
        return [
40 1
            KernelEvents::REQUEST   => ['updateTwigLexer'],
41 1
            KernelEvents::RESPONSE  => ['rollbackTwigLexer'],
42 1
            KernelEvents::EXCEPTION => ['rollbackTwigLexerForException'],
43 1
        ];
44
    }
45
46
    /**
47
     * Set the custom twig lexer to display custom tags on the front
48
     *
49
     * @param GetResponseEvent $event
50
     */
51 3
    public function updateTwigLexer(GetResponseEvent $event)
52
    {
53 3
        if (false === $this->stopPropagation && $event->isMasterRequest() && !$event->getRequest()->isXmlHttpRequest()) {
54 1
            $this->twigLexerManager->updateLexer();
55 1
            $this->stopPropagation = true;
56 1
        }
57 3
    }
58
59
    /**
60
     * Rollback the custom twig lexer to display custom tags on the front
61
     *
62
     * @param FilterResponseEvent $event
63
     */
64 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...
65
    {
66 1
        $this->twigLexerManager->rollbackLexer();
67 1
        $this->stopPropagation = true;
68 1
    }
69
70
    /**
71
     * Rollback the custom twig lexer to display custom tags on the front
72
     *
73
     * @param GetResponseForExceptionEvent $event
74
     */
75 1
    public function rollbackTwigLexerForException(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 1
        $this->twigLexerManager->rollbackLexer();
78 1
        $this->stopPropagation = true;
79 1
    }
80
}
81