Completed
Push — master ( 2c00e3...367d56 )
by Peter
05:32
created

getCommandHandler()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 1
crap 1
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 GpsLab\Component\Command\Handler\CommandHandler;
15
use Symfony\Component\DependencyInjection\ContainerAwareInterface;
16
use Symfony\Component\DependencyInjection\ContainerAwareTrait;
17
use Symfony\Component\DependencyInjection\ContainerInterface;
18
19
class SymfonyContainerCommandHandlerLocator implements CommandHandlerLocator, ContainerAwareInterface
20
{
21
    use ContainerAwareTrait;
22
23
    /**
24
     * @var string[]
25
     */
26
    private $command_handler_ids = [];
27
28
    /**
29
     * @param Command $command
30
     *
31
     * @return CommandHandler|null
32
     */
33 4
    public function getCommandHandler(Command $command)
34
    {
35 4
        return $this->lazyLoad(get_class($command));
36
    }
37
38
    /**
39
     * @param string $command_name
40
     * @param string $service
41
     */
42 4
    public function registerService($command_name, $service)
43
    {
44 4
        $this->command_handler_ids[$command_name] = $service;
45 4
    }
46
47
    /**
48
     * @param $command_name
49
     *
50
     * @return CommandHandler
51
     */
52 4
    private function lazyLoad($command_name)
53
    {
54 4
        if ($this->container instanceof ContainerInterface && isset($this->command_handler_ids[$command_name])) {
55 3
            $handler = $this->container->get($this->command_handler_ids[$command_name]);
56
57 3
            if ($handler instanceof CommandHandler) {
58 1
                return $handler;
59
            }
60 2
        }
61
62 3
        return null;
63
    }
64
}
65