Total Complexity | 48 |
Total Lines | 420 |
Duplicated Lines | 0 % |
Changes | 1 | ||
Bugs | 0 | Features | 0 |
Complex classes like SetupController 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 SetupController, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
50 | class SetupController extends AbstractBaseController |
||
51 | { |
||
52 | private const DEFAULT_DBTYPE = 'mysql'; |
||
53 | private const DEFAULT_PREFIX = 'wt_'; |
||
54 | private const DEFAULT_DATA = [ |
||
55 | 'baseurl' => '', |
||
56 | 'lang' => '', |
||
57 | 'dbtype' => self::DEFAULT_DBTYPE, |
||
58 | 'dbhost' => '', |
||
59 | 'dbport' => '', |
||
60 | 'dbuser' => '', |
||
61 | 'dbpass' => '', |
||
62 | 'dbname' => '', |
||
63 | 'tblpfx' => self::DEFAULT_PREFIX, |
||
64 | 'wtname' => '', |
||
65 | 'wtuser' => '', |
||
66 | 'wtpass' => '', |
||
67 | 'wtemail' => '', |
||
68 | ]; |
||
69 | |||
70 | /** @var string */ |
||
71 | protected $layout = 'layouts/setup'; |
||
72 | |||
73 | /** @var MigrationService */ |
||
74 | private $migration_service; |
||
75 | |||
76 | /** @var ModuleService */ |
||
77 | private $module_service; |
||
78 | |||
79 | /** @var ServerCheckService */ |
||
80 | private $server_check_service; |
||
81 | |||
82 | /** @var UserService */ |
||
83 | private $user_service; |
||
84 | |||
85 | /** |
||
86 | * SetupController constructor. |
||
87 | * |
||
88 | * @param MigrationService $migration_service |
||
89 | * @param ModuleService $module_service |
||
90 | * @param ServerCheckService $server_check_service |
||
91 | * @param UserService $user_service |
||
92 | */ |
||
93 | public function __construct( |
||
94 | MigrationService $migration_service, |
||
95 | ModuleService $module_service, |
||
96 | ServerCheckService $server_check_service, |
||
97 | UserService $user_service |
||
98 | ) { |
||
99 | $this->user_service = $user_service; |
||
100 | $this->migration_service = $migration_service; |
||
101 | $this->module_service = $module_service; |
||
102 | $this->server_check_service = $server_check_service; |
||
103 | } |
||
104 | |||
105 | /** |
||
106 | * Installation wizard - check user input and proceed to the next step. |
||
107 | * |
||
108 | * @param ServerRequestInterface $request |
||
109 | * |
||
110 | * @return ResponseInterface |
||
111 | */ |
||
112 | public function setup(ServerRequestInterface $request): ResponseInterface |
||
113 | { |
||
114 | // We will need an IP address for the logs. |
||
115 | $ip_address = $request->getServerParams()['REMOTE_ADDR'] ?? '127.0.0.1'; |
||
116 | $request = $request->withAttribute('client-ip', $ip_address); |
||
117 | |||
118 | app()->instance(ServerRequestInterface::class, $request); |
||
119 | app()->instance('cache.array', new Repository(new ArrayStore())); |
||
120 | |||
121 | $data = $this->userData($request); |
||
122 | |||
123 | $step = (int) ($request->getParsedBody()['step'] ?? '1'); |
||
124 | |||
125 | $locales = $this->module_service |
||
126 | ->setupLanguages() |
||
127 | ->map(static function (ModuleLanguageInterface $module): LocaleInterface { |
||
128 | return $module->locale(); |
||
129 | }); |
||
130 | |||
131 | if ($data['lang'] === '') { |
||
132 | $default = new LocaleEnUs(); |
||
133 | |||
134 | $locale = Locale::httpAcceptLanguage($request->getServerParams(), $locales->all(), $default); |
||
135 | |||
136 | $data['lang'] = $locale->languageTag(); |
||
137 | } |
||
138 | |||
139 | I18N::init($data['lang'], true); |
||
140 | |||
141 | $data['cpu_limit'] = $this->maxExecutionTime(); |
||
142 | $data['locales'] = $locales->all(); |
||
143 | $data['memory_limit'] = $this->memoryLimit(); |
||
144 | |||
145 | // Only show database errors after the user has chosen a driver. |
||
146 | if ($step >= 4) { |
||
147 | $data['errors'] = $this->server_check_service->serverErrors($data['dbtype']); |
||
148 | $data['warnings'] = $this->server_check_service->serverWarnings($data['dbtype']); |
||
149 | } else { |
||
150 | $data['errors'] = $this->server_check_service->serverErrors(); |
||
151 | $data['warnings'] = $this->server_check_service->serverWarnings(); |
||
152 | } |
||
153 | |||
154 | if (!$this->checkFolderIsWritable(Webtrees::DATA_DIR)) { |
||
155 | $data['errors']->push( |
||
156 | '<code>' . e(realpath(Webtrees::DATA_DIR)) . '</code><br>' . |
||
157 | I18N::translate('Oops! webtrees was unable to create files in this folder.') . ' ' . |
||
158 | I18N::translate('This usually means that you need to change the folder permissions to 777.') |
||
159 | ); |
||
160 | } |
||
161 | |||
162 | switch ($step) { |
||
163 | default: |
||
164 | case 1: |
||
165 | return $this->step1Language($data); |
||
166 | case 2: |
||
167 | return $this->step2CheckServer($data); |
||
168 | case 3: |
||
169 | return $this->step3DatabaseType($data); |
||
170 | case 4: |
||
171 | return $this->step4DatabaseConnection($data); |
||
172 | case 5: |
||
173 | return $this->step5Administrator($data); |
||
174 | case 6: |
||
175 | return $this->step6Install($data); |
||
176 | } |
||
177 | } |
||
178 | |||
179 | /** |
||
180 | * @param ServerRequestInterface $request |
||
181 | * |
||
182 | * @return mixed[] |
||
183 | */ |
||
184 | private function userData(ServerRequestInterface $request): array |
||
185 | { |
||
186 | $data = []; |
||
187 | |||
188 | foreach (self::DEFAULT_DATA as $key => $default) { |
||
189 | $data[$key] = $request->getParsedBody()[$key] ?? $default; |
||
190 | } |
||
191 | |||
192 | return $data; |
||
193 | } |
||
194 | |||
195 | /** |
||
196 | * The server's memory limit |
||
197 | * |
||
198 | * @return int |
||
199 | */ |
||
200 | private function maxExecutionTime(): int |
||
201 | { |
||
202 | return (int) ini_get('max_execution_time'); |
||
203 | } |
||
204 | |||
205 | /** |
||
206 | * The server's memory limit (in MB). |
||
207 | * |
||
208 | * @return int |
||
209 | */ |
||
210 | private function memoryLimit(): int |
||
236 | } |
||
237 | |||
238 | /** |
||
239 | * Check we can write to the data folder. |
||
240 | * |
||
241 | * @param string $data_dir |
||
242 | * |
||
243 | * @return bool |
||
244 | */ |
||
245 | private function checkFolderIsWritable(string $data_dir): bool |
||
246 | { |
||
247 | $text1 = random_bytes(32); |
||
248 | |||
249 | try { |
||
250 | file_put_contents($data_dir . 'test.txt', $text1); |
||
251 | $text2 = file_get_contents(Webtrees::DATA_DIR . 'test.txt'); |
||
252 | unlink(Webtrees::DATA_DIR . 'test.txt'); |
||
253 | } catch (Exception $ex) { |
||
254 | return false; |
||
255 | } |
||
256 | |||
257 | return $text1 === $text2; |
||
258 | } |
||
259 | |||
260 | /** |
||
261 | * @param mixed[] $data |
||
262 | * |
||
263 | * @return ResponseInterface |
||
264 | */ |
||
265 | private function step1Language(array $data): ResponseInterface |
||
266 | { |
||
267 | return $this->viewResponse('setup/step-1-language', $data); |
||
268 | } |
||
269 | |||
270 | /** |
||
271 | * @param mixed[] $data |
||
272 | * |
||
273 | * @return ResponseInterface |
||
274 | */ |
||
275 | private function step2CheckServer(array $data): ResponseInterface |
||
276 | { |
||
277 | return $this->viewResponse('setup/step-2-server-checks', $data); |
||
278 | } |
||
279 | |||
280 | /** |
||
281 | * @param mixed[] $data |
||
282 | * |
||
283 | * @return ResponseInterface |
||
284 | */ |
||
285 | private function step3DatabaseType(array $data): ResponseInterface |
||
286 | { |
||
287 | if ($data['errors']->isNotEmpty()) { |
||
288 | return $this->viewResponse('setup/step-2-server-checks', $data); |
||
289 | } |
||
290 | |||
291 | return $this->viewResponse('setup/step-3-database-type', $data); |
||
292 | } |
||
293 | |||
294 | /** |
||
295 | * @param mixed[] $data |
||
296 | * |
||
297 | * @return ResponseInterface |
||
298 | */ |
||
299 | private function step4DatabaseConnection(array $data): ResponseInterface |
||
300 | { |
||
301 | if ($data['errors']->isNotEmpty()) { |
||
302 | return $this->step3DatabaseType($data); |
||
303 | } |
||
304 | |||
305 | return $this->viewResponse('setup/step-4-database-' . $data['dbtype'], $data); |
||
306 | } |
||
307 | |||
308 | /** |
||
309 | * @param mixed[] $data |
||
310 | * |
||
311 | * @return ResponseInterface |
||
312 | */ |
||
313 | private function step5Administrator(array $data): ResponseInterface |
||
325 | } |
||
326 | |||
327 | /** |
||
328 | * @param mixed[] $data |
||
329 | * |
||
330 | * @return ResponseInterface |
||
331 | */ |
||
332 | private function step6Install(array $data): ResponseInterface |
||
333 | { |
||
334 | $error = $this->checkAdminUser($data['wtname'], $data['wtuser'], $data['wtpass'], $data['wtemail']); |
||
335 | |||
336 | if ($error !== '') { |
||
337 | $data['errors']->push($error); |
||
338 | |||
339 | return $this->step5Administrator($data); |
||
340 | } |
||
341 | |||
342 | try { |
||
343 | $this->createConfigFile($data); |
||
344 | } catch (Throwable $exception) { |
||
345 | return $this->viewResponse('setup/step-6-failed', ['exception' => $exception]); |
||
346 | } |
||
347 | |||
348 | // Done - start using webtrees! |
||
349 | return redirect($data['baseurl']); |
||
350 | } |
||
351 | |||
352 | /** |
||
353 | * @param string $wtname |
||
354 | * @param string $wtuser |
||
355 | * @param string $wtpass |
||
356 | * @param string $wtemail |
||
357 | * |
||
358 | * @return string |
||
359 | */ |
||
360 | private function checkAdminUser($wtname, $wtuser, $wtpass, $wtemail): string |
||
361 | { |
||
362 | if ($wtname === '' || $wtuser === '' || $wtpass === '' || $wtemail === '') { |
||
363 | return I18N::translate('You must enter all the administrator account fields.'); |
||
364 | } |
||
365 | |||
366 | if (mb_strlen($wtpass) < 6) { |
||
367 | return I18N::translate('The password needs to be at least six characters long.'); |
||
368 | } |
||
369 | |||
370 | return ''; |
||
371 | } |
||
372 | |||
373 | /** |
||
374 | * @param string[] $data |
||
375 | * |
||
376 | * @return void |
||
377 | */ |
||
378 | private function createConfigFile(array $data): void |
||
379 | { |
||
380 | // Create/update the database tables. |
||
381 | $this->connectToDatabase($data); |
||
382 | $this->migration_service->updateSchema('\Fisharebest\Webtrees\Schema', 'WT_SCHEMA_VERSION', Webtrees::SCHEMA_VERSION); |
||
383 | |||
384 | // Add some default/necessary configuration data. |
||
385 | $this->migration_service->seedDatabase(); |
||
386 | |||
387 | // If we are re-installing, then this user may already exist. |
||
388 | $admin = $this->user_service->findByIdentifier($data['wtemail']); |
||
389 | if ($admin === null) { |
||
390 | $admin = $this->user_service->findByIdentifier($data['wtuser']); |
||
391 | } |
||
392 | // Create the user |
||
393 | if ($admin === null) { |
||
394 | $admin = $this->user_service->create($data['wtuser'], $data['wtname'], $data['wtemail'], $data['wtpass']); |
||
395 | $admin->setPreference(User::PREF_LANGUAGE, $data['lang']); |
||
396 | $admin->setPreference(User::PREF_IS_VISIBLE_ONLINE, '1'); |
||
397 | } else { |
||
398 | $admin->setPassword($_POST['wtpass']); |
||
399 | } |
||
400 | // Make the user an administrator |
||
401 | $admin->setPreference(User::PREF_IS_ADMINISTRATOR, '1'); |
||
402 | $admin->setPreference(User::PREF_IS_EMAIL_VERIFIED, '1'); |
||
403 | $admin->setPreference(User::PREF_IS_ACCOUNT_APPROVED, '1'); |
||
404 | |||
405 | // Write the config file. We already checked that this would work. |
||
406 | $config_ini_php = view('setup/config.ini', $data); |
||
407 | |||
408 | file_put_contents(Webtrees::CONFIG_FILE, $config_ini_php); |
||
409 | |||
410 | // Login as the new user |
||
411 | $request = app(ServerRequestInterface::class) |
||
412 | ->withAttribute('base_url', $data['baseurl']); |
||
413 | |||
414 | Session::start($request); |
||
415 | Auth::login($admin); |
||
416 | Session::put('language', $data['lang']); |
||
417 | } |
||
418 | |||
419 | private function connectToDatabase(array $data): void |
||
470 | } |
||
471 | } |
||
472 |