MalformedInputExceptionListener   A
last analyzed

Complexity

Total Complexity 2

Size/Duplication

Total Lines 22
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 22
loc 22
ccs 0
cts 10
cp 0
rs 10
c 0
b 0
f 0

1 Method

Rating   Name   Duplication   Size   Complexity  
A onKernelException() 12 12 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 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