Completed
Push — master ( 0228a6...5da3c1 )
by Fabrice
13:35
created

FlowRegistry::removeNode()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 6
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 3
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
 * class FlowRegistry
17
 */
18
class FlowRegistry implements FlowRegistryInterface
19
{
20
    /**
21
     * @var array
22
     */
23
    protected static $registry = [];
24
25
    /**
26
     * @var FlowInterface[]
27
     */
28
    protected static $flows = [];
29
30
    /**
31
     * @var NodeInterface[]
32
     */
33
    protected static $nodes = [];
34
35
    /**
36
     * Get registry meta data reference
37
     *
38
     * @param string $flowId
39
     *
40
     * @return mixed
41
     */
42
    public function &get($flowId)
43
    {
44
        return static::$registry[$flowId];
45
    }
46
47
    /**
48
     * Used upon FlowMap un-serialization
49
     *
50
     * @param FlowInterface $flow
51
     * @param array         $entry
52
     *
53
     * @throws NodalFlowException
54
     *
55
     * @return $this
56
     */
57
    public function load(FlowInterface $flow, array $entry)
58
    {
59
        $this->registerFlow($flow);
60
        $flowId                    = $flow->getId();
61
        static::$registry[$flowId] = $entry;
62
63
        foreach ($flow->getNodes() as $node) {
64
            $this->registerNode($node);
65
        }
66
67
        return $this;
68
    }
69
70
    /**
71
     * @param FlowInterface $flow
72
     *
73
     * @throws NodalFlowException
74
     *
75
     * @return $this
76
     */
77 View Code Duplication
    public function registerFlow(FlowInterface $flow)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
78
    {
79
        $flowId = $flow->getId();
80
        if (isset(static::$flows[$flowId])) {
81
            throw new NodalFlowException('Duplicate Flow instances are not allowed', 1, null, [
82
                'flowClass' => get_class($flow),
83
                'flowId'    => $flowId,
84
            ]);
85
        }
86
87
        static::$flows[$flowId] = $flow;
88
89
        return $this;
90
    }
91
92
    /**
93
     * @param NodeInterface $node
94
     *
95
     * @throws NodalFlowException
96
     *
97
     * @return $this
98
     */
99 View Code Duplication
    public function registerNode(NodeInterface $node)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
100
    {
101
        $nodeId = $node->getId();
102
        if (isset(static::$nodes[$nodeId])) {
103
            throw new NodalFlowException('Duplicate Node instances are not allowed', 1, null, [
104
                'nodeClass' => get_class($node),
105
                'nodeId'    => $nodeId,
106
            ]);
107
        }
108
109
        static::$nodes[$nodeId] = $node;
110
111
        return $this;
112
    }
113
114
    /**
115
     * @param $flowId
116
     *
117
     * @return FlowInterface|null
118
     */
119
    public function getFlow($flowId)
120
    {
121
        return isset(static::$flows[$flowId]) ? static::$flows[$flowId] : null;
122
    }
123
124
    /**
125
     * @param $nodeId
126
     *
127
     * @return NodeInterface|null
128
     */
129
    public function getNode($nodeId)
130
    {
131
        return isset(static::$nodes[$nodeId]) ? static::$nodes[$nodeId] : null;
132
    }
133
134
    /**
135
     * @param NodeInterface $node
136
     *
137
     * @return $this
138
     */
139
    public function removeNode(NodeInterface $node)
140
    {
141
        static::$nodes[$node->getId()] = null;
142
143
        return $this;
144
    }
145
}
146