Completed
Push — master ( 96a89d...d895c6 )
by Hannes
03:11
created

VisitorContainer::addVisitor()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
1
<?php
2
/**
3
 * This file is part of byrokrat\autogiro.
4
 *
5
 * byrokrat\autogiro is free software: you can redistribute it and/or
6
 * modify it under the terms of the GNU General Public License as published
7
 * by the Free Software Foundation, either version 3 of the License, or
8
 * (at your option) any later version.
9
 *
10
 * byrokrat\autogiro is distributed in the hope that it will be useful,
11
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13
 * GNU General Public License for more details.
14
 *
15
 * You should have received a copy of the GNU General Public License
16
 * along with byrokrat\autogiro. If not, see <http://www.gnu.org/licenses/>.
17
 *
18
 * Copyright 2016-18 Hannes Forsgård
19
 */
20
21
declare(strict_types = 1);
22
23
namespace byrokrat\autogiro\Visitor;
24
25
use byrokrat\autogiro\Tree\Node;
26
use byrokrat\autogiro\Exception\TreeException;
27
28
/**
29
 * Container for multiple visitors
30
 */
31
final class VisitorContainer extends Visitor
32
{
33
    use ErrorAwareTrait;
34
35
    /**
36
     * @var VisitorInterface[] Contained visitors
37
     */
38
    private $visitors = [];
39
40
    /**
41
     * Get contained visitors
42
     *
43
     * @return VisitorInterface[]
44
     */
45
    public function getVisitors(): array
46
    {
47
        return $this->visitors;
48
    }
49
50
    /**
51
     * Add a visitor to container
52
     */
53
    public function addVisitor(VisitorInterface $visitor): void
54
    {
55
        $this->visitors[] = $visitor;
56
    }
57
58
    /**
59
     * Delegate visit before to registered visitors
60
     */
61
    public function visitBefore(Node $node): void
62
    {
63
        parent::visitBefore($node);
64
65
        foreach ($this->visitors as $visitor) {
66
            $visitor->visitBefore($node);
67
        }
68
    }
69
70
    /**
71
     * Delegate visit after to registered visitors
72
     */
73
    public function visitAfter(Node $node): void
74
    {
75
        foreach ($this->visitors as $visitor) {
76
            $visitor->visitAfter($node);
77
        }
78
79
        parent::visitAfter($node);
80
    }
81
82
    /**
83
     * Reset the error container before a file node
84
     */
85
    public function beforeAutogiroFile(): void
86
    {
87
        $this->getErrorObject()->resetErrors();
88
    }
89
90
    /**
91
     * Throw exception if there are errors after iteration
92
     */
93
    public function afterAutogiroFile(): void
94
    {
95
        if ($this->getErrorObject()->hasErrors()) {
96
            throw new TreeException($this->getErrorObject()->getErrors());
97
        }
98
    }
99
}
100