Completed
Push — master ( cfb02d...d520a3 )
by Mike
04:37 queued 04:19
created

ClassnameZendLocator   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 36
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 100%

Importance

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

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A getHandlerForCommand() 0 17 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\Exception\ServiceNotFoundException;
7
use Zend\ServiceManager\ServiceLocatorInterface;
8
9
class ClassnameZendLocator implements HandlerLocator
10
{
11
    private $serviceLocator;
12
13
    public function __construct(ServiceLocatorInterface $serviceLocator)
14
    {
15
        $this->serviceLocator = $serviceLocator;
16
    }
17
18
    /**
19
     * Retrieves the handler for a specified command
20
     *
21
     * @param string $commandName
22
     *
23 3
     * @return mixed
24
     *
25 3
     * @throws MissingHandlerException
26
     */
27
    public function getHandlerForCommand($commandName)
28 3
    {
29 2
        $handlerFQCN = $commandName . 'Handler';
30
31
        try {
32
            return $this->serviceLocator->get($handlerFQCN);
33
        } catch (ServiceNotFoundException $e) {
34 2
            // Further check exists for class availability.
35 1
            // If not, Exception will be thrown anyway.
36
        }
37
38 1
        if (class_exists($handlerFQCN)) {
39
            return new $handlerFQCN();
40
        }
41
42
        throw MissingHandlerException::forCommand($commandName);
43
    }
44
}
45