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

validate()   A

Complexity

Conditions 5
Paths 4

Size

Total Lines 16
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 5.025

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 9
c 1
b 0
f 0
dl 0
loc 16
ccs 9
cts 10
cp 0.9
rs 9.6111
cc 5
nc 4
nop 2
crap 5.025
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