Passed
Push — master ( 254d09...309d02 )
by Jakub
01:36
created

Application::doRun()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 3
nc 1
nop 2
dl 0
loc 7
ccs 4
cts 4
cp 1
crap 1
rs 10
c 0
b 0
f 0
1
<?php declare(strict_types=1);
2
3
namespace Zalas\Toolbox\Cli;
4
5
use Symfony\Component\Console\Application as CliApplication;
6
use Symfony\Component\Console\CommandLoader\CommandLoaderInterface;
7
use Symfony\Component\Console\CommandLoader\ContainerCommandLoader;
8
use Symfony\Component\Console\Input\InputInterface;
9
use Symfony\Component\Console\Input\InputOption;
10
use Symfony\Component\Console\Output\OutputInterface;
11
use Zalas\Toolbox\Cli\Command\InstallCommand;
12
use Zalas\Toolbox\Cli\Command\ListCommand;
13
use Zalas\Toolbox\Cli\Command\TestCommand;
14
15
final class Application extends CliApplication
16
{
17
    private $serviceContainer;
18
19 12
    public function __construct(string $version, ServiceContainer $serviceContainer)
20
    {
21 12
        parent::__construct('toolbox', $version);
22
23 12
        $this->serviceContainer = $serviceContainer;
24
25 12
        $this->setCommandLoader($this->createCommandLoader());
26
    }
27
28 1
    public function doRun(InputInterface $input, OutputInterface $output)
29
    {
30
        $this->serviceContainer->setParameter('toolbox_json', function () use ($input): array {
31 1
            return $input->getOption('tools');
32 1
        });
33
34 1
        return parent::doRun($input, $output);
35
    }
36
37 7
    protected function getDefaultInputDefinition()
38
    {
39 7
        $definition = parent::getDefaultInputDefinition();
40 7
        $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 7
        return $definition;
43
    }
44
45 7
    private function toolsJsonDefault(): array
46
    {
47 7
        return \getenv('TOOLBOX_JSON')
48
            ? \array_map('trim', \explode(',', \getenv('TOOLBOX_JSON')))
49 7
            : [__DIR__.'/../../resources/pre-installation.json', __DIR__.'/../../resources/tools.json'];
50
    }
51
52 12
    private function createCommandLoader(): CommandLoaderInterface
53
    {
54 12
        return new ContainerCommandLoader(
55 12
            $this->serviceContainer,
56
            [
57 12
                InstallCommand::NAME => InstallCommand::class,
58
                ListCommand::NAME => ListCommand::class,
59
                TestCommand::NAME => TestCommand::class,
60
            ]
61
        );
62
    }
63
}
64