Completed
Pull Request — master (#34)
by Christoffer
02:28
created

ParallelVisitor::leaveNode()   B

Complexity

Conditions 5
Paths 5

Size

Total Lines 22
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 5
eloc 11
nc 5
nop 4
dl 0
loc 22
rs 8.6737
c 0
b 0
f 0
1
<?php
2
3
namespace Digia\GraphQL\Language\AST\Visitor;
4
5
use Digia\GraphQL\Language\AST\Node\NodeInterface;
6
use Digia\GraphQL\Util\SerializationInterface;
7
8
class ParallelVisitor implements VisitorInterface
9
{
10
    /**
11
     * @var array|VisitorInterface[]
12
     */
13
    protected $visitors;
14
15
    /**
16
     * @var array
17
     */
18
    protected $_skipping = [];
19
20
    /**
21
     * ParallelVisitor constructor.
22
     * @param array|VisitorInterface[] $visitors
23
     */
24
    public function __construct($visitors)
25
    {
26
        $this->visitors = $visitors;
27
    }
28
29
    /**
30
     * @param NodeInterface|AcceptVisitorTrait $node
31
     * @param string|int|null                  $key
32
     * @param NodeInterface|null               $parent
33
     * @param array                            $path
34
     * @return NodeInterface|SerializationInterface|null
35
     * @throws VisitorBreak
36
     */
37
    public function enterNode(
38
        NodeInterface $node,
39
        $key = null,
40
        ?NodeInterface $parent = null,
41
        array $path = []
42
    ): ?NodeInterface {
43
        $newNode = null;
44
45
        foreach ($this->visitors as $i => $visitor) {
46
            if (!isset($this->_skipping[$i])) {
47
                try {
48
                    $newNode = $visitor->enterNode($node, $key, $parent, $path);
49
                } catch (VisitorBreak $break) {
50
                    $this->_skipping[$i] = $break;
51
                    continue;
52
                }
53
54
                if (null === $newNode) {
55
                    $this->_skipping[$i] = $node;
56
57
                    $newNode = $node;
58
                }
59
            }
60
        }
61
62
        return $newNode;
63
    }
64
65
    /**
66
     * @param NodeInterface|AcceptVisitorTrait $node
67
     * @param string|int|null                  $key
68
     * @param NodeInterface|null               $parent
69
     * @param array                            $path
70
     * @return NodeInterface|SerializationInterface|null
71
     * @throws VisitorBreak
72
     */
73
    public function leaveNode(
74
        NodeInterface $node,
75
        $key = null,
76
        ?NodeInterface $parent = null,
77
        array $path = []
78
    ): ?NodeInterface {
79
        $newNode = null;
80
81
        foreach ($this->visitors as $i => $visitor) {
82
            if (!isset($this->_skipping[$i])) {
83
                try {
84
                    $newNode = $visitor->leaveNode($node, $key, $parent, $path);
85
                } catch (VisitorBreak $break) {
86
                    $this->_skipping[$i] = $break;
87
                    continue;
88
                }
89
            } elseif ($this->_skipping[$i] === $node) {
90
                unset($this->_skipping[$i]);
91
            }
92
        }
93
94
        return $newNode;
95
    }
96
}
97