Completed
Push — master ( 214ac8...aa7016 )
by Krystian
01:23
created

ConsoleUtility::getOptionFromDboptions()   B

Complexity

Conditions 7
Paths 3

Size

Total Lines 23

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 23
rs 8.6186
c 0
b 0
f 0
cc 7
nc 3
nop 2
1
<?php
2
3
namespace SourceBroker\DeployerExtendedDatabase\Utility;
4
5
use Symfony\Component\Console\Output\OutputInterface;
6
use Symfony\Component\Console\Input\InputInterface;
7
8
/**
9
 * Class ConsoleUtility
10
 *
11
 * @package SourceBroker\DeployerExtendedDatabase\Utility
12
 */
13
class ConsoleUtility
14
{
15
    /**
16
     * Returns OutputInterface verbosity as parameter that can be used in cli command
17
     *
18
     * @param OutputInterface $output
19
     * @return string
20
     */
21
    public function getVerbosityAsParameter(OutputInterface $output)
22
    {
23
        switch ($output->getVerbosity()) {
24
            case OutputInterface::VERBOSITY_DEBUG:
25
                $verbosity = ' -vvv';
26
                break;
27
            case OutputInterface::VERBOSITY_VERY_VERBOSE:
28
                $verbosity = ' -vv';
29
                break;
30
            case OutputInterface::VERBOSITY_VERBOSE:
31
                $verbosity = ' -v';
32
                break;
33
            case OutputInterface::VERBOSITY_QUIET:
34
                $verbosity = ' -q';
35
                break;
36
            case OutputInterface::VERBOSITY_NORMAL:
37
            default:
38
                $verbosity = '';
39
        }
40
        return $verbosity;
41
    }
42
43
    /**
44
     * Check if option is present and return it. If not throw exception.
45
     *
46
     * @param string $requiredOption
47
     * @param InputInterface $input
48
     * @return mixed
49
     */
50
    public function optionRequired($requiredOption, InputInterface $input)
51
    {
52
        if (!empty($input->getOption($requiredOption))) {
53
            $requiredOptionValue = $input->getOption($requiredOption);
54
        } else {
55
            throw new \InvalidArgumentException('No --' . $requiredOption . ' option set.', 1458937128560);
56
        }
57
        return $requiredOptionValue;
58
    }
59
60
61
    /**
62
     * Check if option is present and return it. If not throw exception.
63
     *
64
     * @param $option
65
     * @param InputInterface $input
66
     * @return mixed
67
     */
68
    public function getOptionFromDboptions($option, InputInterface $input)
69
    {
70
        $dbOptionReturnValue = null;
71
        if (!empty($input->getOption('dboptions'))) {
72
            $dbOptions = explode(',', $input->getOption('dboptions'));
73
            if (is_array($dbOptions)) {
74
                foreach ($dbOptions as $dbOption) {
75
                    $dbOptionParts = explode(':', $dbOption);
76
                    if (!empty($dbOptionParts[1])) {
77
                        $dbOptionValue = $dbOptionParts[1];
78
                    }
79
                    if ($option === $dbOptionParts[0]) {
80
                        if (!empty($dbOptionValue)) {
81
                            $dbOptionReturnValue = $dbOptionValue;
82
                        } else {
83
                            $dbOptionReturnValue = true;
84
                        }
85
                    }
86
                }
87
            }
88
        }
89
        return $dbOptionReturnValue;
90
    }
91
}
92