Passed
Push — master ( b89ee9...82ea21 )
by Sebastian
03:18
created

ConfigAware::getConfigPath()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 14
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 6
c 1
b 0
f 0
dl 0
loc 14
rs 10
cc 3
nc 3
nop 1
1
<?php
2
3
/**
4
 * This file is part of CaptainHook
5
 *
6
 * (c) Sebastian Feldmann <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace CaptainHook\App\Console\Command;
13
14
use CaptainHook\App\CH;
15
use CaptainHook\App\Config;
16
use CaptainHook\App\Console\Command;
17
use CaptainHook\App\Console\IOUtil;
18
use RuntimeException;
19
use SebastianFeldmann\Camino\Check;
20
use Symfony\Component\Console\Input\InputInterface;
21
use Symfony\Component\Console\Input\InputOption;
22
23
/**
24
 * Class ConfigAware
25
 *
26
 * Base class for all commands that need to be aware of the CaptainHook configuration.
27
 *
28
 * @package CaptainHook\App
29
 */
30
abstract class ConfigAware extends Command
31
{
32
    /**
33
     * Setup the configuration command option
34
     *
35
     * @return void
36
     */
37
    protected function configure(): void
38
    {
39
        parent::configure();
40
        $this->addOption(
41
            'configuration',
42
            'c',
43
            InputOption::VALUE_OPTIONAL,
44
            'Path to your json configuration',
45
            './' . CH::CONFIG
46
        );
47
    }
48
49
    /**
50
     * Create a new Config object
51
     *
52
     * @param  \Symfony\Component\Console\Input\InputInterface $input
53
     * @param  bool                                            $failIfNotFound
54
     * @param  array<string>                                   $settings
55
     * @return \CaptainHook\App\Config
56
     * @throws \Exception
57
     */
58
    protected function createConfig(InputInterface $input, bool $failIfNotFound = false, array $settings = []): Config
59
    {
60
        $config = Config\Factory::create($this->getConfigPath($input), $this->fetchConfigSettings($input, $settings));
61
        if ($failIfNotFound && !$config->isLoadedFromFile()) {
62
            throw new RuntimeException(
63
                'Please create a captainhook configuration first' . PHP_EOL .
64
                'Run \'captainhook configure\'' . PHP_EOL .
65
                'If you have a configuration located elsewhere use the --configuration option'
66
            );
67
        }
68
        return $config;
69
    }
70
71
    /**
72
     * Return the given config path option
73
     *
74
     * @param \Symfony\Component\Console\Input\InputInterface $input
75
     * @return string
76
     */
77
    private function getConfigPath(InputInterface $input): string
78
    {
79
        $path = IOUtil::argToString($input->getOption('configuration'));
80
81
        // if path not absolute
82
        if (!Check::isAbsolutePath($path)) {
83
            // try to guess the config location and
84
            // transform relative path to absolute path
85
            if (substr($path, 0, 2) === './') {
86
                return getcwd() . substr($path, 1);
87
            }
88
            return getcwd() . '/' . $path;
89
        }
90
        return $path;
91
    }
92
93
    /**
94
     * Return list of available options to overwrite the configuration settings
95
     *
96
     * @param  \Symfony\Component\Console\Input\InputInterface $input
97
     * @param  array<string>                                   $settingNames
98
     * @return array<string, string>
99
     */
100
    private function fetchConfigSettings(InputInterface $input, array $settingNames): array
101
    {
102
        $settings = [];
103
        foreach ($settingNames as $setting) {
104
            $value = IOUtil::argToString($input->getOption($setting));
105
            if (!empty($value)) {
106
                $settings[$setting] = $value;
107
            }
108
        }
109
        return $settings;
110
    }
111
}
112