Passed
Push — master ( 355586...94527c )
by Vincent
04:50
created

TransformerExceptionConstraintValidator   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 21
Duplicated Lines 0 %

Test Coverage

Coverage 90%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 5
eloc 10
c 1
b 0
f 0
dl 0
loc 21
ccs 9
cts 10
cp 0.9
rs 10

1 Method

Rating   Name   Duplication   Size   Complexity  
A validate() 0 16 5
1
<?php
2
3
namespace Bdf\Form\Validator;
4
5
use Symfony\Component\Validator\Constraint;
6
use Symfony\Component\Validator\ConstraintValidator;
7
use Symfony\Component\Validator\Exception\UnexpectedTypeException;
8
9
/**
10
 * @internal
11
 */
12
final class TransformerExceptionConstraintValidator extends ConstraintValidator
13
{
14
    /**
15
     * {@inheritdoc}
16
     */
17 21
    public function validate($value, Constraint $constraint)
18
    {
19 21
        if (!$constraint instanceof TransformerExceptionConstraint) {
20
            throw new UnexpectedTypeException($constraint, TransformerExceptionConstraint::class);
21
        }
22
23 21
        if ($constraint->validationCallback) {
24 1
            if (!($constraint->validationCallback)($value, $constraint, $this->context->getRoot())) {
25 1
                return;
26
            }
27
        }
28
29 21
        $this->context->buildViolation($constraint->message ?: $constraint->exception->getMessage())
30 21
            ->setCode($constraint->code)
31 21
            ->setParameter('{{ value }}', $this->formatValue($value))
32 21
            ->addViolation()
33
        ;
34 21
    }
35
}
36