Issues (224)

src/Console/ConfigurationLoader.php (1 issue)

Labels
Severity
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;
16
17
use Fidry\Console\IO;
0 ignored issues
show
The type Fidry\Console\IO was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
18
use InvalidArgumentException;
19
use KevinGH\Box\Configuration\Configuration;
20
use KevinGH\Box\Configuration\ConfigurationLoader as ConfigLoader;
21
use KevinGH\Box\Configuration\NoConfigurationFound;
22
use KevinGH\Box\Json\JsonValidationException;
23
use KevinGH\Box\NotInstantiable;
24
use function sprintf;
25
26
/**
27
 * Utility to load the configuration.
28
 *
29
 * @private
30
 */
31
final class ConfigurationLoader
32
{
33
    use NotInstantiable;
34
35
    /**
36
     * Returns the configuration settings.
37
     *
38
     * @param bool $allowNoFile Load the config nonetheless if not file is found when true
39
     *
40
     * @throws JsonValidationException|NoConfigurationFound
41
     */
42
    public static function getConfig(
43
        ?string $configPath,
44
        IO $io,
45
        bool $allowNoFile,
46
    ): Configuration {
47
        $configPath = self::getConfigPath($configPath, $io, $allowNoFile);
48
        $configLoader = new ConfigLoader();
49
50
        try {
51
            return $configLoader->loadFile($configPath);
52
        } catch (InvalidArgumentException $invalidConfig) {
53
            $io->error('The configuration file is invalid.');
54
55
            throw $invalidConfig;
56
        }
57
    }
58
59
    private static function getConfigPath(
60
        ?string $configPath,
61
        IO $io,
62
        bool $allowNoFile,
63
    ): ?string {
64
        try {
65
            $configPath ??= ConfigurationLocator::findDefaultPath();
66
        } catch (NoConfigurationFound $noConfigurationFound) {
67
            if (false === $allowNoFile) {
68
                throw $noConfigurationFound;
69
            }
70
71
            $io->comment('Loading without a configuration file.');
72
73
            return null;
74
        }
75
76
        $io->comment(
77
            sprintf(
78
                'Loading the configuration file "<comment>%s</comment>".',
79
                $configPath,
80
            ),
81
        );
82
83
        return $configPath;
84
    }
85
}
86