Completed
Pull Request — master (#70)
by Christoffer
02:06
created

ParallelVisitor::enterNode()   B

Complexity

Conditions 5
Paths 5

Size

Total Lines 22
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

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