Completed
Pull Request — master (#131)
by Łukasz
03:27
created

ValidationErrorViewFactory   A

Complexity

Total Complexity 2

Size/Duplication

Total Lines 18
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 1

Importance

Changes 0
Metric Value
wmc 2
c 0
b 0
f 0
lcom 0
cbo 1
dl 0
loc 18
rs 10

1 Method

Rating   Name   Duplication   Size   Complexity  
A create() 0 12 2
1
<?php
2
3
namespace Sylius\ShopApiPlugin\Factory;
4
5
use Sylius\ShopApiPlugin\View\ValidationErrorView;
6
use Symfony\Component\HttpFoundation\Response;
7
use Symfony\Component\Validator\ConstraintViolationInterface;
8
use Symfony\Component\Validator\ConstraintViolationListInterface;
9
10
final class ValidationErrorViewFactory implements ValidationErrorViewFactoryInterface
11
{
12
    /**
13
     * {@inheritdoc}
14
     */
15
    public function create(ConstraintViolationListInterface $validationResults)
16
    {
17
        $errorMessage = new ValidationErrorView();
18
        $errorMessage->code = Response::HTTP_BAD_REQUEST;
19
        $errorMessage->message = 'Validation failed';
20
        /** @var ConstraintViolationInterface $result */
21
        foreach ($validationResults as $result) {
22
            $errorMessage->errors[$result->getPropertyPath()][] = $result->getMessage();
23
        }
24
25
        return $errorMessage;
26
    }
27
}
28