| Conditions | 26 |
| Paths | > 20000 |
| Total Lines | 195 |
| Code Lines | 138 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 3 | ||
| Bugs | 2 | Features | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
| 1 | <?php |
||
| 25 | public function execute() |
||
| 26 | { |
||
| 27 | $this->notEmptyCallback = function ($input) { |
||
| 28 | if (empty($input)) { |
||
| 29 | throw new \InvalidArgumentException('Please enter a value'); |
||
| 30 | } |
||
| 31 | return $input; |
||
| 32 | }; |
||
| 33 | |||
| 34 | $this->getCommand()->getApplication()->setAutoExit(false); |
||
| 35 | |||
| 36 | $dialog = $this->getCommand()->getHelper('dialog'); |
||
| 37 | |||
| 38 | $defaults = $this->commandConfig['installation']['defaults']; |
||
| 39 | |||
| 40 | $useDefaultConfigParams = $this->getCommand()->parseBoolOption( |
||
| 41 | $this->input->getOption('useDefaultConfigParams') |
||
| 42 | ); |
||
| 43 | |||
| 44 | $sessionSave = $useDefaultConfigParams ? $defaults['session-save'] : $dialog->ask( |
||
| 45 | $this->output, |
||
| 46 | '<question>Please enter the session save:</question> <comment>[' . |
||
| 47 | $defaults['session-save'] . ']</comment>: ', |
||
| 48 | $defaults['session-save'] |
||
| 49 | ); |
||
| 50 | |||
| 51 | $adminFrontname = $useDefaultConfigParams ? $defaults['backend-frontname'] : $dialog->askAndValidate( |
||
| 52 | $this->output, |
||
| 53 | '<question>Please enter the admin/backend frontname:</question> <comment>[' . |
||
| 54 | $defaults['backend-frontname'] . ']</comment> ', |
||
| 55 | $this->notEmptyCallback, |
||
| 56 | false, |
||
| 57 | $defaults['backend-frontname'] |
||
| 58 | ); |
||
| 59 | |||
| 60 | $currency = $useDefaultConfigParams ? $defaults['currency'] : $dialog->askAndValidate( |
||
| 61 | $this->output, |
||
| 62 | '<question>Please enter the default currency code:</question> <comment>[' . |
||
| 63 | $defaults['currency'] . ']</comment>: ', |
||
| 64 | $this->notEmptyCallback, |
||
| 65 | false, |
||
| 66 | $defaults['currency'] |
||
| 67 | ); |
||
| 68 | |||
| 69 | $locale = $useDefaultConfigParams ? $defaults['locale'] : $dialog->askAndValidate( |
||
| 70 | $this->output, |
||
| 71 | '<question>Please enter the locale code:</question> <comment>[' . $defaults['locale'] . ']</comment>: ', |
||
| 72 | $this->notEmptyCallback, |
||
| 73 | false, |
||
| 74 | $defaults['locale'] |
||
| 75 | ); |
||
| 76 | |||
| 77 | $timezone = $useDefaultConfigParams ? $defaults['timezone'] : $dialog->askAndValidate( |
||
| 78 | $this->output, |
||
| 79 | '<question>Please enter the timezone:</question> <comment>[' . $defaults['timezone'] . ']</comment>: ', |
||
| 80 | $this->notEmptyCallback, |
||
| 81 | false, |
||
| 82 | $defaults['timezone'] |
||
| 83 | ); |
||
| 84 | |||
| 85 | $adminUsername = $useDefaultConfigParams ? $defaults['admin-user'] : $dialog->askAndValidate( |
||
| 86 | $this->output, |
||
| 87 | '<question>Please enter the admin username:</question> <comment>[' . |
||
| 88 | $defaults['admin-user'] . ']</comment>: ', |
||
| 89 | $this->notEmptyCallback, |
||
| 90 | false, |
||
| 91 | $defaults['admin-user'] |
||
| 92 | ); |
||
| 93 | |||
| 94 | $adminPassword = $useDefaultConfigParams ? $defaults['admin-password'] : $dialog->askAndValidate( |
||
| 95 | $this->output, |
||
| 96 | '<question>Please enter the admin password:</question> <comment>[' . |
||
| 97 | $defaults['admin-password'] . ']</comment>: ', |
||
| 98 | $this->notEmptyCallback, |
||
| 99 | false, |
||
| 100 | $defaults['admin-password'] |
||
| 101 | ); |
||
| 102 | |||
| 103 | $adminFirstname = $useDefaultConfigParams ? $defaults['admin-firstname'] : $dialog->askAndValidate( |
||
| 104 | $this->output, |
||
| 105 | '<question>Please enter the admin\'s firstname:</question> <comment>[' . |
||
| 106 | $defaults['admin-firstname'] . ']</comment>: ', |
||
| 107 | $this->notEmptyCallback, |
||
| 108 | false, |
||
| 109 | $defaults['admin-firstname'] |
||
| 110 | ); |
||
| 111 | |||
| 112 | $adminLastname = $useDefaultConfigParams ? $defaults['admin-lastname'] : $dialog->askAndValidate( |
||
| 113 | $this->output, |
||
| 114 | '<question>Please enter the admin\'s lastname:</question> <comment>[' . |
||
| 115 | $defaults['admin-lastname'] . ']</comment>: ', |
||
| 116 | $this->notEmptyCallback, |
||
| 117 | false, |
||
| 118 | $defaults['admin-lastname'] |
||
| 119 | ); |
||
| 120 | |||
| 121 | $adminEmail = $useDefaultConfigParams ? $defaults['admin-email'] : $dialog->askAndValidate( |
||
| 122 | $this->output, |
||
| 123 | '<question>Please enter the admin\'s email:</question> <comment>[' . |
||
| 124 | $defaults['admin-email'] . ']</comment>: ', |
||
| 125 | $this->notEmptyCallback, |
||
| 126 | false, |
||
| 127 | $defaults['admin-email'] |
||
| 128 | ); |
||
| 129 | |||
| 130 | $validateBaseUrl = function ($url) { |
||
| 131 | if (!preg_match('|^http(s)?://[a-z0-9-]+(.[a-z0-9-]+)*(:[0-9]+)?(/.*)?$|i', $url)) { |
||
| 132 | throw new \InvalidArgumentException('Please enter a valid URL'); |
||
| 133 | } |
||
| 134 | if (parse_url($url, \PHP_URL_HOST) == 'localhost') { |
||
| 135 | throw new \InvalidArgumentException( |
||
| 136 | 'localhost cause problems! Please use 127.0.0.1 or another hostname' |
||
| 137 | ); |
||
| 138 | } |
||
| 139 | |||
| 140 | return $url; |
||
| 141 | }; |
||
| 142 | |||
| 143 | $baseUrl = ($this->input->getOption('baseUrl') !== null) |
||
| 144 | ? $this->input->getOption('baseUrl') |
||
| 145 | : $dialog->askAndValidate( |
||
| 146 | $this->output, |
||
| 147 | '<question>Please enter the base url:</question> ', |
||
| 148 | $validateBaseUrl, |
||
| 149 | false |
||
| 150 | ); |
||
| 151 | $baseUrl = rtrim($baseUrl, '/') . '/'; // normalize baseUrl |
||
| 152 | |||
| 153 | /** |
||
| 154 | * Correct session save (common mistake) |
||
| 155 | */ |
||
| 156 | if ($sessionSave == 'file') { |
||
| 157 | $sessionSave = 'files'; |
||
| 158 | } |
||
| 159 | $this->_getDefaultSessionFolder($sessionSave); |
||
| 160 | |||
| 161 | $argv = array( |
||
| 162 | 'language' => $locale, |
||
| 163 | 'timezone' => $timezone, |
||
| 164 | 'db-host' => $this->_prepareDbHost(), |
||
| 165 | 'db-name' => $this->config->getString('db_name'), |
||
| 166 | 'db-user' => $this->config->getString('db_user'), |
||
| 167 | 'base-url' => $baseUrl, |
||
| 168 | 'use-rewrites' => 1, |
||
| 169 | 'use-secure' => 0, |
||
| 170 | 'use-secure-admin' => 1, |
||
| 171 | 'admin-user' => $adminUsername, |
||
| 172 | 'admin-lastname' => $adminLastname, |
||
| 173 | 'admin-firstname' => $adminFirstname, |
||
| 174 | 'admin-email' => $adminEmail, |
||
| 175 | 'admin-password' => $adminPassword, |
||
| 176 | 'session-save' => $sessionSave, |
||
| 177 | 'backend-frontname' => $adminFrontname, |
||
| 178 | 'currency' => $currency, |
||
| 179 | ); |
||
| 180 | |||
| 181 | $dbPass = $this->config->getString('db_pass'); |
||
| 182 | if (!empty($dbPass)) { |
||
| 183 | $argv['db-password'] = $dbPass; |
||
| 184 | } |
||
| 185 | |||
| 186 | if ($useDefaultConfigParams) { |
||
| 187 | if (isset($defaults['encryption-key']) && strlen($defaults['encryption-key']) > 0) { |
||
| 188 | $argv['encryption-key'] = $defaults['encryption-key']; |
||
| 189 | } |
||
| 190 | if (strlen($defaults['use-secure']) > 0) { |
||
| 191 | $argv['use-secure'] = $defaults['use-secure']; |
||
| 192 | $argv['base-url-secure'] = str_replace('http://', 'https://', $baseUrl); |
||
| 193 | } |
||
| 194 | if (strlen($defaults['use-rewrites']) > 0) { |
||
| 195 | $argv['use-rewrites'] = $defaults['use-rewrites']; |
||
| 196 | } |
||
| 197 | } |
||
| 198 | |||
| 199 | $this->config->setArray('installation_args', $argv); |
||
| 200 | |||
| 201 | $installArgs = ''; |
||
| 202 | foreach ($argv as $argName => $argValue) { |
||
| 203 | if (is_null($argValue)) { |
||
| 204 | $installArgs .= '--' . $argName . ' '; |
||
| 205 | } elseif (is_bool($argValue)) { |
||
| 206 | if ($argValue) { |
||
| 207 | $argValue = '1'; |
||
| 208 | } else { |
||
| 209 | $argValue = '0'; |
||
| 210 | } |
||
| 211 | $installArgs .= '--' . $argName . '=' . $argValue . ' '; |
||
| 212 | } else { |
||
| 213 | $installArgs .= '--' . $argName . '=' . escapeshellarg($argValue) . ' '; |
||
| 214 | } |
||
| 215 | } |
||
| 216 | |||
| 217 | $this->output->writeln('<info>Start installation process.</info>'); |
||
| 218 | $this->_runInstaller($installArgs); |
||
| 219 | } |
||
| 220 | |||
| 293 |
If you suppress an error, we recommend checking for the error condition explicitly: