onKernelException()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 12

Duplication

Lines 12
Ratio 100 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
dl 12
loc 12
ccs 0
cts 10
cp 0
rs 9.8666
c 0
b 0
f 0
cc 2
nc 2
nop 1
crap 6
1
<?php
2
/**
3
 * Listener for no input exceptions
4
 */
5
6
namespace Graviton\ExceptionBundle\Listener;
7
8
use Symfony\Component\HttpKernel\Event\GetResponseForExceptionEvent;
9
use Symfony\Component\HttpFoundation\Response;
10
use Graviton\ExceptionBundle\Exception\MalformedInputException;
11
12
/**
13
 * Listener for no input 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 MalformedInputExceptionListener 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
        if (($exception = $event->getException()) instanceof MalformedInputException) {
31
            $msg = array("message" => "Bad Request - " . $exception->getMessage());
32
33
            $response = $exception->getResponse()
34
                ->setStatusCode(Response::HTTP_BAD_REQUEST)
35
                ->setContent($this->getSerializedContent($msg));
36
37
            $event->setResponse($response);
38
        }
39
    }
40
}
41