Completed
Push — master ( d1711a...bf9da9 )
by Alex
13s
created

ActionsRegistry::getActions()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 4
rs 10
cc 1
eloc 2
nc 1
nop 0
1
<?php
2
3
/**
4
 * This file is part of the PierstovalCharacterManagerBundle package.
5
 *
6
 * (c) Alexandre Rock Ancelet <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Pierstoval\Bundle\CharacterManagerBundle\Registry;
13
14
use Pierstoval\Bundle\CharacterManagerBundle\Action\StepActionInterface;
15
16
class ActionsRegistry implements ActionsRegistryInterface
17
{
18
    /**
19
     * @var StepActionInterface[][]
20
     */
21
    private $actions = [];
22
23
    public function addStepAction(string $manager, StepActionInterface $action): void
24
    {
25
        $this->actions[$manager][$action->getStep()->getName()] = $action;
26
    }
27
28
    public function getAction(string $stepName, string $manager = null): StepActionInterface
29
    {
30
        if (!$this->actions) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $this->actions of type Pierstoval\Bundle\Charac...StepActionInterface[][] is implicitly converted to a boolean; are you sure this is intended? If so, consider using empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
31
            throw new \RuntimeException('No actions in the registry.');
32
        }
33
34
        if ($manager === null) {
35
            $manager = array_keys($this->actions)[0];
36
        }
37
38
        if (!isset($this->actions[$manager])) {
39
            throw new \InvalidArgumentException(\sprintf(
40
                'Manager "%s" does not exist.',
41
                $manager
42
            ));
43
        }
44
45
        if (!isset($this->actions[$manager][$stepName])) {
46
                throw new \InvalidArgumentException(\sprintf(
47
                'Step "%s" not found in manager "%s".',
48
                $stepName, $manager
49
            ));
50
        }
51
52
        return $this->actions[$manager][$stepName];
53
    }
54
}
55