Completed
Push — ezp26352-skip_csrf_check_on_re... ( 19f37a )
by
unknown
36:29
created

ValueObjectVisitorDispatcher::visit()   B

Complexity

Conditions 6
Paths 9

Size

Total Lines 25
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 6
eloc 15
nc 9
nop 3
dl 0
loc 25
rs 8.439
c 0
b 0
f 0
1
<?php
2
3
/**
4
 * File containing the ValueObjectVisitorDispatcher class.
5
 *
6
 * @copyright Copyright (C) eZ Systems AS. All rights reserved.
7
 * @license For full copyright and license information view LICENSE file distributed with this source code.
8
 *
9
 * @version //autogentag//
10
 */
11
namespace eZ\Publish\Core\REST\Common\Output;
12
13
use eZ\Publish\Core\REST\Common\Output\Generator as OutputGenerator;
14
15
/**
16
 * Dispatches value objects to a visitor depending on the class name.
17
 */
18
class ValueObjectVisitorDispatcher
19
{
20
    /**
21
     * @var ValueObjectVisitor[]
22
     */
23
    private $visitors;
24
25
    /**
26
     * @var \eZ\Publish\Core\REST\Common\Output\Visitor
27
     */
28
    private $outputVisitor;
29
30
    /**
31
     * @var \eZ\Publish\Core\REST\Common\Output\Generator
32
     */
33
    private $outputGenerator;
34
35
    public function setOutputVisitor(Visitor $outputVisitor)
36
    {
37
        $this->outputVisitor = $outputVisitor;
38
    }
39
40
    public function setOutputGenerator(Generator $outputGenerator)
41
    {
42
        $this->outputGenerator = $outputGenerator;
43
    }
44
45
    /**
46
     * @param string $visitedClassName The FQN of the visited class
47
     * @param \eZ\Publish\Core\REST\Common\Output\ValueObjectVisitor $visitor The visitor object
48
     */
49
    public function addVisitor($visitedClassName, ValueObjectVisitor $visitor)
50
    {
51
        $this->visitors[$visitedClassName] = $visitor;
52
    }
53
54
    /**
55
     * @param object $data The visited object
56
     *
57
     * @throws \eZ\Publish\Core\REST\Common\Output\Exceptions\NoVisitorFoundException
58
     * @throws \eZ\Publish\Core\REST\Common\Output\Exceptions\InvalidTypeException
59
     *
60
     * @return mixed
61
     */
62
    public function visit($data, Generator $generator = null, Visitor $visitor = null)
63
    {
64
        if (!is_object($data)) {
65
            throw new Exceptions\InvalidTypeException($data);
66
        }
67
        $checkedClassNames = array();
68
69
        if ($visitor === null) {
70
            $visitor = $this->outputVisitor;
71
        }
72
73
        if ($generator === null) {
74
            $generator = $this->outputGenerator;
75
        }
76
77
        $className = get_class($data);
78
        do {
79
            $checkedClassNames[] = $className;
80
            if (isset($this->visitors[$className])) {
81
                return $this->visitors[$className]->visit($visitor, $generator, $data);
82
            }
83
        } while ($className = get_parent_class($className));
84
85
        throw new Exceptions\NoVisitorFoundException($checkedClassNames);
86
    }
87
}
88