RedirectExceptionSubscriber   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 24
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 9
dl 0
loc 24
rs 10
c 0
b 0
f 0
wmc 4

2 Methods

Rating   Name   Duplication   Size   Complexity  
A getSubscribedEvents() 0 4 1
A Redirect() 0 10 3
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
}