Completed
Push — master ( ed65a2...293665 )
by Fabrice
01:50
created

FlowRegistry   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 114
Duplicated Lines 24.56 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 0
Metric Value
wmc 11
lcom 1
cbo 3
dl 28
loc 114
rs 10
c 0
b 0
f 0

6 Methods

Rating   Name   Duplication   Size   Complexity  
A get() 0 4 1
A load() 0 12 2
A registerFlow() 14 14 2
A registerNode() 14 14 2
A getFlow() 0 4 2
A getNode() 0 4 2

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

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
     * @return $this
54
     */
55
    public function load(FlowInterface $flow, array $entry)
56
    {
57
        $this->registerFlow($flow);
58
        $flowId                    = $flow->getId();
59
        static::$registry[$flowId] = $entry;
60
61
        foreach ($flow->getNodes() as $node) {
62
            $this->registerNode($node);
63
        }
64
65
        return $this;
66
    }
67
68
    /**
69
     * @param FlowInterface $flow
70
     *
71
     * @throws NodalFlowException
72
     *
73
     * @return $this
74
     */
75 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...
76
    {
77
        $flowId = $flow->getId();
78
        if (isset(static::$flows[$flowId])) {
79
            throw new NodalFlowException('Duplicate Flow instances are not allowed', 1, null, [
80
                'flowClass' => get_class($flow),
81
                'flowId'    => $flowId,
82
            ]);
83
        }
84
85
        static::$flows[$flowId] = $flow;
86
87
        return $this;
88
    }
89
90
    /**
91
     * @param NodeInterface $node
92
     *
93
     * @throws NodalFlowException
94
     *
95
     * @return $this
96
     */
97 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...
98
    {
99
        $nodeId = $node->getId();
100
        if (isset(static::$nodes[$nodeId])) {
101
            throw new NodalFlowException('Duplicate Node instances are not allowed', 1, null, [
102
                'nodeClass' => get_class($node),
103
                'nodeId'    => $nodeId,
104
            ]);
105
        }
106
107
        static::$nodes[$nodeId] = $node;
108
109
        return $this;
110
    }
111
112
    /**
113
     * @param $flowId
114
     *
115
     * @return FlowInterface|null
116
     */
117
    public function getFlow($flowId)
118
    {
119
        return isset(static::$flows[$flowId]) ? static::$flows[$flowId] : null;
120
    }
121
122
    /**
123
     * @param $nodeId
124
     *
125
     * @return NodeInterface|null
126
     */
127
    public function getNode($nodeId)
128
    {
129
        return isset(static::$nodes[$nodeId]) ? static::$nodes[$nodeId] : null;
130
    }
131
}
132