NamespaceLocator::getHandlerForCommand()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 16
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 16
rs 9.4285
cc 2
eloc 9
nc 2
nop 1
1
<?php
2
3
namespace TillKruss\LaravelTactician\Locators;
4
5
use Illuminate\Contracts\Container\Container;
6
use League\Tactician\Handler\Locator\HandlerLocator;
7
use League\Tactician\Exception\MissingHandlerException;
8
use Illuminate\Contracts\Config\Repository as Configuration;
9
10
class NamespaceLocator implements HandlerLocator
11
{
12
    /**
13
     * The container instance.
14
     *
15
     * @var \Illuminate\Contracts\Container\Container
16
     */
17
    protected $container;
18
19
    /**
20
     * The configuration instance.
21
     *
22
     * @var \Illuminate\Contracts\Config\Repository
23
     */
24
    protected $config;
25
26
    /**
27
     * Create a new namespace locator instance.
28
     *
29
     * @param \Illuminate\Contracts\Container\Container  $container
30
     * @param \Illuminate\Contracts\Config\Repository    $config
31
     */
32
    public function __construct(Container $container, Configuration $config)
33
    {
34
        $this->container = $container;
35
        $this->config = $config;
36
    }
37
38
    /**
39
     * Returns the handler located in the configured namespace for given command.
40
     *
41
     * @param  string  $command
42
     * @return object
43
     *
44
     * @throws \League\Tactician\Exception\MissingHandlerException
45
     */
46
    public function getHandlerForCommand($command)
47
    {
48
        $namespaces = $this->config->get('tactician.namespaces', []);
49
50
        $handler = sprintf(
51
            '%s\\%sHandler',
52
            $namespaces['handlers'],
53
            trim(str_ireplace($namespaces['commands'], '', $command), '\\')
54
        );
55
56
        if (! class_exists($handler)) {
57
            throw MissingHandlerException::forCommand($handler);
58
        }
59
60
        return $this->container->make($handler);
61
    }
62
}
63