ContainerCommandHandlerLocator   A
last analyzed

Complexity

Total Complexity 12

Size/Duplication

Total Lines 91
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 12
lcom 1
cbo 1
dl 0
loc 91
ccs 26
cts 26
cp 1
rs 10
c 0
b 0
f 0

6 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A findHandler() 0 4 1
A registerService() 0 4 1
A registerSubscriberService() 0 9 4
A lazyLoad() 0 10 2
A resolve() 0 14 3
1
<?php
2
declare(strict_types=1);
3
4
/**
5
 * GpsLab component.
6
 *
7
 * @author    Peter Gribanov <[email protected]>
8
 * @copyright Copyright (c) 2011, Peter Gribanov
9
 * @license   http://opensource.org/licenses/MIT
10
 */
11
12
namespace GpsLab\Component\Command\Handler\Locator;
13
14
use GpsLab\Component\Command\Command;
15
use GpsLab\Component\Command\Handler\CommandSubscriber;
16
use Psr\Container\ContainerInterface;
17
18
class ContainerCommandHandlerLocator implements CommandHandlerLocator
19
{
20
    /**
21
     * @var ContainerInterface
22
     */
23
    private $container;
24
25
    /**
26
     * @var array[]
27
     */
28
    private $command_handler_ids = [];
29
30
    /**
31
     * @param ContainerInterface $container
32
     */
33 6
    public function __construct(ContainerInterface $container)
34
    {
35 6
        $this->container = $container;
36 6
    }
37
38
    /**
39
     * @param Command $command
40
     *
41
     * @return callable|null
42
     */
43 6
    public function findHandler(Command $command): ?callable
44
    {
45 6
        return $this->lazyLoad(get_class($command));
46
    }
47
48
    /**
49
     * @param string $command_name
50
     * @param string $service
51
     * @param string $method
52
     */
53 5
    public function registerService(string $command_name, string $service, string $method = '__invoke'): void
54
    {
55 5
        $this->command_handler_ids[$command_name] = [$service, $method];
56 5
    }
57
58
    /**
59
     * @param string $service_name
60
     * @param string $class_name
61
     */
62 1
    public function registerSubscriberService(string $service_name, string $class_name): void
63
    {
64 1
        $get_subscribed_commands = [$class_name, 'getSubscribedCommands'];
65 1
        if (is_callable($get_subscribed_commands) && is_a($class_name, CommandSubscriber::class, true)) {
66 1
            foreach ($get_subscribed_commands() as $command_name => $method) {
67 1
                $this->registerService($command_name, $service_name, $method);
68
            }
69
        }
70 1
    }
71
72
    /**
73
     * @param string $command_name
74
     *
75
     * @return callable|null
76
     */
77 6
    private function lazyLoad(string $command_name): ?callable
78
    {
79 6
        if (isset($this->command_handler_ids[$command_name])) {
80 5
            [$service, $method] = $this->command_handler_ids[$command_name];
0 ignored issues
show
Bug introduced by
The variable $service does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
Bug introduced by
The variable $method does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
81
82 5
            return $this->resolve($this->container->get($service), $method);
83
        }
84
85 1
        return null;
86
    }
87
88
    /**
89
     * @param mixed  $service
90
     * @param string $method
91
     *
92
     * @return callable|null
93
     */
94 5
    private function resolve($service, string $method): ?callable
95
    {
96 5
        if (is_callable($service)) {
97 1
            return $service;
98
        }
99
100 4
        $handler = [$service, $method];
101
102 4
        if (is_callable($handler)) {
103 2
            return $handler;
104
        }
105
106 2
        return null;
107
    }
108
}
109