| Total Complexity | 42 |
| Total Lines | 139 |
| Duplicated Lines | 0 % |
| Changes | 1 | ||
| Bugs | 0 | Features | 0 |
Complex classes like InstallerController 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 InstallerController, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 31 | class InstallerController extends Controller |
||
| 32 | { |
||
| 33 | |||
| 34 | public function showInstaller() |
||
| 35 | { |
||
| 36 | return view('installer/installer'); |
||
| 37 | } |
||
| 38 | |||
| 39 | public function db(request $request) |
||
| 40 | { |
||
| 41 | if($request->database == 'MySQL'){ |
||
| 42 | return redirect(url('?mysql')); |
||
| 43 | }else{ |
||
| 44 | return redirect(url('?4')); |
||
| 45 | } |
||
| 46 | } |
||
| 47 | |||
| 48 | public function createAdmin(request $request) |
||
| 49 | { |
||
| 50 | |||
| 51 | $email = $request->email; |
||
| 52 | $password = $request->password; |
||
| 53 | $handle = $request->handle; |
||
| 54 | $name = $request->name; |
||
| 55 | |||
| 56 | $file = base_path('INSTALLERLOCK'); |
||
| 57 | if (!file_exists($file)) { |
||
| 58 | $handleFile = fopen($file, 'w') or die('Cannot create file: '.$file); |
||
|
|
|||
| 59 | fclose($handleFile); |
||
| 60 | } |
||
| 61 | |||
| 62 | try{EnvEditor::addKey('ADMIN_EMAIL', $email);}catch(Exception $e){} |
||
| 63 | |||
| 64 | if(DB::table('users')->count() == '0'){ |
||
| 65 | Schema::disableForeignKeyConstraints(); |
||
| 66 | DB::table('users')->delete(); |
||
| 67 | DB::table('users')->truncate(); |
||
| 68 | Schema::enableForeignKeyConstraints(); |
||
| 69 | |||
| 70 | $user = User::create([ |
||
| 71 | 'name' => $name, |
||
| 72 | 'email' => $email, |
||
| 73 | 'email_verified_at' => '0001-01-01 00:00:00', |
||
| 74 | 'password' => Hash::make($password), |
||
| 75 | 'littlelink_name' => $handle, |
||
| 76 | 'littlelink_description' => 'admin page', |
||
| 77 | 'block' => 'no', |
||
| 78 | ]); |
||
| 79 | |||
| 80 | User::where('id', '1')->update(['role' => 'admin']); |
||
| 81 | } |
||
| 82 | |||
| 83 | return redirect(url('?5')); |
||
| 84 | } |
||
| 85 | |||
| 86 | public function mysql(request $request) |
||
| 87 | { |
||
| 88 | $DB_CONNECTION = 'mysql'; |
||
| 89 | $DB_HOST = $request->host; |
||
| 90 | $DB_PORT = $request->port; |
||
| 91 | $DB_DATABASE = $request->name; |
||
| 92 | $DB_USERNAME = $request->username; |
||
| 93 | $DB_PASSWORD = $request->password; |
||
| 94 | |||
| 95 | if(EnvEditor::keyExists('DB_CONNECTION')){EnvEditor::editKey('DB_CONNECTION', $DB_CONNECTION);}else{EnvEditor::addKey('DB_CONNECTION', $DB_CONNECTION);} |
||
| 96 | if(EnvEditor::keyExists('DB_HOST')){EnvEditor::editKey('DB_HOST', $DB_HOST);}else{EnvEditor::addKey('DB_HOST', $DB_HOST);} |
||
| 97 | if(EnvEditor::keyExists('DB_PORT')){EnvEditor::editKey('DB_PORT', $DB_PORT);}else{EnvEditor::addKey('DB_PORT', $DB_PORT);} |
||
| 98 | if(EnvEditor::keyExists('DB_DATABASE')){EnvEditor::editKey('DB_DATABASE', $DB_DATABASE);}else{EnvEditor::addKey('DB_DATABASE', $DB_DATABASE);} |
||
| 99 | if(EnvEditor::keyExists('DB_USERNAME')){EnvEditor::editKey('DB_USERNAME', $DB_USERNAME);}else{EnvEditor::addKey('DB_USERNAME', $DB_USERNAME);} |
||
| 100 | if(EnvEditor::keyExists('DB_PASSWORD')){EnvEditor::editKey('DB_PASSWORD', $DB_PASSWORD);}else{EnvEditor::addKey('DB_PASSWORD', $DB_PASSWORD);} |
||
| 101 | |||
| 102 | return redirect(url('mysql-test')); |
||
| 103 | |||
| 104 | } |
||
| 105 | |||
| 106 | public function mysqlTest(request $request) |
||
| 107 | { |
||
| 108 | try {Artisan::call('migrate');} catch (exception $e) {$failed = "true";} |
||
| 109 | try {Artisan::call('db:seed --force');} catch (exception $e) {$failed = "true";} |
||
| 110 | try {Artisan::call('db:seed --class="PageSeeder" --force');} catch (exception $e) {$failed = "true";} |
||
| 111 | try {Artisan::call('db:seed --class="ButtonSeeder" --force');} catch (exception $e) {$failed = "true";} |
||
| 112 | |||
| 113 | try {$users = DB::table('buttons')->count(); $failed = false;} catch (exception $e) {$failed = true;} |
||
| 114 | |||
| 115 | if($failed == true){ |
||
| 116 | if(EnvEditor::keyExists('DB_CONNECTION')){EnvEditor::editKey('DB_CONNECTION', 'sqlite');}else{EnvEditor::addKey('DB_CONNECTION', 'sqlite');} |
||
| 117 | if(EnvEditor::keyExists('DB_HOST')){EnvEditor::deleteKey('DB_HOST');} |
||
| 118 | if(EnvEditor::keyExists('DB_PORT')){EnvEditor::deleteKey('DB_PORT');} |
||
| 119 | if(EnvEditor::keyExists('DB_DATABASE')){EnvEditor::deleteKey('DB_DATABASE');} |
||
| 120 | if(EnvEditor::keyExists('DB_USERNAME')){EnvEditor::deleteKey('DB_USERNAME');} |
||
| 121 | if(EnvEditor::keyExists('DB_PASSWORD')){EnvEditor::deleteKey('DB_PASSWORD');} |
||
| 122 | return redirect(url('?error')); |
||
| 123 | }else{ |
||
| 124 | return redirect(url('?4')); |
||
| 125 | } |
||
| 126 | } |
||
| 127 | |||
| 128 | public function options(request $request) |
||
| 129 | { |
||
| 130 | |||
| 131 | $user = User::find(1); |
||
| 132 | $llName = $user->littlelink_name; |
||
| 133 | |||
| 134 | if($request->register == 'Yes'){ |
||
| 135 | if(EnvEditor::keyExists('ALLOW_REGISTRATION')){EnvEditor::editKey('ALLOW_REGISTRATION', 'true');}else{EnvEditor::addKey('ALLOW_REGISTRATION', 'true');} |
||
| 136 | } else { |
||
| 137 | if(EnvEditor::keyExists('ALLOW_REGISTRATION')){EnvEditor::editKey('ALLOW_REGISTRATION', 'false');}else{EnvEditor::addKey('ALLOW_REGISTRATION', 'false');} |
||
| 138 | } |
||
| 139 | |||
| 140 | if($request->verify == 'Yes'){$value = "verified";}else{$value = "auth";} |
||
| 141 | if(EnvEditor::keyExists('REGISTER_AUTH')){EnvEditor::editKey('REGISTER_AUTH', $value);} |
||
| 142 | |||
| 143 | if($request->page == 'No'){$value = "";}else{$value = '"' . $llName . '"';} |
||
| 144 | if(EnvEditor::keyExists('HOME_URL')){EnvEditor::editKey('HOME_URL', $value);} |
||
| 145 | |||
| 146 | if(EnvEditor::keyExists('APP_NAME')){EnvEditor::editKey('APP_NAME', '"' . $request->app . '"');} |
||
| 147 | |||
| 148 | if(file_exists(base_path("INSTALLING"))){unlink(base_path("INSTALLING"));} |
||
| 149 | |||
| 150 | $file = base_path('INSTALLERLOCK'); |
||
| 151 | if (file_exists($file)) { |
||
| 152 | unlink($file) or die('Cannot delete file: '.$file); |
||
| 153 | sleep(1); |
||
| 154 | } |
||
| 155 | |||
| 156 | return redirect(url('dashboard')); |
||
| 157 | } |
||
| 158 | |||
| 159 | public function editConfigInstaller(request $request) |
||
| 170 | } |
||
| 171 | |||
| 172 | } |
||
| 173 |
In general, usage of exit should be done with care and only when running in a scripting context like a CLI script.