Completed
Branch master (755019)
by Oliver
04:40
created

Process   A

Complexity

Total Complexity 13

Size/Duplication

Total Lines 111
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Test Coverage

Coverage 94.29%

Importance

Changes 9
Bugs 5 Features 2
Metric Value
wmc 13
c 9
b 5
f 2
lcom 1
cbo 4
dl 0
loc 111
ccs 33
cts 35
cp 0.9429
rs 10

9 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 1
A createCollection() 0 5 1
A getInitialState() 0 4 1
A getStates() 0 4 1
A getState() 0 4 1
A hasState() 0 4 1
A merge() 0 5 1
A addState() 0 16 4
A getStateCollectionMerger() 0 8 2
1
<?php
2
3
namespace Metabor\Statemachine;
4
5
use Metabor\Named;
6
use Metabor\Statemachine\Util\StateCollectionMerger;
7
use MetaborStd\MergeableInterface;
8
use MetaborStd\Statemachine\ProcessInterface;
9
use MetaborStd\Statemachine\StateInterface;
10
use MetaborStd\Statemachine\TransitionInterface;
11
12
/**
13
 * @author Oliver Tischlinger
14
 */
15
class Process extends Named implements ProcessInterface, MergeableInterface
16
{
17
    /**
18
     * @var StateCollection
19
     */
20
    private $states;
21
22
    /**
23
     * @var StateInterface
24
     */
25
    private $initialState;
26
27
    /**
28
     * @var StateCollectionMerger
29
     */
30
    private $stateCollectionMerger;
31
32
    /**
33
     * @param string         $name
34
     * @param StateInterface $initialState
35
     */
36 12
    public function __construct($name, StateInterface $initialState)
37
    {
38 12
        parent::__construct($name);
39 12
        $this->initialState = $initialState;
40 12
        $this->createCollection();
41 12
    }
42
43
    /**
44
     * @param StateInterface $state
45
     */
46 12
    protected function addState(StateInterface $state)
47
    {
48 12
        $name = $state->getName();
49 12
        if ($this->states->hasState($name)) {
50
            if ($this->states->getState($name) !== $state) {
51
                throw new \Exception('There is already a different state with name "' . $name . '"');
52
            }
53
        } else {
54 12
            $this->states->addState($state);
55
            /* @var $transition TransitionInterface */
56 12
            foreach ($state->getTransitions() as $transition) {
57 5
                $targetState = $transition->getTargetState();
58 5
                $this->addState($targetState);
59
            }
60
        }
61 12
    }
62
63
    /**
64
     */
65 12
    protected function createCollection()
66
    {
67 12
        $this->states = new StateCollection();
68 12
        $this->addState($this->initialState);
69 12
    }
70
71
    /**
72
     * @see MetaborStd\Statemachine.ProcessInterface::getInitialState()
73
     */
74 9
    public function getInitialState()
75
    {
76 9
        return $this->initialState;
77
    }
78
79
    /**
80
     * @see MetaborStd\Statemachine.ProcessInterface::getStates()
81
     */
82 1
    public function getStates()
83
    {
84 1
        return $this->states->getStates();
85
    }
86
87
    /**
88
     * @param string $name
89
     */
90 2
    public function getState($name)
91
    {
92 2
        return $this->states->getState($name);
93
    }
94
95
    /**
96
     * @param string $name
97
     *
98
     * @return bool
99
     */
100 1
    public function hasState($name)
101
    {
102 1
        return $this->states->hasState($name);
103
    }
104
105
    /**
106
     * @return StateCollectionMerger
107
     */
108 1
    public function getStateCollectionMerger()
109
    {
110 1
        if (!$this->stateCollectionMerger) {
111 1
            $this->stateCollectionMerger = new StateCollectionMerger($this->states);
112
        }
113
114 1
        return $this->stateCollectionMerger;
115
    }
116
117
    /**
118
     * @see \MetaborStd\MergeableInterface::merge()
119
     */
120 1
    public function merge($source)
121
    {
122 1
        $merger = $this->getStateCollectionMerger();
123 1
        $merger->merge($source);
124 1
    }
125
}
126