ExceptionListener::onKernelException()   B
last analyzed

Complexity

Conditions 4
Paths 4

Size

Total Lines 30
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
dl 0
loc 30
rs 8.5806
c 1
b 0
f 1
cc 4
eloc 16
nc 4
nop 1
1
<?php
2
3
namespace Loevgaard\DandomainAltapayBundle\EventListener;
4
5
use Loevgaard\DandomainAltapayBundle\Exception\Exception;
6
use Loevgaard\DandomainAltapayBundle\Exception\PaymentException;
7
use Symfony\Bundle\FrameworkBundle\Templating\EngineInterface;
8
use Symfony\Component\HttpFoundation\Response;
9
use Symfony\Component\HttpKernel\Event\GetResponseForExceptionEvent;
10
11
/**
12
 * This exception listener will intercept exceptions throw from this bundle and redirect the user
13
 * to an error page describing the error.
14
 */
15
class ExceptionListener
16
{
17
    /**
18
     * @var EngineInterface
19
     */
20
    private $engine;
21
22
    public function __construct(EngineInterface $engine)
23
    {
24
        $this->engine = $engine;
25
    }
26
27
    public function onKernelException(GetResponseForExceptionEvent $event)
28
    {
29
        $exception = $event->getException();
30
31
        // check if exception is part of this bundle
32
        if (!($exception instanceof Exception)) {
33
            return false;
34
        }
35
36
        $redirect = '';
37
38
        if ($exception instanceof PaymentException) {
39
            $redirect = $exception->getPayment()->getReferrer();
40
            if (!$redirect) {
41
                $redirect = 'http://'.$exception->getPayment()->getCallBackServerUrl();
42
            }
43
        }
44
45
        $responseText = $this->engine->render('@LoevgaardDandomainAltapay/error/error.html.twig', [
46
            'error' => $exception->getMessage(),
47
            'redirect' => $redirect,
48
        ]);
49
50
        $response = new Response();
51
        $response->setContent($responseText);
52
53
        $event->setResponse($response);
54
55
        return true;
56
    }
57
}
58