Dispatcher   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 41
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 0
Metric Value
wmc 6
lcom 1
cbo 3
dl 0
loc 41
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
B dispatch() 0 31 5
1
<?php
2
namespace App\Core;
3
4
use ZF\Console\Dispatcher as ZfDispatcher;
5
use ZF\Console\Route;
6
use Zend\Console\Adapter\AdapterInterface as ConsoleAdapter;
7
use Zend\Console\ColorInterface as Color;
8
9
class Dispatcher extends ZfDispatcher
10
{
11
    protected $container;
12
13
    public function __construct($container)
14
    {
15
        $this->container = $container;
16
    }
17
18
    public function dispatch(Route $route, ConsoleAdapter $console)
19
    {
20
        $name = $route->getName();
0 ignored issues
show
Bug introduced by
Consider using $route->name. There is an issue with getName() and APC-enabled PHP versions.
Loading history...
21
        if (! isset($this->commandMap[$name])) {
22
            $console->writeLine('');
23
            $console->writeLine(sprintf('Unhandled command "%s" invoked', $name), Color::WHITE, Color::RED);
24
            $console->writeLine('');
25
            $console->writeLine('The command does not have a registered handler.');
26
            return 1;
27
        }
28
29
        $callable = $this->commandMap[$name];
30
31
        if (! is_callable($callable) && is_string($callable)) {
32
            $callable = new $callable();
33
            if (! is_callable($callable)) {
34
                throw new RuntimeException(sprintf(
35
                    'Invalid command class specified for "%s"; class must be invokable',
36
                    $name
37
                ));
38
            }
39
            $this->commandMap[$name] = $callable;
40
        }
41
        $return = $this->container->call($callable, [
42
            $route,
43
            $console,
44
            $this->container
45
        ]);
46
47
        return (int) $return;
48
    }
49
}
50