InstanceOfConstraintValidator::validate()   B
last analyzed

Complexity

Conditions 5
Paths 5

Size

Total Lines 23
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 30

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 23
ccs 0
cts 20
cp 0
rs 8.5906
cc 5
eloc 13
nc 5
nop 2
crap 30
1
<?php
2
3
namespace Majora\Framework\Validation\Constraint;
4
5
use Majora\Framework\Validation\Constraint\InstanceOfConstraint;
6
use Symfony\Component\Validator\Constraint;
7
use Symfony\Component\Validator\ConstraintValidator;
8
use Symfony\Component\Validator\Exception\UnexpectedTypeException;
9
10
/**
11
 * Validator for InstanceOfConstraint
12
 */
13
class InstanceOfConstraintValidator extends ConstraintValidator
14
{
15
    /**
16
     * {@inheritdoc}
17
     */
18
    public function validate($object, Constraint $constraint)
19
    {
20
        if (!$constraint instanceof InstanceOfConstraint) {
21
            throw new UnexpectedTypeException($constraint, 'Majora\Framework\Validation\Constraint\InstanceOfConstraint');
22
        }
23
24
        if (null === $object) {
25
            return;
26
        }
27
28
        if (!is_object($object)) {
29
            throw new UnexpectedTypeException($object, 'object');
30
        }
31
32
        if (!is_a($object, $constraint->class)) {
33
            $this->context
34
                ->buildViolation($constraint->message)
35
                    ->setParameter('{{ object_class }}', $this->formatValue(get_class($object)))
36
                    ->setParameter('{{ tested_class }}', $this->formatValue($constraint->class))
37
                ->addViolation()
38
            ;
39
        }
40
    }
41
}
42