RedirectExceptionSubscriber::Redirect()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 10
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 5
nc 3
nop 1
dl 0
loc 10
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace App\EventSubscriber\Exception;
4
5
use App\Exception\RedirectException;
6
use App\FlashMessage\AddFlashTrait;
7
use App\FlashMessage\FlashMessageCategory;
8
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
9
use Symfony\Component\HttpFoundation\RedirectResponse;
10
use Symfony\Component\HttpKernel\Event\GetResponseForExceptionEvent;
11
use Symfony\Component\HttpKernel\KernelEvents;
12
13
14
class RedirectExceptionSubscriber implements EventSubscriberInterface
15
{
16
    use AddFlashTrait;
17
18
    public static function getSubscribedEvents()
19
    {
20
        return [
21
            KernelEvents::EXCEPTION => 'Redirect'
22
        ];
23
    }
24
25
    /**
26
     * @param GetResponseForExceptionEvent $event
27
     */
28
    public function Redirect(GetResponseForExceptionEvent $event)
29
    {
30
        if ($event->getException() instanceof RedirectException) {
31
            $redirectResponse = new RedirectResponse($event->getException()->getRedirectResponse());
32
33
            if($event->getException()->getMessage() !== ""){
34
                $this->addFlashMessage(FlashMessageCategory::ERROR, $event->getException()->getMessage());
35
            }
36
37
            $event->setResponse($redirectResponse);
38
        }
39
    }
40
41
}