onException()   A
last analyzed

Complexity

Conditions 4
Paths 6

Size

Total Lines 19

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 19
rs 9.6333
c 0
b 0
f 0
cc 4
nc 6
nop 1
1
<?php
2
namespace  WhiteOctober\PagerfantaBundle\EventListener;
3
4
use Pagerfanta\Exception\NotValidMaxPerPageException;
5
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
6
use Symfony\Component\HttpKernel\Event\GetResponseForExceptionEvent;
7
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
8
use Symfony\Component\HttpKernel\KernelEvents;
9
10
class ConvertNotValidMaxPerPageToNotFoundListener implements EventSubscriberInterface
11
{
12
    /**
13
     * @param GetResponseForExceptionEvent $event
14
     */
15
    public function onException(GetResponseForExceptionEvent $event)
16
    {
17
        if (method_exists($event, 'getThrowable')) {
18
            $throwable = $event->getThrowable();
19
        } else {
20
            // Support for Symfony 4.3 and before
21
            $throwable = $event->getException();
0 ignored issues
show
Deprecated Code introduced by
The method Symfony\Component\HttpKe...onEvent::getException() has been deprecated with message: since Symfony 4.4, use getThrowable instead

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
22
        }
23
24
        if ($throwable instanceof NotValidMaxPerPageException) {
25
            $notFoundHttpException = new NotFoundHttpException('Page Not Found', $throwable);
26
            if (method_exists($event, 'setThrowable')) {
27
                $event->setThrowable($notFoundHttpException);
28
            } else {
29
                // Support for Symfony 4.3 and before
30
                $event->setException($notFoundHttpException);
0 ignored issues
show
Deprecated Code introduced by
The method Symfony\Component\HttpKe...onEvent::setException() has been deprecated with message: since Symfony 4.4, use setThrowable instead

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
31
            }
32
        }
33
    }
34
35
    /**
36
     * {@inheritDoc}
37
     */
38
    public static function getSubscribedEvents()
39
    {
40
        return array(
41
            KernelEvents::EXCEPTION => array('onException', 512)
42
        );
43
    }
44
}
45