Passed
Push — develop ( 96281a...e6f82b )
by Daniel
05:53 queued 26s
created

FormHandlerClassValidator   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 35
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
eloc 21
dl 0
loc 35
ccs 0
cts 20
cp 0
rs 10
c 0
b 0
f 0
wmc 5

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A validate() 0 25 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(
30
                    ' It should implement %s or tagged %s',
31
                    [
32
                        FormHandlerInterface::class,
33
                        'silverback_api_component.form_handler'
34
                    ]
35
                );
36
                $this->context
37
                    ->buildViolation($constraint->message . $conditionsStr)
38
                    ->setParameter('{{ string }}', $value)
39
                    ->addViolation();
40
            }
41
        } catch (InvalidArgumentException $exception) {
42
            $this->context
43
                ->buildViolation($constraint->message . ' ' . $exception->getMessage())
44
                ->setParameter('{{ string }}', $value)
45
                ->addViolation();
46
        }
47
    }
48
}
49