Total Complexity | 47 |
Total Lines | 410 |
Duplicated Lines | 0 % |
Changes | 2 | ||
Bugs | 0 | Features | 0 |
Complex classes like SetupWizard often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use SetupWizard, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
62 | class SetupWizard implements RequestHandlerInterface |
||
63 | { |
||
64 | use ViewResponseTrait; |
||
65 | |||
66 | private const DEFAULT_DBTYPE = DB::MYSQL; |
||
67 | private const DEFAULT_PREFIX = 'wt_'; |
||
68 | private const DEFAULT_DATA = [ |
||
69 | 'baseurl' => '', |
||
70 | 'lang' => '', |
||
71 | 'dbtype' => self::DEFAULT_DBTYPE, |
||
72 | 'dbhost' => '', |
||
73 | 'dbport' => '', |
||
74 | 'dbuser' => '', |
||
75 | 'dbpass' => '', |
||
76 | 'dbname' => '', |
||
77 | 'tblpfx' => self::DEFAULT_PREFIX, |
||
78 | 'wtname' => '', |
||
79 | 'wtuser' => '', |
||
80 | 'wtpass' => '', |
||
81 | 'wtemail' => '', |
||
82 | ]; |
||
83 | |||
84 | private const DEFAULT_PORTS = [ |
||
85 | DB::MYSQL => '3306', |
||
86 | DB::POSTGRES => '5432', |
||
87 | DB::SQLITE => '', |
||
88 | DB::SQL_SERVER => '', // Do not use default, as it is valid to have no port number. |
||
89 | ]; |
||
90 | |||
91 | private MigrationService $migration_service; |
||
92 | |||
93 | private ModuleService $module_service; |
||
94 | |||
95 | private ServerCheckService $server_check_service; |
||
96 | |||
97 | private UserService $user_service; |
||
98 | |||
99 | /** |
||
100 | * @param MigrationService $migration_service |
||
101 | * @param ModuleService $module_service |
||
102 | * @param ServerCheckService $server_check_service |
||
103 | * @param UserService $user_service |
||
104 | */ |
||
105 | public function __construct( |
||
115 | } |
||
116 | |||
117 | /** |
||
118 | * Installation wizard - check user input and proceed to the next step. |
||
119 | * |
||
120 | * @param ServerRequestInterface $request |
||
121 | * |
||
122 | * @return ResponseInterface |
||
123 | */ |
||
124 | public function handle(ServerRequestInterface $request): ResponseInterface |
||
125 | { |
||
126 | $this->layout = 'layouts/setup'; |
||
127 | |||
128 | // Some functions need a cache, but we don't have one yet. |
||
129 | Registry::cache(new CacheFactory()); |
||
130 | |||
131 | // We will need an IP address for the logs. |
||
132 | $ip_address = Validator::serverParams($request)->string('REMOTE_ADDR', '127.0.0.1'); |
||
133 | $request = $request->withAttribute('client-ip', $ip_address); |
||
134 | |||
135 | Registry::container()->set(ServerRequestInterface::class, $request); |
||
136 | |||
137 | $data = $this->userData($request); |
||
138 | |||
139 | $step = Validator::parsedBody($request)->integer('step', 1); |
||
140 | |||
141 | $locales = $this->module_service |
||
142 | ->setupLanguages() |
||
143 | ->map(static fn (ModuleLanguageInterface $module): LocaleInterface => $module->locale()); |
||
144 | |||
145 | if ($data['lang'] === '') { |
||
146 | $default = new LocaleEnUs(); |
||
147 | |||
148 | $locale = Locale::httpAcceptLanguage($request->getServerParams(), $locales->all(), $default); |
||
149 | |||
150 | $data['lang'] = $locale->languageTag(); |
||
151 | } |
||
152 | |||
153 | I18N::init($data['lang'], true); |
||
154 | |||
155 | $data['cpu_limit'] = $this->maxExecutionTime(); |
||
156 | $data['locales'] = $locales; |
||
157 | $data['memory_limit'] = $this->memoryLimit(); |
||
158 | |||
159 | // Only show database errors after the user has chosen a driver. |
||
160 | if ($step >= 4) { |
||
161 | $data['errors'] = $this->server_check_service->serverErrors($data['dbtype']); |
||
162 | $data['warnings'] = $this->server_check_service->serverWarnings($data['dbtype']); |
||
163 | } else { |
||
164 | $data['errors'] = $this->server_check_service->serverErrors(); |
||
165 | $data['warnings'] = $this->server_check_service->serverWarnings(); |
||
166 | } |
||
167 | |||
168 | if (!$this->checkFolderIsWritable(Webtrees::DATA_DIR)) { |
||
169 | $data['errors']->push( |
||
170 | '<code>' . e(realpath(Webtrees::DATA_DIR)) . '</code><br>' . |
||
171 | I18N::translate('Oops! webtrees was unable to create files in this folder.') . ' ' . |
||
172 | I18N::translate('This usually means that you need to change the folder permissions to 777.') |
||
173 | ); |
||
174 | } |
||
175 | |||
176 | switch ($step) { |
||
177 | default: |
||
178 | case 1: |
||
179 | return $this->step1Language($data); |
||
180 | case 2: |
||
181 | return $this->step2CheckServer($data); |
||
182 | case 3: |
||
183 | return $this->step3DatabaseType($data); |
||
184 | case 4: |
||
185 | return $this->step4DatabaseConnection($data); |
||
186 | case 5: |
||
187 | return $this->step5Administrator($data); |
||
188 | case 6: |
||
189 | return $this->step6Install($data); |
||
190 | } |
||
191 | } |
||
192 | |||
193 | /** |
||
194 | * @param ServerRequestInterface $request |
||
195 | * |
||
196 | * @return array<string,mixed> |
||
197 | */ |
||
198 | private function userData(ServerRequestInterface $request): array |
||
199 | { |
||
200 | $data = []; |
||
201 | |||
202 | foreach (self::DEFAULT_DATA as $key => $default) { |
||
203 | $data[$key] = Validator::parsedBody($request)->string($key, $default); |
||
204 | } |
||
205 | |||
206 | return $data; |
||
207 | } |
||
208 | |||
209 | /** |
||
210 | * The server's memory limit |
||
211 | * |
||
212 | * @return int |
||
213 | */ |
||
214 | private function maxExecutionTime(): int |
||
215 | { |
||
216 | return (int) ini_get('max_execution_time'); |
||
217 | } |
||
218 | |||
219 | /** |
||
220 | * The server's memory limit (in MB). |
||
221 | * |
||
222 | * @return int |
||
223 | */ |
||
224 | private function memoryLimit(): int |
||
225 | { |
||
226 | $memory_limit = ini_get('memory_limit'); |
||
227 | |||
228 | $number = (int) $memory_limit; |
||
229 | |||
230 | switch (substr($memory_limit, -1)) { |
||
231 | case 'g': |
||
232 | case 'G': |
||
233 | return $number * 1024; |
||
234 | case 'm': |
||
235 | case 'M': |
||
236 | return $number; |
||
237 | case 'k': |
||
238 | case 'K': |
||
239 | return (int) ($number / 1024); |
||
240 | default: |
||
241 | return (int) ($number / 1048576); |
||
242 | } |
||
243 | } |
||
244 | |||
245 | /** |
||
246 | * Check we can write to the data folder. |
||
247 | * |
||
248 | * @param string $data_dir |
||
249 | * |
||
250 | * @return bool |
||
251 | */ |
||
252 | private function checkFolderIsWritable(string $data_dir): bool |
||
253 | { |
||
254 | $text1 = random_bytes(32); |
||
255 | |||
256 | try { |
||
257 | file_put_contents($data_dir . 'test.txt', $text1); |
||
258 | $text2 = file_get_contents(Webtrees::DATA_DIR . 'test.txt'); |
||
259 | unlink(Webtrees::DATA_DIR . 'test.txt'); |
||
260 | } catch (Exception) { |
||
261 | return false; |
||
262 | } |
||
263 | |||
264 | return $text1 === $text2; |
||
265 | } |
||
266 | |||
267 | /** |
||
268 | * @param array<string,mixed> $data |
||
269 | * |
||
270 | * @return ResponseInterface |
||
271 | */ |
||
272 | private function step1Language(array $data): ResponseInterface |
||
273 | { |
||
274 | return $this->viewResponse('setup/step-1-language', $data); |
||
275 | } |
||
276 | |||
277 | /** |
||
278 | * @param array<string,mixed> $data |
||
279 | * |
||
280 | * @return ResponseInterface |
||
281 | */ |
||
282 | private function step2CheckServer(array $data): ResponseInterface |
||
283 | { |
||
284 | return $this->viewResponse('setup/step-2-server-checks', $data); |
||
285 | } |
||
286 | |||
287 | /** |
||
288 | * @param array<string,mixed> $data |
||
289 | * |
||
290 | * @return ResponseInterface |
||
291 | */ |
||
292 | private function step3DatabaseType(array $data): ResponseInterface |
||
293 | { |
||
294 | if ($data['errors']->isNotEmpty()) { |
||
295 | return $this->viewResponse('setup/step-2-server-checks', $data); |
||
296 | } |
||
297 | |||
298 | return $this->viewResponse('setup/step-3-database-type', $data); |
||
299 | } |
||
300 | |||
301 | /** |
||
302 | * @param array<string,mixed> $data |
||
303 | * |
||
304 | * @return ResponseInterface |
||
305 | */ |
||
306 | private function step4DatabaseConnection(array $data): ResponseInterface |
||
307 | { |
||
308 | if ($data['errors']->isNotEmpty()) { |
||
309 | return $this->step3DatabaseType($data); |
||
310 | } |
||
311 | |||
312 | return $this->viewResponse('setup/step-4-database-' . $data['dbtype'], $data); |
||
313 | } |
||
314 | |||
315 | /** |
||
316 | * @param array<string,mixed> $data |
||
317 | * |
||
318 | * @return ResponseInterface |
||
319 | */ |
||
320 | private function step5Administrator(array $data): ResponseInterface |
||
335 | } |
||
336 | |||
337 | /** |
||
338 | * @param array<string,mixed> $data |
||
339 | * |
||
340 | * @return ResponseInterface |
||
341 | */ |
||
342 | private function step6Install(array $data): ResponseInterface |
||
343 | { |
||
344 | $error = $this->checkAdminUser($data['wtname'], $data['wtuser'], $data['wtpass'], $data['wtemail']); |
||
345 | |||
346 | if ($error !== '') { |
||
347 | $data['errors']->push($error); |
||
348 | |||
349 | return $this->step5Administrator($data); |
||
350 | } |
||
351 | |||
352 | try { |
||
353 | $this->createConfigFile($data); |
||
354 | } catch (Throwable $exception) { |
||
355 | return $this->viewResponse('setup/step-6-failed', ['exception' => $exception]); |
||
356 | } |
||
357 | |||
358 | // Done - start using webtrees! |
||
359 | return redirect($data['baseurl']); |
||
360 | } |
||
361 | |||
362 | /** |
||
363 | * @param string $wtname |
||
364 | * @param string $wtuser |
||
365 | * @param string $wtpass |
||
366 | * @param string $wtemail |
||
367 | * |
||
368 | * @return string |
||
369 | */ |
||
370 | private function checkAdminUser(string $wtname, string $wtuser, string $wtpass, string $wtemail): string |
||
371 | { |
||
372 | if ($wtname === '' || $wtuser === '' || $wtpass === '' || $wtemail === '') { |
||
373 | return I18N::translate('You must enter all the administrator account fields.'); |
||
374 | } |
||
375 | |||
376 | if (mb_strlen($wtpass) < 6) { |
||
377 | return I18N::translate('The password needs to be at least six characters long.'); |
||
378 | } |
||
379 | |||
380 | return ''; |
||
381 | } |
||
382 | |||
383 | /** |
||
384 | * @param array<string,mixed> $data |
||
385 | * |
||
386 | * @return void |
||
387 | */ |
||
388 | private function createConfigFile(array $data): void |
||
427 | } |
||
428 | |||
429 | /** |
||
430 | * @param array<string,mixed> $data |
||
431 | * |
||
432 | * @return void |
||
433 | */ |
||
434 | private function connectToDatabase(array $data): void |
||
472 | ); |
||
473 | } |
||
475 |