Completed
Push — ezp26352-skip_csrf_check_on_re... ( 19f37a...6abe82 )
by
unknown
79:54 queued 33:48
created

ValueObjectVisitorDispatcher   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 62
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 0
Metric Value
dl 0
loc 62
rs 10
c 0
b 0
f 0
wmc 7
lcom 1
cbo 3

4 Methods

Rating   Name   Duplication   Size   Complexity  
A setOutputVisitor() 0 4 1
A setOutputGenerator() 0 4 1
A addVisitor() 0 4 1
A visit() 0 17 4
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)
63
    {
64
        if (!is_object($data)) {
65
            throw new Exceptions\InvalidTypeException($data);
66
        }
67
        $checkedClassNames = array();
68
69
        $className = get_class($data);
70
        do {
71
            $checkedClassNames[] = $className;
72
            if (isset($this->visitors[$className])) {
73
                return $this->visitors[$className]->visit($this->outputVisitor, $this->outputGenerator, $data);
74
            }
75
        } while ($className = get_parent_class($className));
76
77
        throw new Exceptions\NoVisitorFoundException($checkedClassNames);
78
    }
79
}
80