summaryrefslogtreecommitdiffstats
path: root/vendor/gipfl/icinga-cli-daemon/src/StateMachine.php
blob: d8ec470b180e63785a72186e5564373d4d9c6c66 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
<?php

namespace gipfl\IcingaCliDaemon;

use RuntimeException;

trait StateMachine
{
    /** @var string */
    private $currentState;

    /** @var array [fromState][toState] = [callback, ...] */
    private $allowedTransitions = [];

    /** @var array [state] = [callback, ...] */
    private $onState = [];

    public function initializeStateMachine($initialState)
    {
        if ($this->currentState !== null) {
            throw new RuntimeException('StateMachine has already been initialized');
        }
        $this->currentState = $initialState;
    }

    /**
     * @param string|array $fromState
     * @param string $toState
     * @param callable $callback
     * @return $this
     */
    public function onTransition($fromState, $toState, $callback)
    {
        if (is_array($fromState)) {
            foreach ($fromState as $state) {
                $this->onTransition($state, $toState, $callback);
            }
        } else {
            $this->allowTransition($fromState, $toState);
            $this->allowedTransitions[$fromState][$toState][] = $callback;
        }

        return $this;
    }

    public function allowTransition($fromState, $toState)
    {
        if (! isset($this->allowedTransitions[$fromState][$toState])) {
            $this->allowedTransitions[$fromState][$toState] = [];
        }

        return $this;
    }

    /**
     * @param $state
     * @param $callback
     * @return $this
     */
    public function onState($state, $callback)
    {
        if (! isset($this->onState[$state])) {
            $this->onState[$state] = [];
        }

        $this->onState[$state][] = $callback;

        return $this;
    }

    public function getState()
    {
        if ($this->currentState === null) {
            throw new RuntimeException('StateMachine has not been initialized');
        }

        return $this->currentState;
    }

    public function setState($state)
    {
        $fromState = $this->getState();
        if ($this->canTransit($fromState, $state)) {
            $this->currentState = $state;
            $this->runStateTransition($fromState, $state);
        } else {
            throw new RuntimeException(sprintf(
                'A transition from %s to %s is not allowed',
                $fromState,
                $state
            ));
        }
    }

    private function runStateTransition($fromState, $toState)
    {
        if (isset($this->allowedTransitions[$fromState][$toState])) {
            foreach ($this->allowedTransitions[$fromState][$toState] as $callback) {
                $callback();
            }
        }
        if (isset($this->onState[$toState])) {
            foreach ($this->onState[$toState] as $callback) {
                $callback();
            }
        }
    }

    public function canTransit($fromState, $toState)
    {
        return isset($this->allowedTransitions[$fromState][$toState]);
    }
}