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

SymfonyContainerCommandHandlerLocator::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 Symfony\Component\DependencyInjection\ContainerAwareInterface;
15
use Symfony\Component\DependencyInjection\ContainerAwareTrait;
16
use Symfony\Component\DependencyInjection\ContainerInterface;
17
18
class SymfonyContainerCommandHandlerLocator implements CommandHandlerLocator, ContainerAwareInterface
19
{
20
    use ContainerAwareTrait;
21
22
    /**
23
     * @var array
24
     */
25
    private $command_handler_ids = [];
26
27
    /**
28
     * @param Command $command
29
     *
30
     * @return callable|null
31
     */
32 5
    public function findHandler(Command $command)
33
    {
34 5
        $command_name = get_class($command);
35
36 5
        if (!($this->container instanceof ContainerInterface || !isset($this->command_handler_ids[$command_name]))) {
37 1
            return null;
38
        }
39
40 4
        return $this->resolve($this->command_handler_ids[$command_name]);
41
    }
42
43
    /**
44
     * @param string $command_name
45
     * @param string $service
46
     * @param string $method
47
     */
48 5
    public function registerService($command_name, $service, $method = '__invoke')
49
    {
50 5
        $this->command_handler_ids[$command_name] = [$service, $method];
51 5
    }
52
53
    /**
54
     * @param array $handler
55
     *
56
     * @return callable|null
57
     */
58 4
    private function resolve(array $handler)
59
    {
60 4
        list($service, $method) = $handler;
61
62 4
        if (is_callable($service)) {
63
            return $service;
64
        }
65
66 4
        if (is_callable([$service, $method])) {
67
            return [$service, $method];
68
        }
69
70 4
        return null;
71
    }
72
}
73