Completed
Pull Request — master (#10)
by Alex
06:03
created

ActionsRegistry::getAction()   B

Complexity

Conditions 8
Paths 11

Size

Total Lines 40

Duplication

Lines 6
Ratio 15 %

Importance

Changes 0
Metric Value
dl 6
loc 40
rs 8.0355
c 0
b 0
f 0
cc 8
nc 11
nop 2
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, string $stepName, $action): void
24
    {
25
        $this->actions[$manager][$stepName] = $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
        $action = $this->actions[$manager][$stepName];
53
54
        if ($action instanceof \Closure) {
55
            // Lazy loading
56
            $action = $this->actions[$manager][$stepName]();
57 View Code Duplication
            if (!$action instanceof StepActionInterface) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
58
                throw new \RuntimeException(\sprintf(
59
                    "Lazy-loaded action \"%s\" for character manager \"%s\" must be resolved to an instance of \"%s\".\n\"%s\" given.",
60
                    $stepName, $manager, StepActionInterface::class, \is_object($action) ? \get_class($action) : \gettype($action)
61
                ));
62
            }
63
            $this->actions[$manager][$stepName] = $action;
64
        }
65
66
        return $action;
67
    }
68
}
69