Completed
Push — master ( d7ece8...443c1d )
by Valentyn
12:07
created

BaseRequest::getErrorResponse()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 14
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 14
ccs 0
cts 9
cp 0
rs 9.4285
cc 1
eloc 10
nc 1
nop 1
crap 2
1
<?php
2
3
namespace App\Request;
4
5
use Fesor\RequestObject\RequestObject;
6
use Symfony\Component\HttpFoundation\JsonResponse;
7
use Fesor\RequestObject\ErrorResponseProvider;
8
use Symfony\Component\Validator\ConstraintViolationListInterface;
9
use Symfony\Component\Validator\ConstraintViolation;
10
use Symfony\Component\PropertyAccess\PropertyAccess;
11
12
class BaseRequest extends RequestObject implements ErrorResponseProvider
13
{
14
    /**
15
     * {@inheritdoc}
16
     */
17
    public function getErrorResponse(ConstraintViolationListInterface $errors)
18
    {
19
        $propertyAccessor = PropertyAccess::createPropertyAccessor();
20
21
        return new JsonResponse([
22
            'message' => 'Please check your data',
23
            'errors' => array_map(function (ConstraintViolation $violation) use ($propertyAccessor) {
24
                return [
25
                    'path' => $violation->getPropertyPath(),
26
                    'message' => $violation->getMessage(),
27
                ];
28
            }, iterator_to_array($errors)),
29
        ], 400);
30
    }
31
}