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

ClassnameZendLocator::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 4
ccs 0
cts 0
cp 0
rs 10
cc 1
eloc 2
nc 1
nop 1
crap 2
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