onKernelException()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 17

Duplication

Lines 17
Ratio 100 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
dl 17
loc 17
ccs 0
cts 12
cp 0
rs 9.7
c 0
b 0
f 0
cc 2
nc 2
nop 1
crap 6
1
<?php
2
/**
3
 * Listener for deserialization exceptions
4
 */
5
6
namespace Graviton\ExceptionBundle\Listener;
7
8
use Symfony\Component\HttpKernel\Event\GetResponseForExceptionEvent;
9
use Graviton\ExceptionBundle\Exception\DeserializationException;
10
use Symfony\Component\HttpFoundation\Response;
11
12
/**
13
 * Listener for deserialization exceptions
14
 *
15
 * @author   List of contributors <https://github.com/libgraviton/graviton/graphs/contributors>
16
 * @license  https://opensource.org/licenses/MIT MIT License
17
 * @link     http://swisscom.ch
18
 */
19 View Code Duplication
class DeserializationExceptionListener extends RestExceptionListener
0 ignored issues
show
Duplication introduced by
This class seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
20
{
21
    /**
22
     * Handle the exception and send the right response
23
     *
24
     * @param GetResponseForExceptionEvent $event Event
25
     *
26
     * @return void
27
     */
28
    public function onKernelException(GetResponseForExceptionEvent $event)
29
    {
30
        // var_dump does not work here... use
31
        //    \Doctrine\Common\Util\Debug::dump($e);die;
32
        if (($exception = $event->getException()) instanceof DeserializationException) {
33
            // hnmm.. no way to find out which property (name) failed??
34
            $msg = array('message' => $exception->getPrevious()->getMessage());
35
36
            $response = $exception->getResponse();
37
            $response->setStatusCode(Response::HTTP_BAD_REQUEST);
38
            $response->setContent(
39
                $this->getSerializedContent($msg)
40
            );
41
42
            $event->setResponse($response);
43
        }
44
    }
45
}
46