Failed Conditions
Pull Request — master (#188)
by Kamil
04:26
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
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