ParentValidator::validate()   A
last analyzed

Complexity

Conditions 4
Paths 4

Size

Total Lines 21
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 4
eloc 12
c 1
b 0
f 1
nc 4
nop 2
dl 0
loc 21
rs 9.8666
1
<?php
2
/**
3
 * @author Muhammed Akbulut <[email protected]>
4
 * @copyright Zicht Online <http://www.zicht.nl>
5
 */
6
7
namespace Zicht\Bundle\FrameworkExtraBundle\Validator;
8
9
use Symfony\Component\Validator\Context\ExecutionContextInterface;
10
11
/**
12
 * Class ParentValidator
13
 *
14
 * @package Zicht\Bundle\FrameworkExtraBundle\Validator
15
 */
16
class ParentValidator
17
{
18
    /**
19
     * Validates parents of a MenuItem
20
     *
21
     * @param object $object
22
     * @param ExecutionContextInterface $context
23
     */
24
    public static function validate($object, ExecutionContextInterface $context)
25
    {
26
        if (!method_exists($object, 'getParent')) {
27
            return;
28
        }
29
30
        $tempObject = $object->getParent();
31
32
        while ($tempObject !== null) {
33
            if ($tempObject === $object) {
34
                $context->buildViolation(
35
                    'Circular reference error. '
36
                    . 'An object can not reference with a parent to itself nor to an ancestor of itself'
37
                )
38
                    ->atPath('parent')
39
                    ->addViolation();
40
41
                break;
42
            }
43
44
            $tempObject = $tempObject->getParent();
45
        }
46
    }
47
}
48