GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.
Completed
Push — develop ( 50cef7...02ca20 )
by Rolf
01:54
created

Utils::getCommand()   B

Complexity

Conditions 6
Paths 6

Size

Total Lines 37
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 17
CRAP Score 6

Importance

Changes 6
Bugs 1 Features 0
Metric Value
c 6
b 1
f 0
dl 0
loc 37
ccs 17
cts 17
cp 1
rs 8.439
cc 6
eloc 17
nc 6
nop 2
crap 6
1
<?php
2
namespace izzum\statemachine\utils;
3
use izzum\command\NullCommand;
4
use izzum\command\Composite;
5
use izzum\statemachine\Exception;
6
use izzum\statemachine\Context;
7
use izzum\command\ICommand;
8
use izzum\statemachine\State;
9
use izzum\statemachine\StateMachine;
10
11
/**
12
 * utils class that has some helper methods for diverse purposes
13
 *
14
 * @author Rolf Vreijdenberger
15
 *
16
 */
17
class Utils {
18
    const STATE_CONCATENATOR = '_to_';
19
20
    /**
21
     * gets the transition name by two state names, using the default convention
22
     * for a transition name (which is concatenating state-from to state-to with
23
     * '_to_')
24
     *
25
     * @param string $from
26
     *            the state from which the transition is made
27
     * @param string $to
28
     *            the state to which the transition will be made
29
     * @return string <state_from>_to_<state_to>
30
     */
31 64
    public static function getTransitionName($from, $to)
32
    {
33 64
        return $from . self::STATE_CONCATENATOR . $to;
34
    }
35
36
    /**
37
     * returns the associated Command for the entry/exit/transition action on a
38
     * State or a Transition.
39
     * the Command will be configured with the 'reference' of the stateful
40
     * object
41
     *
42
     * @param string $command_name
43
     *            entry~,exit~ or transition command name.
44
     *            multiple commands can be split by a ',' in which case a
45
     *            composite command will be returned.
46
     * @param Context $context
47
     *            to be able to get the entity
48
     * @return ICommand
49
     * @throws Exception
50
     */
51 39
    public static function getCommand($command_name, Context $context)
52
    {
53
        // it's oke to have no command, as there might be 'marker' states, where
54
        // we just need to transition something to a next state (according to a
55
        // rule)
56
        // where useful work can be done (eg: from the 'initial' type state to
57
        // a 'shortcut' state for special cases.
58 39
        if ($command_name === '' || $command_name === null) {
59
            // return a command without side effects
60 26
            return new NullCommand();
61
        }
62
63 26
        $output = new Composite();
64
65
        // a command string can be made up of multiple commands seperated by a
66
        // comma
67 26
        $all_commands = explode(',', $command_name);
68
69
        // get the correct object to inject in the command(s)
70 26
        $entity = $context->getEntity();
71
72 26
        foreach ($all_commands as $single_command) {
73 26
            if (!class_exists($single_command)) {
74 3
                $e = new Exception(sprintf("failed command creation, class does not exist: (%s) for Context (%s)", $single_command, $context->toString()), Exception::COMMAND_CREATION_FAILURE);
75 3
                throw $e;
76
            }
77
78
            try {
79 23
                $command = new $single_command($entity);
80 21
                $output->add($command);
81 23
            } catch(\Exception $e) {
82 2
                $e = new Exception(sprintf("command (%s) objects to construction for Context (%s). message: '%s'", $single_command, $context->toString(), $e->getMessage()), Exception::COMMAND_CREATION_FAILURE);
83 2
                throw $e;
84
            }
85 21
        }
86 21
        return $output;
87
    }
88
89
    /**
90
     * Always returns an izzum exception (converts a non-izzum exception to an
91
     * izzum exception).
92
     * optionally throws it.
93
     *
94
     * @param \Exception $e
95
     * @param int $code
96
     * @return Exception
97
     * @throws Exception
98
     */
99 4
    public static function wrapToStateMachineException(\Exception $e, $code, $throw = false)
100
    {
101 4
        if (!is_a($e, 'izzum\statemachine\Exception')) {
102
            // wrap the exception and use the code provided.
103 2
            $e = new Exception($e->getMessage(), $code, $e);
104 2
        }
105 4
        if ($throw) {
106 1
            throw $e;
107
        }
108 3
        return $e;
109
    }
110
111
    /**
112
     * get all states that match a possible regex state from the set of states
113
     * provided
114
     *
115
     * @param State $regex
116
     *            a possible regex state.
117
     * @param State[] $targets
118
     *            all target State instances that we check the regex against.
119
     * @return State[] an array of State instances from the $targets State
120
     *         instances that matched the (negated) regex, or the $regex State if it was
121
     *         not a regex State after all.
122
     * @link https://php.net/manual/en/function.preg-match.php
123
     * @link http://regexr.com/ for trying out regular expressions
124
     */
125 41
    public static function getAllRegexMatchingStates(State $regex, $targets)
126
    {
127 41
        $all = array();
128 41
        if ($regex->isRegex()) {
129
            // lookup all from states that conform to this rgex
130 19
            foreach ($targets as $target) {
131 19
                if (!$target->isRegex() && self::matchesRegex($regex, $target)) {
132 19
                    $all [] = $target;
133 19
                }
134 19
            }
135 19
        } else {
136 37
            $all [] = $regex;
137
        }
138 41
        return $all;
139
    }
140
141
    /**
142
     * does an input regex state match a target states' name?
143
     *
144
     * @param State $regex
145
     *            the regex state
146
     * @param State $target
147
     *            the state to match the regular expression to
148
     * @return boolean
149
     * @link https://php.net/manual/en/function.preg-match.php
150
     * @link http://regexr.com/ for trying out regular expressions
151
     */
152 20
    public static function matchesRegex(State $regex, State $target)
153
    {
154 20
        $matches = false;
155 20
        if ($regex->isNormalRegex()) {
156 20
            $expression = str_replace(State::REGEX_PREFIX, '', $regex->getName());
157 20
            $matches = preg_match($expression, $target->getName()) === 1;
158 20
        }
159 20
        if($regex->isNegatedRegex()) {
160 1
            $expression = str_replace(State::REGEX_PREFIX_NEGATED, '', $regex->getName());
161 1
            $matches = preg_match($expression, $target->getName()) !== 1;
162 1
        }
163 20
        return $matches;
164
    }
165
}