FlowRegistry::load()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 11
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 6
c 1
b 0
f 0
nc 2
nop 2
dl 0
loc 11
rs 10
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(string $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): FlowRegistryInterface
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
    public function registerFlow(FlowInterface $flow): FlowRegistryInterface
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
    public function registerNode(NodeInterface $node): FlowRegistryInterface
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 string $flowId
116
     *
117
     * @return FlowInterface|null
118
     */
119
    public function getFlow(string $flowId): ? FlowInterface
120
    {
121
        return isset(static::$flows[$flowId]) ? static::$flows[$flowId] : null;
122
    }
123
124
    /**
125
     * @param string $nodeId
126
     *
127
     * @return NodeInterface|null
128
     */
129
    public function getNode(string $nodeId): ? NodeInterface
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): FlowRegistryInterface
140
    {
141
        static::$nodes[$node->getId()] = null;
142
143
        return $this;
144
    }
145
}
146