Completed
Push — master ( dd95a4...bb9045 )
by Sergey
02:33
created

ResponseProvidingRequest::rules()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 6
rs 9.4285
cc 1
eloc 3
nc 1
nop 0
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 View Code Duplication
    public function getErrorResponse(ConstraintViolationListInterface $errors)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
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