Completed
Push — master ( 4ed40a...f1e4e0 )
by Prasetyo
02:16
created

Silex::addHandler()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 11
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 11
rs 9.4285
cc 2
eloc 6
nc 2
nop 2
1
<?php
2
3
namespace Silex\Component\Tactician\Locator;
4
5
use League\Tactician\Handler\Locator\HandlerLocator;
6
use Pimple\Container;
7
8
/**
9
 * Class Silex
10
 * @package Silex\Component\Tactician\Locator
11
 */
12
class Silex implements HandlerLocator
13
{
14
    /**
15
     * @var Container
16
     */
17
    private $app;
18
19
    /**
20
     * @param Container $app
21
     */
22
    public function __construct(Container $app)
23
    {
24
        $this->app = $app;
25
        $this->app['tactician.handler_map'] = new \ArrayObject();
26
    }
27
28
    /**
29
     * @param $command
30
     * @return mixed
31
     */
32
    public function getHandlerForCommand($commandClassName)
33
    {
34
        $handlerClassName = $this->app['tactician.handler_map'][$commandClassName];
35
36
        $handlerObject = $this->app[$handlerClassName];
37
38
        if ( ! is_object($handlerObject)) {
39
            throw new \InvalidArgumentException(sprintf(
40
                'Class %s is not registered in container',
41
                $handlerClassName
42
            ));
43
        }
44
45
        return $this->app[$handlerClassName];
46
    }
47
48
    /**
49
     * @param string $commandClassName
50
     * @param string $handlerClassName
51
     */
52
    public function addHandler($commandClassName, $handlerClassName)
53
    {
54
        if ( ! class_exists($handlerClassName)) {
55
            throw new \InvalidArgumentException(sprintf(
56
                'Handler class %s not found',
57
                $handlerClassName
58
            ));
59
        }
60
61
        $this->app['tactician.handler_map'][$commandClassName] = $handlerClassName;
62
    }
63
}
64