Completed
Push — master ( b08bdd...3448b0 )
by Adrian
55:15 queued 49:20
created

ValidationExceptionListener   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 64
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 6

Test Coverage

Coverage 0%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 7
c 1
b 0
f 0
lcom 1
cbo 6
dl 0
loc 64
ccs 0
cts 32
cp 0
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A setConstraintUtils() 0 4 1
A onKernelException() 0 15 2
A getErrorMessages() 0 15 4
1
<?php
2
/**
3
 * Listener for validation exceptions
4
 */
5
6
namespace Graviton\ExceptionBundle\Listener;
7
8
use Graviton\JsonSchemaBundle\Exception\ValidationException;
9
use Graviton\JsonSchemaBundle\Exception\ValidationExceptionError;
10
use Graviton\SchemaBundle\Constraint\ConstraintUtils;
11
use JsonSchema\Entity\JsonPointer;
12
use Symfony\Component\HttpKernel\Event\GetResponseForExceptionEvent;
13
use Symfony\Component\HttpFoundation\Response;
14
15
/**
16
 * Listener for validation exceptions
17
 *
18
 * @author   List of contributors <https://github.com/libgraviton/graviton/graphs/contributors>
19
 * @license  http://opensource.org/licenses/gpl-license.php GNU Public License
20
 * @link     http://swisscom.ch
21
 */
22
class ValidationExceptionListener extends RestExceptionListener
23
{
24
25
    /**
26
     * @var ConstraintUtils
27
     */
28
    private $constraintUtils;
29
30
    /**
31
     * set constraint utils
32
     *
33
     * @param ConstraintUtils $utils utils
34
     *
35
     * @return void
36
     */
37
    public function setConstraintUtils(ConstraintUtils $utils)
38
    {
39
        $this->constraintUtils = $utils;
40
    }
41
42
    /**
43
     * Handle the exception and send the right response
44
     *
45
     * @param GetResponseForExceptionEvent $event Event
46
     *
47
     * @return void
48
     */
49
    public function onKernelException(GetResponseForExceptionEvent $event)
50
    {
51
        if (($exception = $event->getException()) instanceof ValidationException) {
52
            $content = $this->getErrorMessages($exception->getErrors());
53
            // Set status code and content
54
            $response = new Response();
55
            $response
56
                ->setStatusCode(Response::HTTP_BAD_REQUEST)
57
                ->setContent(
58
                    $this->getSerializedContent($content)
59
                );
60
61
            $event->setResponse($response);
62
        }
63
    }
64
65
    /**
66
     * @param ValidationExceptionError[] $errors errors
67
     *
68
     * @return array
69
     */
70
    private function getErrorMessages(array $errors)
71
    {
72
        $content = [];
73
        foreach ($errors as $error) {
74
            $property = $error->getProperty();
75
            if ($property instanceof JsonPointer && $this->constraintUtils instanceof ConstraintUtils) {
76
                $property = $this->constraintUtils->getNormalizedPathFromPointer($property);
77
            }
78
            $content[] = [
79
                'propertyPath' => $property,
80
                'message' => $error->getMessage(),
81
            ];
82
        }
83
        return $content;
84
    }
85
}
86