Completed
Push — master ( 66fc0d...244682 )
by Peter
03:13
created

ContainerCommandHandlerLocator::resolve()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 14
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 3.2098

Importance

Changes 0
Metric Value
dl 0
loc 14
ccs 5
cts 7
cp 0.7143
rs 9.4285
c 0
b 0
f 0
cc 3
eloc 7
nc 3
nop 1
crap 3.2098
1
<?php
2
3
/**
4
 * GpsLab component.
5
 *
6
 * @author    Peter Gribanov <[email protected]>
7
 * @copyright Copyright (c) 2011, Peter Gribanov
8
 * @license   http://opensource.org/licenses/MIT
9
 */
10
11
namespace GpsLab\Component\Command\Handler\Locator;
12
13
use GpsLab\Component\Command\Command;
14
use Psr\Container\ContainerInterface;
15
16
class ContainerCommandHandlerLocator implements CommandHandlerLocator
17
{
18
    /**
19
     * @var ContainerInterface
20
     */
21
    private $container;
22
23
    /**
24
     * @var array
25
     */
26
    private $command_handler_ids = [];
27
28
    /**
29
     * @param ContainerInterface $container
30
     */
31 4
    public function __construct(ContainerInterface $container)
32
    {
33 4
        $this->container = $container;
34 4
    }
35
36
    /**
37
     * @param Command $command
38
     *
39
     * @return callable|null
40
     */
41 4
    public function findHandler(Command $command)
42
    {
43 4
        $command_name = get_class($command);
44
45 4
        if (!isset($this->command_handler_ids[$command_name])) {
46
            return null;
47
        }
48
49 4
        return $this->resolve($this->command_handler_ids[$command_name]);
50
    }
51
52
    /**
53
     * @param string $command_name
54
     * @param string $service
55
     * @param string $method
56
     */
57 4
    public function registerService($command_name, $service, $method = '__invoke')
58
    {
59 4
        $this->command_handler_ids[$command_name] = [$service, $method];
60 4
    }
61
62
    /**
63
     * @param array $handler
64
     *
65
     * @return callable|null
66
     */
67 4
    private function resolve(array $handler)
68
    {
69 4
        list($service, $method) = $handler;
70
71 4
        if (is_callable($service)) {
72
            return $service;
73
        }
74
75 4
        if (is_callable([$service, $method])) {
76
            return [$service, $method];
77
        }
78
79 4
        return null;
80
    }
81
}
82