Completed
Pull Request — master (#188)
by Kamil
03:23
created

ValidationErrorViewFactory::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
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
    /** @var string */
13
    private $validationErrorViewClass;
14
15
    public function __construct(string $validationErrorViewClass)
16
    {
17
        $this->validationErrorViewClass = $validationErrorViewClass;
18
    }
19
20
    /**
21
     * {@inheritdoc}
22
     */
23
    public function create(ConstraintViolationListInterface $validationResults): ValidationErrorView
24
    {
25
        /** @var ValidationErrorView $errorMessage */
26
        $errorMessage = new $this->validationErrorViewClass();
27
28
        $errorMessage->code = Response::HTTP_BAD_REQUEST;
29
        $errorMessage->message = 'Validation failed';
30
        /** @var ConstraintViolationInterface $result */
31
        foreach ($validationResults as $result) {
32
            $errorMessage->errors[$result->getPropertyPath()][] = $result->getMessage();
33
        }
34
35
        return $errorMessage;
36
    }
37
}
38