ConsoleApplication   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 68
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 40%

Importance

Changes 0
Metric Value
wmc 5
lcom 1
cbo 3
dl 0
loc 68
ccs 10
cts 25
cp 0.4
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A run() 0 4 1
A initConsole() 0 14 2
A resolveCommand() 0 4 1
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
}