Test Failed
Branch develop (ac2838)
by Daniel
09:09
created

FormHandlerClassValidator   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 33
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
dl 0
loc 33
rs 10
c 0
b 0
f 0
wmc 5

2 Methods

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