Completed
Push — master ( 7c1eaa...80d841 )
by Joachim
15:53
created

ExceptionListener   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 39
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 5

Importance

Changes 1
Bugs 0 Features 1
Metric Value
wmc 5
lcom 0
cbo 5
dl 0
loc 39
c 1
b 0
f 1
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
B onKernelException() 0 26 4
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
            $redirect = '';
34
35
            if ($exception instanceof PaymentException) {
36
                $redirect = $exception->getPayment()->getReferrer();
37
                if (!$redirect) {
38
                    $redirect = 'http://'.$exception->getPayment()->getCallBackServerUrl();
39
                }
40
            }
41
42
            $responseText = $this->engine->render('@LoevgaardDandomainAltapay/error/error.html.twig', [
43
                'error' => $exception->getMessage(),
44
                'redirect' => $redirect,
45
            ]);
46
47
            $response = new Response();
48
            $response->setContent($responseText);
49
50
            $event->setResponse($response);
51
        }
52
    }
53
}
54