ParentValidator   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 29
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
eloc 13
c 1
b 0
f 1
dl 0
loc 29
rs 10
wmc 4

1 Method

Rating   Name   Duplication   Size   Complexity  
A validate() 0 21 4
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