Completed
Push — master ( 9d4828...f71b73 )
by Hannes
02:28
created

VisitorContainer   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 73
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Importance

Changes 0
Metric Value
wmc 10
lcom 1
cbo 4
dl 0
loc 73
rs 10
c 0
b 0
f 0

7 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A getVisitors() 0 4 1
A addVisitor() 0 4 1
A visitBefore() 0 8 2
A visitAfter() 0 8 2
A beforeFileNode() 0 4 1
A afterFileNode() 0 6 2
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\ContentException;
27
28
/**
29
 * Container for multiple visitors
30
 */
31
class VisitorContainer extends ErrorAwareVisitor
32
{
33
    /**
34
     * @var VisitorInterface[] Contained visitors
35
     */
36
    private $visitors;
37
38
    public function __construct(ErrorObject $errorObj, VisitorInterface ...$visitors)
39
    {
40
        parent::__construct($errorObj);
41
        $this->visitors = $visitors;
42
    }
43
44
    /**
45
     * Get contained visitors
46
     *
47
     * @return VisitorInterface[]
48
     */
49
    public function getVisitors(): array
50
    {
51
        return $this->visitors;
52
    }
53
54
    /**
55
     * Add a visitor to container
56
     */
57
    public function addVisitor(VisitorInterface $visitor): void
58
    {
59
        $this->visitors[] = $visitor;
60
    }
61
62
    /**
63
     * Delegate visit before to registered visitors
64
     */
65
    public function visitBefore(Node $node): void
66
    {
67
        parent::visitBefore($node);
68
69
        foreach ($this->visitors as $visitor) {
70
            $visitor->visitBefore($node);
71
        }
72
    }
73
74
    /**
75
     * Delegate visit after to registered visitors
76
     */
77
    public function visitAfter(Node $node): void
78
    {
79
        foreach ($this->visitors as $visitor) {
80
            $visitor->visitAfter($node);
81
        }
82
83
        parent::visitAfter($node);
84
    }
85
86
    /**
87
     * Reset the error container before a file node
88
     */
89
    public function beforeFileNode(): void
90
    {
91
        $this->getErrorObject()->resetErrors();
92
    }
93
94
    /**
95
     * Throw exception if there are errors after iteration
96
     */
97
    public function afterFileNode(): void
98
    {
99
        if ($this->getErrorObject()->hasErrors()) {
100
            throw new ContentException($this->getErrorObject()->getErrors());
101
        }
102
    }
103
}
104