Completed
Push — master ( b31514...603a9f )
by Changwan
06:16
created

Dispatcher   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 39
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
dl 0
loc 39
ccs 0
cts 22
cp 0
rs 10
c 0
b 0
f 0
wmc 6
lcom 1
cbo 3

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A attach() 0 13 4
A execute() 0 4 1
1
<?php
2
namespace Wandu\Console;
3
4
use Psr\Container\ContainerInterface;
5
use Symfony\Component\Console\Application;
6
use Symfony\Component\Console\Command\Command as SymfonyCommand;
7
use Wandu\Console\Contracts\CommandAttachable;
8
use Wandu\Console\Symfony\CommandAdapter;
9
use RuntimeException;
10
11
class Dispatcher implements CommandAttachable
12
{
13
    /** @var \Psr\Container\ContainerInterface */
14
    protected $container;
15
16
    /** @var \Symfony\Component\Console\Application */
17
    protected $application;
18
19
    public function __construct(Application $application, ContainerInterface $container)
20
    {
21
        $this->application = $application;
22
        $this->container = $container;
23
    }
24
25
    /**
26
     * {@inheritdoc}
27
     */
28
    public function attach(string $name, $command): SymfonyCommand
29
    {
30
        if (is_string($command)) {
31
            $command = $this->container->get($command);
32
        }
33
        if ($command instanceof Command) {
34
            $command = new CommandAdapter($command);
35
        }
36
        if ($command = $this->application->add($command->setName($name))) {
37
            return $command;
38
        }
39
        throw new RuntimeException("cannot attach the command");
40
    }
41
42
    /**
43
     * @return int
44
     */
45
    public function execute()
46
    {
47
        return $this->application->run();
48
    }
49
}
50