DeserializationExceptionListener   A
last analyzed

Complexity

Total Complexity 2

Size/Duplication

Total Lines 27
Duplicated Lines 100 %

Coupling/Cohesion

Components 0
Dependencies 4

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 2
lcom 0
cbo 4
dl 27
loc 27
ccs 0
cts 12
cp 0
rs 10
c 0
b 0
f 0

1 Method

Rating   Name   Duplication   Size   Complexity  
A onKernelException() 17 17 2

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

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