Passed
Pull Request — master (#287)
by Théo
02:12
created

ConfigurableCommand::configure()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 5
dl 0
loc 7
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
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 InvalidArgumentException;
18
use KevinGH\Box\Configuration;
19
use KevinGH\Box\Console\ConfigurationHelper;
20
use KevinGH\Box\Json\JsonValidationException;
21
use KevinGH\Box\Throwable\Exception\NoConfigurationFound;
22
use Symfony\Component\Console\Command\Command;
23
use Symfony\Component\Console\Input\InputInterface;
24
use Symfony\Component\Console\Input\InputOption;
25
use Symfony\Component\Console\Output\OutputInterface;
26
use Symfony\Component\Console\Style\SymfonyStyle;
27
use function sprintf;
28
29
/**
30
 * Allows a configuration file path to be specified for a command.
31
 *
32
 * @private
33
 */
34
abstract class ConfigurableCommand extends Command
35
{
36
    private const CONFIG_PARAM = 'config';
37
38
    /**
39
     * {@inheritdoc}
40
     */
41
    protected function configure(): void
42
    {
43
        $this->addOption(
44
            self::CONFIG_PARAM,
45
            'c',
46
            InputOption::VALUE_REQUIRED,
47
            'The alternative configuration file path.'
48
        );
49
    }
50
51
    /**
52
     * Returns the configuration settings.
53
     *
54
     * @param InputInterface  $input
55
     * @param OutputInterface $output
56
     * @param bool            $allowNoFile Load the config nonetheless if not file is found when true
57
     *
58
     * @throws JsonValidationException
59
     *
60
     * @return Configuration
61
     */
62
    final protected function getConfig(InputInterface $input, OutputInterface $output, bool $allowNoFile = false): Configuration
63
    {
64
        /** @var ConfigurationHelper $helper */
65
        $helper = $this->getHelper('config');
66
67
        $io = new SymfonyStyle($input, $output);
68
69
        try {
70
            $configPath = null !== $input->getOption(self::CONFIG_PARAM)
71
                ? $input->getOption(self::CONFIG_PARAM)
72
                : $helper->findDefaultPath()
73
            ;
74
75
            $io->comment(
76
                sprintf(
77
                    'Loading the configuration file "<comment>%s</comment>".',
78
                    $configPath
0 ignored issues
show
Bug introduced by
It seems like $configPath can also be of type string[]; however, parameter $args of sprintf() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

78
                    /** @scrutinizer ignore-type */ $configPath
Loading history...
79
                )
80
            );
81
        } catch (NoConfigurationFound $exception) {
82
            if (false === $allowNoFile) {
83
                throw $exception;
84
            }
85
86
            $io->comment('Loading without a configuration file.');
87
88
            $configPath = null;
89
        }
90
91
        try {
92
            return $helper->loadFile($configPath);
93
        } catch (InvalidArgumentException $exception) {
94
            $io->error('The configuration file is invalid.');
95
96
            throw $exception;
97
        }
98
    }
99
}
100