Completed
Pull Request — master (#17)
by Valentyn
13:24
created

BaseRequest   A

Complexity

Total Complexity 1

Size/Duplication

Total Lines 21
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 3

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 1
lcom 0
cbo 3
dl 0
loc 21
ccs 8
cts 8
cp 1
rs 10
c 1
b 0
f 0

1 Method

Rating   Name   Duplication   Size   Complexity  
A getErrorResponse() 0 15 1
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
11
//todo How to inject TranslatorInterface ?
12
class BaseRequest extends RequestObject implements ErrorResponseProvider
13
{
14
    /**
15
     * {@inheritdoc}
16
     */
17 2
    public function getErrorResponse(ConstraintViolationListInterface $errors)
18
    {
19 2
        return new JsonResponse([
20 2
            'message' => 'Validation error. Please check your data.', //todo translate this message
21 2
            'errors' => array_map(function (ConstraintViolation $violation) {
22
                // todo find the way to show correct path to property.
23
                // Assert\* will return path like "[registration][username]"
24
                // But UniqueEntity will return path like "username"
25
                return [
26 2
                    'path' => $violation->getPropertyPath(),
27 2
                    'message' => $violation->getMessage(),
28
                ];
29 2
            }, iterator_to_array($errors)),
30 2
        ], 400);
31
    }
32
}