Passed
Pull Request — master (#342)
by Théo
02:37
created

ConfigurableCommand::getConfigPath()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 1
dl 0
loc 3
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * This file is part of the box project.
7
 *
8
 * (c) Kevin Herrera <[email protected]>
9
 *     Théo Fidry <[email protected]>
10
 *
11
 * This source file is subject to the MIT license that is bundled
12
 * with this source code in the file LICENSE.
13
 */
14
15
namespace KevinGH\Box\Console\Command;
16
17
use KevinGH\Box\Configuration;
18
use KevinGH\Box\Console\ConfigurationLoader;
19
use KevinGH\Box\Json\JsonValidationException;
20
use Symfony\Component\Console\Input\InputInterface;
21
use Symfony\Component\Console\Input\InputOption;
22
use Symfony\Component\Console\Output\OutputInterface;
23
use Symfony\Component\Console\Style\SymfonyStyle;
24
25
/**
26
 * Allows a configuration file path to be specified for a command.
27
 *
28
 * @private
29
 */
30
abstract class ConfigurableCommand extends Command
31
{
32
    private const CONFIG_PARAM = 'config';
33
34
    /**
35
     * {@inheritdoc}
36
     */
37
    protected function configure(): void
38
    {
39
        $this->addOption(
40
            self::CONFIG_PARAM,
41
            'c',
42
            InputOption::VALUE_REQUIRED,
43
            'The alternative configuration file path.'
44
        );
45
    }
46
47
    /**
48
     * Returns the configuration settings.
49
     *
50
     * @param bool $allowNoFile Load the config nonetheless if not file is found when true
51
     *
52
     * @throws JsonValidationException
53
     */
54
    final protected function getConfig(InputInterface $input, OutputInterface $output, bool $allowNoFile = false): Configuration
55
    {
56
        return ConfigurationLoader::getConfig(
57
            $input->getOption(self::CONFIG_PARAM),
58
            $this->getConfigurationHelper(),
59
            new SymfonyStyle($input, $output),
60
            $allowNoFile
61
        );
62
    }
63
64
    final protected function getConfigPath(InputInterface $input): string
65
    {
66
        return $input->getOption(self::CONFIG_PARAM) ?? $this->getConfigurationHelper()->findDefaultPath();
67
    }
68
}
69