Completed
Push — master ( 7b4301...3d29af )
by Sebastian
05:32
created

Base::fetchInputSettings()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 10
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 3

Importance

Changes 0
Metric Value
eloc 6
c 0
b 0
f 0
dl 0
loc 10
ccs 7
cts 7
cp 1
rs 10
cc 3
nc 3
nop 2
crap 3
1
<?php
2
/**
3
 * This file is part of CaptainHook.
4
 *
5
 * (c) Sebastian Feldmann <[email protected]>
6
 *
7
 * For the full copyright and license information, please view the LICENSE
8
 * file that was distributed with this source code.
9
 */
10
namespace CaptainHook\App\Console\Command;
11
12
use CaptainHook\App\Config;
13
use CaptainHook\App\Console\IO;
14
use CaptainHook\App\Console\IOUtil;
15
use Symfony\Component\Console\Command\Command;
16
use Symfony\Component\Console\Input\InputInterface;
17
use Symfony\Component\Console\Output\OutputInterface;
18
19
/**
20
 * Class Base
21
 *
22
 * @package CaptainHook
23
 * @author  Sebastian Feldmann <[email protected]>
24
 * @link    https://github.com/captainhookphp/captainhook
25
 * @since   Class available since Release 0.9.0
26
 */
27
abstract class Base extends Command
28
{
29
    /**
30
     * Input output handler
31
     *
32
     * @var \CaptainHook\App\Console\IO
33
     */
34
    private $io;
35
36
    /**
37
     * CaptainHook configuration
38
     *
39
     * @var \CaptainHook\App\Config
40
     */
41
    private $config;
42
43
    /**
44
     * IO setter
45
     *
46
     * @param \CaptainHook\App\Console\IO $io
47
     */
48 22
    public function setIO(IO $io) : void
49
    {
50 22
        $this->io = $io;
51 22
    }
52
53
    /**
54
     * IO interface getter
55
     *
56
     * @param  \Symfony\Component\Console\Input\InputInterface   $input
57
     * @param  \Symfony\Component\Console\Output\OutputInterface $output
58
     * @return \CaptainHook\App\Console\IO
59
     */
60 22
    public function getIO(InputInterface $input, OutputInterface $output) : IO
61
    {
62 22
        if (null === $this->io) {
63 1
            $this->io = new IO\DefaultIO($input, $output, $this->getHelperSet());
64
        }
65 22
        return $this->io;
66
    }
67
68
    /**
69
     * Return list of available options to overwrite the configuration settings
70
     *
71
     * @param  \Symfony\Component\Console\Input\InputInterface $input
72
     * @param  array                                           $settingNames
73
     * @return array
74
     */
75 7
    protected function fetchInputSettings(InputInterface $input, array $settingNames): array
76
    {
77 7
        $settings = [];
78 7
        foreach ($settingNames as $setting) {
79 7
            $value = IOUtil::argToString($input->getOption($setting));
80 7
            if (!empty($value)) {
81 4
                $settings[$setting] = $value;
82
            }
83
        }
84 7
        return $settings;
85
    }
86
87
    /**
88
     * CaptainHook config factory
89
     *
90
     * @param  string $path
91
     * @param  bool   $failIfNotFound
92
     * @param  array  $settings
93
     * @return \CaptainHook\App\Config
94
     * @throws \Exception
95
     */
96 21
    protected function getConfig(string $path = '', bool $failIfNotFound = false, array $settings = []) : Config
97
    {
98 21
        $this->config = Config\Factory::create($path, $settings);
99
100 21
        if ($failIfNotFound && !$this->config->isLoadedFromFile()) {
101 4
            throw new \RuntimeException(
102 4
                'Please create a captainhook configuration first' . PHP_EOL .
103 4
                'Run \'captainhook configure\'' . PHP_EOL .
104 4
                'If you have a configuration located elsewhere use the --configuration option'
105
            );
106
        }
107 17
        return $this->config;
108
    }
109
}
110