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

Dispatcher::attach()   A

Complexity

Conditions 4
Paths 8

Size

Total Lines 13
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 20

Importance

Changes 0
Metric Value
cc 4
eloc 8
nc 8
nop 2
dl 0
loc 13
ccs 0
cts 13
cp 0
crap 20
rs 9.2
c 0
b 0
f 0
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