Completed
Pull Request — master (#7)
by Fabrice
02:29
created

FlowInterruptAbstract::breakFlow()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 1
1
<?php
2
3
/*
4
 * This file is part of NodalFlow.
5
 *     (c) Fabrice de Stefanis / https://github.com/fab2s/NodalFlow
6
 * This source file is licensed under the MIT license which you will
7
 * find in the LICENSE file or at https://opensource.org/licenses/MIT
8
 */
9
10
namespace fab2s\NodalFlow\Flows;
11
12
use fab2s\NodalFlow\NodalFlowException;
13
use fab2s\NodalFlow\Nodes\NodeInterface;
14
15
/**
16
 * Abstract Class FlowInterruptAbstract
17
 */
18
abstract class FlowInterruptAbstract implements FlowInterface
19
{
20
    /**
21
     * Continue flag
22
     *
23
     * @var bool
24
     */
25
    protected $continue = false;
26
27
    /**
28
     * Break Flag
29
     *
30
     * @var bool
31
     */
32
    protected $break = false;
33
34
    /**
35
     * @var string|bool
36
     */
37
    protected $interruptNodeId;
38
39
    /**
40
     * @var FlowMapInterface
41
     */
42
    protected $flowMap;
43
44
    /**
45
     * Current Flow Status
46
     *
47
     * @var FlowStatusInterface
48
     */
49
    protected $flowStatus;
50
51
    /**
52
     * Break the flow's execution, conceptually similar to breaking
53
     * a regular loop
54
     *
55
     * @param InterrupterInterface|null $flowInterrupt
56
     *
57
     * @return $this
58
     */
59
    public function breakFlow(InterrupterInterface $flowInterrupt = null)
60
    {
61
        return $this->interruptFlow(InterrupterInterface::TYPE_BREAK, $flowInterrupt);
62
    }
63
64
    /**
65
     * Continue the flow's execution, conceptually similar to continuing
66
     * a regular loop
67
     *
68
     * @param InterrupterInterface|null $flowInterrupt
69
     *
70
     * @return $this
71
     */
72
    public function continueFlow(InterrupterInterface $flowInterrupt = null)
73
    {
74
        return $this->interruptFlow(InterrupterInterface::TYPE_CONTINUE, $flowInterrupt);
75
    }
76
77
    /**
78
     * @param string                    $interruptType
79
     * @param InterrupterInterface|null $flowInterrupt
80
     *
81
     * @throws NodalFlowException
82
     *
83
     * @return $this
84
     */
85
    public function interruptFlow($interruptType, InterrupterInterface $flowInterrupt = null)
86
    {
87
        switch ($interruptType) {
88
            case InterrupterInterface::TYPE_CONTINUE:
89
                $this->continue = true;
90
                $this->flowMap->incrementFlow('num_continue');
91
                break;
92
            case InterrupterInterface::TYPE_BREAK:
93
                $this->flowStatus = new FlowStatus(FlowStatus::FLOW_DIRTY);
94
                $this->break      = true;
95
                $this->flowMap->incrementFlow('num_break');
96
                break;
97
            default:
98
                throw new NodalFlowException('FlowInterrupt Type missing');
99
        }
100
101
        if ($flowInterrupt) {
102
            $flowInterrupt->setType($interruptType)->propagate($this);
103
        }
104
105
        return $this;
106
    }
107
108
    /**
109
     * Used to set the eventual Node Target of an Interrupt signal
110
     * set to :
111
     * - A node hash to target
112
     * - true to interrupt every upstream nodes
113
     *     in this Flow
114
     * - false to only interrupt up to the first
115
     *     upstream Traversable in this Flow
116
     *
117
     * @param string|bool $interruptNodeId
118
     *
119
     * @return $this
120
     */
121
    public function setInterruptNodeId($interruptNodeId)
122
    {
123
        $this->interruptNodeId = $interruptNodeId;
124
125
        return $this;
126
    }
127
128
    /**
129
     * @param NodeInterface $node
130
     *
131
     * @return bool
132
     */
133
    protected function interruptNode(NodeInterface $node)
134
    {
135
        // if we have an interruptNodeId, bubble up until we match a node
136
        // else stop propagation
137
        return $this->interruptNodeId ? $this->interruptNodeId !== $node->getId() : false;
138
    }
139
}
140