Bus::handlersNamespace()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 4
rs 10
cc 1
eloc 2
nc 1
nop 0
1
<?php
2
3
namespace R\Hive\Concrete\Commands;
4
5
use R\Hive\Concrete\Exceptions\NoSupportedHandlerFoundException;
6
use R\Hive\Contracts\Commands\BusInterface;
7
use R\Hive\Contracts\Commands\CommandInterface;
8
9
class Bus implements BusInterface
10
{
11
    public function execute(CommandInterface $command)
12
    {
13
        return $this
14
            ->resolveHandler($command->serial())
15
            ->execute($command);
16
    }
17
18
    public function handlersNamespace()
19
    {
20
        return 'App\Lib\Commands\Handlers';
21
    }
22
23
    public function resolveHandler($serial)
24
    {
25
        $class = $this->getClassNameFromSerial($serial);
26
        if (class_exists($class)) {
27
            return new $class();
28
        }
29
30
        throw new NoSupportedHandlerFoundException($serial);
31
    }
32
33
    protected function getClassNameFromSerial($serial)
34
    {
35
        $parts = explode('_', $serial);
36
        $class = '';
37
38
        foreach ($parts as $part) {
39
            $class .= ucfirst($part);
40
        }
41
42
        return $this->handlersNamespace().'\\'.$class.'Handler';
43
    }
44
}
45