Passed
Branch master (7b18cd)
by Jakub
03:40
created

Application   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 61
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 34
c 2
b 0
f 0
dl 0
loc 61
ccs 20
cts 20
cp 1
rs 10
wmc 6

5 Methods

Rating   Name   Duplication   Size   Complexity  
A getDefaultInputDefinition() 0 6 1
A createCommandLoader() 0 8 1
A toolsJsonDefault() 0 21 2
A __construct() 0 7 1
A doRun() 0 6 1
1
<?php declare(strict_types=1);
2
3
namespace Zalas\Toolbox\Cli;
4
5
use Psr\Container\ContainerInterface;
6
use Symfony\Component\Console\Application as CliApplication;
7
use Symfony\Component\Console\CommandLoader\CommandLoaderInterface;
8
use Symfony\Component\Console\CommandLoader\ContainerCommandLoader;
9
use Symfony\Component\Console\Input\InputInterface;
10
use Symfony\Component\Console\Input\InputOption;
11
use Symfony\Component\Console\Output\OutputInterface;
12
use Zalas\Toolbox\Cli\Command\InstallCommand;
13
use Zalas\Toolbox\Cli\Command\ListCommand;
14
use Zalas\Toolbox\Cli\Command\TestCommand;
15
16
final class Application extends CliApplication
17
{
18
    private $serviceContainer;
19
20 36
    public function __construct(string $version, ServiceContainer $serviceContainer)
21
    {
22 36
        parent::__construct('toolbox', $version);
23
24 36
        $this->serviceContainer = $serviceContainer;
25
26 36
        $this->setCommandLoader($this->createCommandLoader($serviceContainer));
27
    }
28
29 2
    public function doRun(InputInterface $input, OutputInterface $output)
30
    {
31 2
        $this->serviceContainer->set(InputInterface::class, $input);
32 2
        $this->serviceContainer->set(OutputInterface::class, $output);
33
34 2
        return parent::doRun($input, $output);
35
    }
36
37 13
    protected function getDefaultInputDefinition()
38
    {
39 13
        $definition = parent::getDefaultInputDefinition();
40 13
        $definition->addOption(new InputOption('tools', null, InputOption::VALUE_REQUIRED | InputOption::VALUE_IS_ARRAY, 'Path(s) to the list of tools. Can also be set with TOOLBOX_JSON environment variable.', $this->toolsJsonDefault()));
41
42 13
        return $definition;
43
    }
44
45 13
    private function toolsJsonDefault(): array
46
    {
47 13
        return \getenv('TOOLBOX_JSON')
48 2
            ? \array_map('trim', \explode(',', \getenv('TOOLBOX_JSON')))
49
            : [
50 13
                __DIR__.'/../../resources/pre-installation.json',
51
                __DIR__.'/../../resources/architecture.json',
52
                __DIR__.'/../../resources/checkstyle.json',
53
                __DIR__.'/../../resources/compatibility.json',
54
                __DIR__.'/../../resources/composer.json',
55
                __DIR__.'/../../resources/deprecation.json',
56
                __DIR__.'/../../resources/documentation.json',
57
                __DIR__.'/../../resources/linting.json',
58
                __DIR__.'/../../resources/metrics.json',
59
                __DIR__.'/../../resources/phpcs.json',
60
                __DIR__.'/../../resources/phpstan.json',
61
                __DIR__.'/../../resources/psalm.json',
62
                __DIR__.'/../../resources/refactoring.json',
63
                __DIR__.'/../../resources/security.json',
64
                __DIR__.'/../../resources/test.json',
65
                __DIR__.'/../../resources/tools.json',
66
            ];
67
    }
68
69 36
    private function createCommandLoader(ContainerInterface $container): CommandLoaderInterface
70
    {
71 36
        return new ContainerCommandLoader(
72 36
            $container,
73
            [
74 36
                InstallCommand::NAME => InstallCommand::class,
75
                ListCommand::NAME => ListCommand::class,
76
                TestCommand::NAME => TestCommand::class,
77
            ]
78
        );
79
    }
80
}
81