ValidationExceptionListener   A
last analyzed

Complexity

Total Complexity 3

Size/Duplication

Total Lines 31
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 5

Importance

Changes 0
Metric Value
wmc 3
lcom 1
cbo 5
dl 0
loc 31
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A onKernelException() 0 13 2
1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * This file is part of the Superdesk Web Publisher Bridge Bundle.
7
 *
8
 * Copyright 2017 Sourcefabric z.ú. and contributors.
9
 *
10
 * For the full copyright and license information, please see the
11
 * AUTHORS and LICENSE files distributed with this source code.
12
 *
13
 * @copyright 2017 Sourcefabric z.ú
14
 * @license http://www.superdesk.org/license
15
 */
16
17
namespace SWP\Bundle\BridgeBundle\EventListener;
18
19
use SWP\Bundle\BridgeBundle\Exception\ValidationException;
20
use SWP\Component\Common\Serializer\SerializerInterface;
21
use Symfony\Component\HttpFoundation\Response;
22
use Symfony\Component\HttpKernel\Event\GetResponseForExceptionEvent;
23
24
final class ValidationExceptionListener
25
{
26
    /**
27
     * @var SerializerInterface
28
     */
29
    private $serializer;
30
31
    /**
32
     * ValidationExceptionListener constructor.
33
     *
34
     * @param SerializerInterface $serializer
35
     */
36
    public function __construct(SerializerInterface $serializer)
37
    {
38
        $this->serializer = $serializer;
39
    }
40
41
    public function onKernelException(GetResponseForExceptionEvent $event)
42
    {
43
        $exception = $event->getException();
0 ignored issues
show
Deprecated Code introduced by
The method Symfony\Component\HttpKe...onEvent::getException() has been deprecated with message: since Symfony 4.4, use getThrowable instead

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
44
45
        if (!$exception instanceof ValidationException) {
46
            return;
47
        }
48
49
        $event->setResponse(new Response(
50
            $this->serializer->serialize($exception->getConstraintViolationList(), $event->getRequest()->getRequestFormat()),
51
            Response::HTTP_BAD_REQUEST
52
        ));
53
    }
54
}
55