ConsoleApplication::resolveCommand()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 0
cts 4
cp 0
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 1
crap 2
1
<?php declare(strict_types = 1);
2
3
namespace Venta\Console;
4
5
use Symfony\Component\Console\Application as SymfonyConsoleApplication;
6
use Symfony\Component\Console\Input\InputInterface;
7
use Symfony\Component\Console\Output\OutputInterface;
8
use Venta\Contracts\Console\CommandCollection as CommandCollectionContract;
9
use Venta\Contracts\Container\Container;
10
use Venta\Contracts\Kernel\Kernel;
11
12
/**
13
 * Class ConsoleApplication
14
 *
15
 * @package Venta\Console
16
 */
17
final class ConsoleApplication
18
{
19
    /**
20
     * @var SymfonyConsoleApplication
21
     */
22
    private $console;
23
24
    /**
25
     * @var Container
26
     */
27
    private $container;
28
29
    /**
30
     * ConsoleApplication constructor.
31
     *
32
     * @param Kernel $kernel
33
     */
34 1
    public function __construct(Kernel $kernel)
35
    {
36 1
        $this->container = $kernel->boot();
37 1
        $this->initConsole('Venta Console', $kernel->version());
38
    }
39
40
    /**
41
     * Runs Console Application.
42
     *
43
     * @param InputInterface $input
44
     * @param OutputInterface $output
45
     * @return int
46
     */
47
    public function run(InputInterface $input, OutputInterface $output): int
48
    {
49
        return $this->console->run($input, $output);
50
    }
51
52
    /**
53
     * Initiates Symfony Console Application.
54
     *
55
     * @param string $name
56
     * @param string $version
57
     */
58 1
    private function initConsole(string $name, string $version)
59
    {
60 1
        $this->console = $this->container->get(SymfonyConsoleApplication::class);
61 1
        $this->console->setName($name);
62 1
        $this->console->setVersion($version);
63 1
        $this->console->setCatchExceptions(false);
64 1
        $this->console->setAutoExit(false);
65
66
        /** @var CommandCollectionContract $commands */
67 1
        $commands = $this->container->get(CommandCollectionContract::class);
68
        foreach ($commands as $command) {
69
            $this->console->add($this->resolveCommand($command));
70
        }
71
    }
72
73
    /**
74
     * Resolves command object from class name.
75
     *
76
     * @param string $commandClass
77
     * @return mixed
78
     */
79
    private function resolveCommand(string $commandClass)
80
    {
81
        return $this->container->get($commandClass);
82
    }
83
84
}