InstanceOfConstraintValidator   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 29
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 5

Test Coverage

Coverage 0%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 5
c 1
b 0
f 0
lcom 1
cbo 5
dl 0
loc 29
ccs 0
cts 20
cp 0
rs 10

1 Method

Rating   Name   Duplication   Size   Complexity  
B validate() 0 23 5
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