Completed
Pull Request — master (#290)
by
unknown
41:51
created

ValidationErrorViewFactory   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 28
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 0
Metric Value
wmc 3
lcom 1
cbo 1
dl 0
loc 28
rs 10
c 0
b 0
f 0

2 Methods

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