Completed
Push — master ( 09c84d...fe53c9 )
by Dmitry
02:55
created

NearbyHandlerLocator::getHandlerForCommand()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 16
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 16
ccs 0
cts 6
cp 0
rs 9.4285
cc 2
eloc 10
nc 2
nop 1
crap 6
1
<?php
2
3
namespace hiapi\bus;
4
5
use League\Tactician\Handler\Locator\HandlerLocator;
6
use ReflectionClass;
7
use yii\base\UnknownClassException;
8
9
class NearbyHandlerLocator implements HandlerLocator
10
{
11
    public function getHandlerForCommand($class)
12
    {
13
        $reflector = new ReflectionClass($class);
14
        $dir = dirname($reflector->getFileName());
15
16
        $commandName = $reflector->getShortName();
17
        $handlerName = substr($commandName, 0, strrpos($commandName, 'Command')) . 'Handler';
18
19
        $path = $dir . DIRECTORY_SEPARATOR . $handlerName . '.php';
20
        if (!is_file($path)) {
21
            throw new UnknownClassException('Class "' . $handlerName . '" was not found near to ' . $reflector->getName());
0 ignored issues
show
Bug introduced by
Consider using $reflector->name. There is an issue with getName() and APC-enabled PHP versions.
Loading history...
22
        }
23
24
        $className = $reflector->getNamespaceName() . '\\' . $handlerName;
25
        return new $className();
26
    }
27
}
28