ResponseProvidingRequest   A
last analyzed

Complexity

Total Complexity 2

Size/Duplication

Total Lines 25
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 5

Importance

Changes 0
Metric Value
wmc 2
lcom 0
cbo 5
dl 0
loc 25
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A rules() 0 6 1
A getErrorResponse() 0 12 1
1
<?php
2
3
namespace Fesor\RequestObject\Examples\Request;
4
5
use Fesor\RequestObject\ErrorResponseProvider;
6
use Fesor\RequestObject\RequestObject;
7
use Symfony\Component\HttpFoundation\JsonResponse;
8
use Symfony\Component\Validator\Constraints as Assert;
9
use Symfony\Component\Validator\ConstraintViolation;
10
use Symfony\Component\Validator\ConstraintViolationListInterface;
11
12
class ResponseProvidingRequest extends RequestObject implements ErrorResponseProvider
13
{
14
    public function rules()
15
    {
16
        return new Assert\Collection([
17
            'test' => new Assert\NotBlank(),
18
        ]);
19
    }
20
21
    /**
22
     * {@inheritdoc}
23
     */
24
    public function getErrorResponse(ConstraintViolationListInterface $errors)
25
    {
26
        return new JsonResponse([
27
            'message' => 'Please check your data',
28
            'errors' => array_map(function (ConstraintViolation $violation) {
29
                return [
30
                    'path' => $violation->getPropertyPath(),
31
                    'message' => $violation->getMessage(),
32
                ];
33
            }, iterator_to_array($errors)),
34
        ], 400);
35
    }
36
}
37