Completed
Pull Request — master (#10)
by Witold
06:43
created

ClassnameZendLocator::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 4
ccs 3
cts 3
cp 1
rs 10
cc 1
eloc 2
nc 1
nop 1
crap 1
1
<?php
2
namespace TacticianModule\Locator;
3
4
use League\Tactician\Exception\MissingHandlerException;
5
use League\Tactician\Handler\Locator\HandlerLocator;
6
use Zend\ServiceManager\ServiceLocatorInterface;
7
8
class ClassnameZendLocator implements HandlerLocator
9
{
10
    private $serviceLocator;
11
12 3
    public function __construct(ServiceLocatorInterface $serviceLocator)
13
    {
14 3
        $this->serviceLocator = $serviceLocator;
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->serviceLocator->has($handlerFQCN)) {
31 1
            return $this->serviceLocator->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