onKernelException()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 19

Duplication

Lines 19
Ratio 100 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
dl 19
loc 19
ccs 0
cts 16
cp 0
rs 9.6333
c 0
b 0
f 0
cc 2
nc 2
nop 1
crap 6
1
<?php
2
/**
3
 * Listener for invalid JSON Patch 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\InvalidJsonPatchException;
11
12
/**
13
 * Listener for invalid JSON Patch 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 InvalidJsonPatchExceptionListener 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 InvalidJsonPatchException) {
0 ignored issues
show
Coding Style introduced by
Blank line found at start of control structure
Loading history...
31
32
            // Set status code and content
33
            $response = new Response();
34
            $response
35
                ->setStatusCode(Response::HTTP_BAD_REQUEST)
36
                ->setContent(
37
                    $this->getSerializedContent(
38
                        [
39
                            'message' => sprintf('Invalid JSON patch request: %s', $exception->getMessage())
40
                        ]
41
                    )
42
                );
43
44
            $event->setResponse($response);
45
        }
46
    }
47
}
48