LaravelLazyLocator::getHandlerForCommand()   A
last analyzed

Complexity

Conditions 3
Paths 2

Size

Total Lines 10
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 1
Metric Value
cc 3
eloc 4
c 2
b 0
f 1
nc 2
nop 1
dl 0
loc 10
rs 10
1
<?php
2
3
namespace Joselfonseca\LaravelTactician\Locator;
4
5
use League\Tactician\Exception\MissingHandlerException;
6
7
class LaravelLazyLocator extends LaravelLocator
8
{
9
10
    /**
11
     * Bind a handler instance to receive all commands with a certain class
12
     *
13
     * @param string $handler          Handler to receive class name
14
     * @param string $commandClassName Command class e.g. "My\TaskAddedCommand"
15
     */
16
    public function addHandler($handler, $commandClassName)
17
    {
18
        $this->handlers[$commandClassName] = function () use ($handler) {
19
            return app($handler);
20
        };
21
    }
22
23
    /**
24
     * Retrieves the handler for a specified command
25
     *
26
     * @param string $commandName
27
     *
28
     * @return object
29
     *
30
     * @throws MissingHandlerException
31
     */
32
    public function getHandlerForCommand($commandName)
33
    {
34
        if (!isset($this->handlers[$commandName]) || !is_callable($this->handlers[$commandName])) {
35
            throw MissingHandlerException::forCommand($commandName);
36
        }
37
38
        /** @var callable $handler */
39
        $handler = $this->handlers[$commandName];
40
41
        return $handler();
42
    }
43
}
44