Passed
Branch php8 (4b68f8)
by Mike
06:45
created

ClassnameLaminasLocator::getHandlerForCommand()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 14

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 3

Importance

Changes 0
Metric Value
dl 0
loc 14
ccs 7
cts 7
cp 1
rs 9.7998
c 0
b 0
f 0
cc 3
nc 3
nop 1
crap 3
1
<?php
2
namespace TacticianModule\Locator;
3
4
use League\Tactician\Exception\MissingHandlerException;
5
use League\Tactician\Handler\Locator\HandlerLocator;
6
use Psr\Container\ContainerInterface;
7
8
class ClassnameLaminasLocator implements HandlerLocator
9
{
10
    private $container;
11
12 3
    public function __construct(ContainerInterface $container)
13
    {
14 3
        $this->container = $container;
15 3
    }
16
17
    /**
18
     * Retrieves the handler for a specified command
19
     *
20
     * @param string $commandName
21
     *
22
     * @return mixed
23
     *
24
     * @throws MissingHandlerException
25
     */
26 3
    public function getHandlerForCommand($commandName)
27
    {
28 3
        $handlerFQCN = $commandName . 'Handler';
29
30 3
        if ($this->container->has($handlerFQCN)) {
31 1
            return $this->container->get($handlerFQCN);
32
        }
33
34 2
        if (class_exists($handlerFQCN)) {
35 1
            return new $handlerFQCN();
36
        }
37
38 1
        throw MissingHandlerException::forCommand($commandName);
39
    }
40
}
41