Validator::shouldThemeBeInstalled()   B
last analyzed

Complexity

Conditions 6
Paths 8

Size

Total Lines 18
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 18
rs 8.8571
c 0
b 0
f 0
cc 6
eloc 9
nc 8
nop 4
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
        $config = self::shouldThemeBeInstalled($config, $helper, $input, $output);
31
32
        return $config;
33
    }
34
35
    /**
36
     * Asks if the theme should be installed and removes the theme specific configuration if not.
37
     *
38
     * @param  array           $config
39
     * @param  QuestionHelper  $helper
40
     * @param  InputInterface  $input
41
     * @param  OutputInterface $output
42
     *
43
     * @return array
44
     */
45
    protected static function shouldThemeBeInstalled($config, QuestionHelper $helper, InputInterface $input, OutputInterface $output)
46
    {
47
        $question = new Question("<info>Install default theme Y/n</info>: ", 'Y');
48
        $response = strtolower( trim( $helper->ask( $input, $output, $question) ) );
49
        $shouldInstall = ( 0 === strpos( $response, 'y' ) || '' === $response );
50
51
        // Remove theme details if the theme is not going to be installed.
52
        if ( ! $shouldInstall && isset( $config['theme'] ) ) {
53
            unset( $config['theme'] );
54
        }
55
56
        // Remove post install theme commands if the theme is not going to be installed.
57
        if ( ! $shouldInstall && isset( $config['commands']['postInstallTheme'] ) ) {
58
            unset( $config['commands']['postInstallTheme'] );
59
        }
60
61
        return $config;
62
    }
63
64
    /**
65
     * Sets a configuration value from validation.
66
     *
67
     * @param array  $config
68
     * @param string $value
69
     * @param string $ruleKey
70
     */
71
    protected static function setValue($config, $value, $ruleKey)
72
    {
73
        // For now all validation is 2 levels deep so this should be good for now.
74
        $keys = explode(':', $ruleKey);
75
        $config[ $keys[0] ][ $keys[1] ] = $value;
76
77
        return $config;
78
    }
79
80
    /**
81
     * Asks a question of the user.
82
     *
83
     * @param  string          $value
84
     * @param  array           $rule
85
     * @param  QuestionHelper  $helper
86
     * @param  InputInterface  $input
87
     * @param  OutputInterface $output
88
     *
89
     * @return string
90
     */
91
    protected static function askQuestion($value, $rule, QuestionHelper $helper, InputInterface $input, OutputInterface $output)
92
    {
93
        $message = $rule['question'];
94
        $verify = isset($rule['verify']) ? (boolean) $rule['verify'] : false;
95
96
        // We should only ask for things when they are empty or requested to verify.
97
        if (!$verify && $value) {
98
            return $value;
99
        }
100
101
        // Ask the question.
102
        $questionValue = $value ? " (<info>{$value}</info>)" : '';
103
        $question = new Question("{$message}{$questionValue}: ", $value);
104
        $question->setValidator(function ($answer) use ($message) {
105
            if (!$answer) {
106
                 throw new RuntimeException("{$message} is required!");
107
            }
108
109
            return $answer;
110
        });
111
112
        return $helper->ask($input, $output, $question);
113
    }
114
115
    /**
116
     * Get value from configuration.
117
     *
118
     * @param  array  $config
119
     * @param  string $ruleKey
120
     *
121
     * @return string
122
     */
123
    protected static function getValue($config, $ruleKey)
124
    {
125
        foreach (explode(':', $ruleKey) as $configKey) {
126
            $config = isset($config[ $configKey ]) ? $config[ $configKey ] : '';
127
        }
128
129
        if (is_array($config)) {
130
            return '';
131
        }
132
133
        return (string) $config;
134
    }
135
136
    /**
137
     * Validation required rules.
138
     *
139
     * @return array
140
     */
141
    protected static function rules()
142
    {
143
        return [
144
            'database:user' => [
145
                'question' => 'Database User',
146
                'verify' => true,
147
            ],
148
            'database:password' => [
149
                'question' => 'Database Password',
150
            ],
151
            'database:prefix' => [
152
                'question' => 'Database Prefix',
153
            ],
154
            'database:name' => [
155
                'question' => 'Database Name',
156
                'verify' => true,
157
            ],
158
            'database:host' => [
159
                'question' => 'Database Host',
160
            ],
161
            'user:name' => [
162
                'question' => 'User Login',
163
                'verify' => true,
164
            ],
165
            'user:email' => [
166
                'question' => 'Email',
167
                'verify' => true,
168
            ],
169
            'user:password' => [
170
                'question' => 'Password',
171
                'verify' => true,
172
            ],
173
            'site:title' => [
174
                'question' => 'Site Title',
175
            ],
176
            'site:url' => [
177
                'question' => 'Site URL',
178
                'verify' => true,
179
            ],
180
        ];
181
    }
182
}
183