Completed
Push — master ( 0be8e1...585ca2 )
by Daniel
59:26 queued 46:02
created

FormHandlerClassValidator::validate()   A

Complexity

Conditions 4
Paths 6

Size

Total Lines 25
Code Lines 18

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 20

Importance

Changes 0
Metric Value
cc 4
eloc 18
nc 6
nop 2
dl 0
loc 25
ccs 0
cts 17
cp 0
crap 20
rs 9.6666
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Silverback\ApiComponentBundle\Validator\Constraints;
6
7
use Silverback\ApiComponentBundle\Form\Handler\FormHandlerInterface;
8
use Silverback\ApiComponentBundle\Validator\ClassNameValidator;
9
use Symfony\Component\Validator\Constraint;
10
use Symfony\Component\Validator\ConstraintValidator;
11
use Symfony\Component\Validator\Exception\InvalidArgumentException;
12
13
class FormHandlerClassValidator extends ConstraintValidator
14
{
15
    private $formHandlers;
16
17
    public function __construct(
18
        iterable $formHandlers
19
    ) {
20
        $this->formHandlers = $formHandlers;
21
    }
22
23
    public function validate($value, Constraint $constraint)
24
    {
25
        if (null === $value) {
26
            return;
27
        }
28
        try {
29
            $valid = ClassNameValidator::validate($value, $this->formHandlers);
30
            if (!$valid) {
31
                $conditionsStr = vsprintf(
32
                    ' It should implement %s or tagged %s',
33
                    [
34
                        FormHandlerInterface::class,
35
                        'silverback_api_component.form_handler'
36
                    ]
37
                );
38
                $this->context
39
                    ->buildViolation($constraint->message . $conditionsStr)
40
                    ->setParameter('{{ string }}', $value)
41
                    ->addViolation();
42
            }
43
        } catch (InvalidArgumentException $exception) {
44
            $this->context
45
                ->buildViolation($constraint->message . ' ' . $exception->getMessage())
46
                ->setParameter('{{ string }}', $value)
47
                ->addViolation();
48
        }
49
    }
50
}
51