Completed
Push — master ( 0be8e1...585ca2 )
by Daniel
59:26 queued 46:02
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
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