Completed
Push — master ( 92f6ed...8d4a88 )
by Joachim
04:27 queued 29s
created

ReportValidator::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 3
dl 0
loc 5
rs 10
c 1
b 0
f 0
cc 1
nc 1
nop 3
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Setono\SyliusStockMovementPlugin\Validator;
6
7
use Setono\SyliusStockMovementPlugin\Factory\ErrorFactoryInterface;
8
use Setono\SyliusStockMovementPlugin\Model\ReportInterface;
9
use Symfony\Component\Validator\Validator\ValidatorInterface;
10
11
final class ReportValidator implements ReportValidatorInterface
12
{
13
    /** @var ValidatorInterface */
14
    private $validator;
15
16
    /** @var ErrorFactoryInterface */
17
    private $errorFactory;
18
19
    /** @var array */
20
    private $validationGroups;
21
22
    public function __construct(ValidatorInterface $validator, ErrorFactoryInterface $errorFactory, array $validationGroups)
23
    {
24
        $this->validator = $validator;
25
        $this->errorFactory = $errorFactory;
26
        $this->validationGroups = $validationGroups;
27
    }
28
29
    public function validate(ReportInterface $report): void
30
    {
31
        $report->clearErrors();
32
        $report->setStatus(ReportInterface::STATUS_SUCCESS);
33
34
        $constraintViolationList = $this->validator->validate($report, null, $this->validationGroups); // todo make this validation group a parameter
35
        if ($constraintViolationList->count() > 0) {
36
            foreach ($constraintViolationList as $constraintViolation) {
37
                $error = $this->errorFactory->createFromConstraintViolation($constraintViolation);
38
                $report->addError($error);
39
            }
40
41
            $report->setStatus(ReportInterface::STATUS_ERROR);
42
        }
43
    }
44
}
45