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

ClassnameZendLocator   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 33
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 4
c 0
b 0
f 0
lcom 1
cbo 2
dl 0
loc 33
ccs 10
cts 10
cp 1
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A getHandlerForCommand() 0 14 3
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