Completed
Push — master ( 83404d...ab1330 )
by Rafał
14:45
created

ValidationExceptionListener::onKernelException()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 13
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 13
rs 9.4285
cc 2
eloc 7
nc 2
nop 1
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();
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