Passed
Push — master ( a5e8db...945884 )
by Danny
02:33
created

Validator::getValue()   A

Complexity

Conditions 4
Paths 6

Size

Total Lines 12
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
eloc 6
nc 6
nop 2
dl 0
loc 12
rs 9.2
c 0
b 0
f 0
1
<?php
2
namespace PressCLI\Option;
3
4
use RuntimeException;
5
use Symfony\Component\Console\Question\Question;
6
use Symfony\Component\Console\Input\InputInterface;
7
use Symfony\Component\Console\Helper\QuestionHelper;
8
use Symfony\Component\Console\Output\OutputInterface;
9
10
class Validator
11
{
12
    /**
13
     * Validates the configuration against the rules and requests/verifies items.
14
     *
15
     * @param  array           $config
16
     * @param  QuestionHelper  $helper
17
     * @param  InputInterface  $input
18
     * @param  OutputInterface $output
19
     *
20
     * @return array
21
     */
22
    public static function validate($config, QuestionHelper $helper, InputInterface $input, OutputInterface $output)
23
    {
24
        foreach (self::rules() as $ruleKey => $rule) {
25
            $value = self::getValue($config, $ruleKey);
26
            $value = self::askQuestion($value, $rule, $helper, $input, $output);
27
            $config = self::setValue($config, $value, $ruleKey);
28
        }
29
30
        return $config;
31
    }
32
33
    /**
34
     * Sets a configuration value from validation.
35
     *
36
     * @param array  $config
37
     * @param string $value
38
     * @param string $ruleKey
39
     */
40
    protected static function setValue($config, $value, $ruleKey)
41
    {
42
        // For now all validation is 2 levels deep so this should be good for now.
43
        $keys = explode(':', $ruleKey);
44
        $config[ $keys[0] ][ $keys[1] ] = $value;
45
46
        return $config;
47
    }
48
49
    /**
50
     * Asks a question of the user.
51
     *
52
     * @param  string          $value
53
     * @param  array           $rule
54
     * @param  QuestionHelper  $helper
55
     * @param  InputInterface  $input
56
     * @param  OutputInterface $output
57
     *
58
     * @return string
59
     */
60
    protected static function askQuestion($value, $rule, QuestionHelper $helper, InputInterface $input, OutputInterface $output)
61
    {
62
        $message = $rule['question'];
63
        $verify = isset($rule['verify']) ? (boolean) $rule['verify'] : false;
64
65
        // We should only ask for things when they are empty or requested to verify.
66
        if (!$verify && $value) {
67
            return $value;
68
        }
69
70
        // Ask the question.
71
        $questionValue = $value ? " (<info>{$value}</info>)" : '';
72
        $question = new Question("{$message}{$questionValue}: ", $value);
73
        $question->setValidator(function ($answer) use ($message) {
74
            if (!$answer) {
75
                 throw new RuntimeException("{$message} is required!");
76
            }
77
78
            return $answer;
79
        });
80
81
        return $helper->ask($input, $output, $question);
82
    }
83
84
    /**
85
     * Get value from configuration.
86
     *
87
     * @param  array  $config
88
     * @param  string $ruleKey
89
     *
90
     * @return string
91
     */
92
    protected static function getValue($config, $ruleKey)
93
    {
94
        foreach (explode(':', $ruleKey) as $configKey) {
95
            $config = isset($config[ $configKey ]) ? $config[ $configKey ] : '';
96
        }
97
98
        if (is_array($config)) {
99
            return '';
100
        }
101
102
        return (string) $config;
103
    }
104
105
    /**
106
     * Validation required rules.
107
     *
108
     * @return array
109
     */
110
    protected static function rules()
111
    {
112
        return [
113
            'database:user' => [
114
                'question' => 'Database User',
115
                'verify' => true,
116
            ],
117
            'database:password' => [
118
                'question' => 'Database Password',
119
            ],
120
            'database:prefix' => [
121
                'question' => 'Database Prefix',
122
            ],
123
            'database:name' => [
124
                'question' => 'Database Name',
125
                'verify' => true,
126
            ],
127
            'database:host' => [
128
                'question' => 'Database Host',
129
            ],
130
            'user:name' => [
131
                'question' => 'User Login',
132
                'verify' => true,
133
            ],
134
            'user:email' => [
135
                'question' => 'Email',
136
                'verify' => true,
137
            ],
138
            'user:password' => [
139
                'question' => 'Password',
140
                'verify' => true,
141
            ],
142
            'site:title' => [
143
                'question' => 'Site Title',
144
            ],
145
            'site:url' => [
146
                'question' => 'Site URL',
147
                'verify' => true,
148
            ],
149
        ];
150
    }
151
}
152