Completed
Pull Request — master (#131)
by Łukasz
02:49
created

ValidationErrorViewFactory::create()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 12
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 12
rs 9.4285
cc 2
eloc 7
nc 2
nop 1
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