Completed
Push — master ( 431e36...3578ad )
by Alex
24s queued 20s
created

ActionsRegistry::getAction()   B

Complexity

Conditions 8
Paths 11

Size

Total Lines 44

Duplication

Lines 6
Ratio 13.64 %

Importance

Changes 0
Metric Value
dl 6
loc 44
rs 7.9715
c 0
b 0
f 0
cc 8
nc 11
nop 2
1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * This file is part of the PierstovalCharacterManagerBundle package.
7
 *
8
 * (c) Alexandre Rock Ancelet <[email protected]>
9
 *
10
 * For the full copyright and license information, please view the LICENSE
11
 * file that was distributed with this source code.
12
 */
13
14
namespace Pierstoval\Bundle\CharacterManagerBundle\Registry;
15
16
use Pierstoval\Bundle\CharacterManagerBundle\Action\StepActionInterface;
17
18
class ActionsRegistry implements ActionsRegistryInterface
19
{
20
    /**
21
     * @var StepActionInterface[][]
22
     */
23
    private $actions = [];
24
25
    public function addStepAction(string $manager, string $stepName, $action): void
26
    {
27
        $this->actions[$manager][$stepName] = $action;
28
    }
29
30
    public function getAction(string $stepName, string $manager = null): StepActionInterface
31
    {
32
        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...
33
            throw new \RuntimeException('No actions in the registry.');
34
        }
35
36
        if (null === $manager) {
37
            $manager = \array_keys($this->actions)[0];
38
        }
39
40
        if (!isset($this->actions[$manager])) {
41
            throw new \InvalidArgumentException(\sprintf(
42
                'Manager "%s" does not exist.',
43
                $manager
44
            ));
45
        }
46
47
        if (!isset($this->actions[$manager][$stepName])) {
48
            throw new \InvalidArgumentException(\sprintf(
49
                'Step "%s" not found in manager "%s".',
50
                $stepName,
51
                $manager
52
            ));
53
        }
54
55
        $action = $this->actions[$manager][$stepName];
56
57
        if ($action instanceof \Closure) {
58
            // Lazy loading
59
            $action = $this->actions[$manager][$stepName]();
60 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...
61
                throw new \RuntimeException(\sprintf(
62
                    "Lazy-loaded action \"%s\" for character manager \"%s\" must be resolved to an instance of \"%s\".\n\"%s\" given.",
63
                    $stepName,
64
                    $manager,
65
                    StepActionInterface::class,
66
                    \is_object($action) ? \get_class($action) : \gettype($action)
67
                ));
68
            }
69
            $this->actions[$manager][$stepName] = $action;
70
        }
71
72
        return $action;
73
    }
74
}
75