| Total Complexity | 44 |
| Total Lines | 374 |
| Duplicated Lines | 0 % |
| Changes | 1 | ||
| Bugs | 0 | Features | 0 |
Complex classes like UpdatesForVersion50 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 UpdatesForVersion50, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 16 | class UpdatesForVersion50 extends Migration |
||
| 17 | { |
||
| 18 | /** |
||
| 19 | * Database changes for version 5.0 |
||
| 20 | * |
||
| 21 | * @return void |
||
| 22 | */ |
||
| 23 | public function up() |
||
| 24 | { |
||
| 25 | // See the function itself for a description of the database changes |
||
| 26 | // DB Modifications |
||
| 27 | $this->updatePhoneIcons(); |
||
| 28 | $this->modifySystemDataTableNames(); |
||
| 29 | $this->addSoftDeleteToCustomerSystems(); |
||
| 30 | |||
| 31 | // DB Adds |
||
| 32 | $this->addPasswordExpiresColumn(); |
||
| 33 | $this->addIsInstallerToUsers(); |
||
| 34 | $this->addHiddenColumn(); |
||
| 35 | $this->addColumnsToFileLinksTable(); |
||
| 36 | $this->addNotesColumnToFileLinkFiles(); |
||
| 37 | $this->addDocumentationColumnToTechTips(); |
||
| 38 | |||
| 39 | // Remove Unneeded Tables |
||
| 40 | $this->removeNavBarView(); |
||
| 41 | $this->removeUserRolesTables(); |
||
| 42 | $this->removeFileLinkInstructionsTable(); |
||
| 43 | $this->removeFileLinkNotesTable(); |
||
| 44 | $this->removeSystemFilesTables(); |
||
| 45 | $this->removeActiveFromCustomers(); |
||
| 46 | |||
| 47 | } |
||
| 48 | |||
| 49 | /** |
||
| 50 | * Reverse the migrations. |
||
| 51 | * |
||
| 52 | * @return void |
||
| 53 | */ |
||
| 54 | public function down() |
||
| 55 | { |
||
| 56 | return 'Unable to Process. Please Downgrade app and load backup.'; |
||
| 57 | } |
||
| 58 | |||
| 59 | /* |
||
| 60 | * Database Modifications |
||
| 61 | */ |
||
| 62 | // Update the icon class to change from font awesome, to theymify icons |
||
| 63 | private function updatePhoneIcons() |
||
| 64 | { |
||
| 65 | PhoneNumberTypes::find(1)->update( |
||
| 66 | [ |
||
| 67 | 'icon_class' => 'ti-home' |
||
| 68 | ] |
||
| 69 | ); |
||
| 70 | PhoneNumberTypes::find(2)->update( |
||
| 71 | [ |
||
| 72 | 'icon_class' => 'ti-briefcase' |
||
| 73 | ] |
||
| 74 | ); |
||
| 75 | PhoneNumberTypes::find(3)->update( |
||
| 76 | [ |
||
| 77 | 'icon_class' => 'ti-mobile' |
||
| 78 | ] |
||
| 79 | ); |
||
| 80 | } |
||
| 81 | |||
| 82 | /* |
||
| 83 | * New Columns and Tables |
||
| 84 | */ |
||
| 85 | // Added the ability to set an expiration date for user passwords - will force them to change after this expires |
||
| 86 | private function addPasswordExpiresColumn() |
||
| 87 | { |
||
| 88 | if (!Schema::hasColumn('users', 'password_expires')) { |
||
| 89 | Schema::table('users', function (Blueprint $table) { |
||
| 90 | $table->timestamp('password_expires') |
||
| 91 | ->nullable() |
||
| 92 | ->after('active'); |
||
| 93 | }); |
||
| 94 | } |
||
| 95 | } |
||
| 96 | |||
| 97 | // Add the is installer column to the users table |
||
| 98 | private function addIsInstallerToUsers() |
||
| 99 | { |
||
| 100 | if (!Schema::hasColumn('users', 'is_installer')) { |
||
| 101 | Schema::table('users', function (Blueprint $table) { |
||
| 102 | $table->boolean('is_installer')->default(0)->after('active'); |
||
| 103 | }); |
||
| 104 | |||
| 105 | // TODO - Clean this up, it does not work |
||
| 106 | // Migrate user roles from the 'user roles' table to the new 'user permissions' table |
||
| 107 | if (Schema::hasTable('user_permissions') && (UserPermissions::all()->isEmpty())) { |
||
| 108 | $userRoles = DB::select('SELECT * FROM `user_role` LEFT JOIN `roles` ON `user_role`.`role_id` = `roles`.`role_id`'); |
||
| 109 | |||
| 110 | foreach ($userRoles as $user) { |
||
| 111 | if ($user->name === 'Installer') { |
||
| 112 | User::find($user->user_id)->update( |
||
| 113 | [ |
||
| 114 | 'is_installer' => 1 |
||
| 115 | ] |
||
| 116 | ); |
||
| 117 | UserPermissions::create( |
||
| 118 | [ |
||
| 119 | 'user_id' => $user->user_id, |
||
| 120 | 'manage_users' => 1, |
||
| 121 | 'run_reports' => 1, |
||
| 122 | 'add_customer' => 1, |
||
| 123 | 'deactivate_customer' => 1, |
||
| 124 | 'use_file_links' => 1, |
||
| 125 | 'create_tech_tip' => 1, |
||
| 126 | 'edit_tech_tip' => 1, |
||
| 127 | 'delete_tech_tip' => 1, |
||
| 128 | 'create_category' => 1, |
||
| 129 | 'modify_category' => 1 |
||
| 130 | ] |
||
| 131 | ); |
||
| 132 | } else if ($user->name === 'Admin') { |
||
| 133 | UserPermissions::create( |
||
| 134 | [ |
||
| 135 | 'user_id' => $user->user_id, |
||
| 136 | 'manage_users' => 1, |
||
| 137 | 'run_reports' => 1, |
||
| 138 | 'add_customer' => 1, |
||
| 139 | 'deactivate_customer' => 1, |
||
| 140 | 'use_file_links' => 1, |
||
| 141 | 'create_tech_tip' => 1, |
||
| 142 | 'edit_tech_tip' => 1, |
||
| 143 | 'delete_tech_tip' => 1, |
||
| 144 | 'create_category' => 1, |
||
| 145 | 'modify_category' => 1 |
||
| 146 | ] |
||
| 147 | ); |
||
| 148 | } else if ($user->name === 'Report') { |
||
| 149 | UserPermissions::create( |
||
| 150 | [ |
||
| 151 | 'user_id' => $user->user_id, |
||
| 152 | 'manage_users' => 0, |
||
| 153 | 'run_reports' => 1, |
||
| 154 | 'add_customer' => 1, |
||
| 155 | 'deactivate_customer' => 0, |
||
| 156 | 'use_file_links' => 1, |
||
| 157 | 'create_tech_tip' => 1, |
||
| 158 | 'edit_tech_tip' => 0, |
||
| 159 | 'delete_tech_tip' => 0, |
||
| 160 | 'create_category' => 0, |
||
| 161 | 'modify_category' => 0 |
||
| 162 | ] |
||
| 163 | ); |
||
| 164 | } else { |
||
| 165 | UserPermissions::create( |
||
| 166 | [ |
||
| 167 | 'user_id' => $user->user_id, |
||
| 168 | 'manage_users' => 0, |
||
| 169 | 'run_reports' => 0, |
||
| 170 | 'add_customer' => 1, |
||
| 171 | 'deactivate_customer' => 0, |
||
| 172 | 'use_file_links' => 1, |
||
| 173 | 'create_tech_tip' => 1, |
||
| 174 | 'edit_tech_tip' => 0, |
||
| 175 | 'delete_tech_tip' => 0, |
||
| 176 | 'create_category' => 0, |
||
| 177 | 'modify_category' => 0 |
||
| 178 | ] |
||
| 179 | ); |
||
| 180 | } |
||
| 181 | } |
||
| 182 | } |
||
| 183 | } |
||
| 184 | } |
||
| 185 | |||
| 186 | // Added a 'hidden' attribute to system customer data types to allow passwords to not be viewed unless clicked or focus |
||
| 187 | private function addHiddenColumn() |
||
| 188 | { |
||
| 189 | if (!Schema::hasColumn('system_data_field_types', 'hidden')) { |
||
| 190 | Schema::table('system_cust_data_types', function (Blueprint $table) { |
||
| 191 | $table->boolean('hidden') |
||
| 192 | ->default(0) |
||
| 193 | ->after('name'); |
||
| 194 | }); |
||
| 195 | } |
||
| 196 | } |
||
| 197 | |||
| 198 | // Update the File links table - add cust_id and note column |
||
| 199 | private function addColumnsToFileLinksTable() |
||
| 200 | { |
||
| 201 | if (!Schema::hasColumn('file_links', 'cust_id')) { |
||
| 202 | Schema::table('file_links', function (Blueprint $table) { |
||
| 203 | $table->integer('cust_id') |
||
| 204 | ->unsigned() |
||
| 205 | ->nullable() |
||
| 206 | ->after('user_id'); |
||
| 207 | $table->foreign('cust_id')->references('cust_id')->on('customers')->onUpdate('cascade')->onDelete('cascade'); |
||
| 208 | }); |
||
| 209 | } |
||
| 210 | if (!Schema::hasColumn('file_links', 'note')) { |
||
| 211 | Schema::table('file_links', function (Blueprint $table) { |
||
| 212 | $table->longText('note') |
||
| 213 | ->nullable() |
||
| 214 | ->after('link_name'); |
||
| 215 | }); |
||
| 216 | // Migrate the instructions from the old table to the new column |
||
| 217 | $instructions = DB::select('SELECT * FROM `file_link_instructions`'); |
||
| 218 | foreach ($instructions as $ins) { |
||
| 219 | FileLinks::find($ins->link_id)->update([ |
||
| 220 | 'note' => $ins->instruction |
||
| 221 | ]); |
||
| 222 | } |
||
| 223 | } |
||
| 224 | } |
||
| 225 | |||
| 226 | // Add Notes column to the file links files table |
||
| 227 | private function addNotesColumnToFileLinkFiles() |
||
| 228 | { |
||
| 229 | if (!Schema::hasColumn('file_link_files', 'note')) { |
||
| 230 | Schema::table('file_link_files', function (Blueprint $table) { |
||
| 231 | $table->longText('note') |
||
| 232 | ->nullable() |
||
| 233 | ->after('upload'); |
||
| 234 | }); |
||
| 235 | // Migrate the existing notes to the new table |
||
| 236 | $notes = DB::select('SELECT * FROM `file_link_notes`'); |
||
| 237 | foreach ($notes as $note) { |
||
| 238 | FileLinkFiles::where('file_id', $note->file_id)->update([ |
||
| 239 | 'note' => $note->note |
||
| 240 | ]); |
||
| 241 | } |
||
| 242 | } |
||
| 243 | } |
||
| 244 | |||
| 245 | // Add the documentation column to the tech tips table |
||
| 246 | private function addDocumentationColumnToTechTips() |
||
| 247 | { |
||
| 248 | if (!Schema::hasColumn('tech_tips', 'documentation')) { |
||
| 249 | Schema::table('tech_tips', function (Blueprint $table) { |
||
| 250 | $table->boolean('documentation')->default(0)->nullable()->after('public'); |
||
| 251 | }); |
||
| 252 | |||
| 253 | // Move all of the system files over to the tech tips table |
||
| 254 | $sysFiles = DB::select('SELECT * FROM `system_files`'); |
||
| 255 | foreach ($sysFiles as $sysFile) { |
||
| 256 | $newTip = TechTips::create([ |
||
| 257 | 'user_id' => $sysFile->user_id, |
||
| 258 | 'public' => 0, |
||
| 259 | 'documentation' => 1, |
||
| 260 | 'subject' => $sysFile->name, |
||
| 261 | 'description' => empty($sysFile->description) ? $sysFile->name : $sysFile->description, |
||
| 262 | 'created_at' => $sysFile->created_at, |
||
| 263 | 'updated_at' => $sysFile->updated_at |
||
| 264 | ]); |
||
| 265 | |||
| 266 | $tipId = $newTip->tip_id; |
||
| 267 | TechTipFiles::create([ |
||
| 268 | 'tip_id' => $tipId, |
||
| 269 | 'file_id' => $sysFile->file_id |
||
| 270 | ]); |
||
| 271 | TechTipSystems::create([ |
||
| 272 | 'tip_id' => $tipId, |
||
| 273 | 'sys_id' => $sysFile->sys_id |
||
| 274 | ]); |
||
| 275 | } |
||
| 276 | } |
||
| 277 | } |
||
| 278 | |||
| 279 | /* |
||
| 280 | * Database Cleanup |
||
| 281 | */ |
||
| 282 | // Remove the NavBar view if it exists - We no longer need it to sort out the system types |
||
| 283 | private function removeNavBarView() |
||
| 284 | { |
||
| 285 | DB::statement('DROP VIEW IF EXISTS `navbar_view`'); |
||
| 286 | } |
||
| 287 | |||
| 288 | // Remove the active column from the customers table |
||
| 289 | private function removeActiveFromCustomers() |
||
| 290 | { |
||
| 291 | if (Schema::hasColumn('customers', 'active')) |
||
| 292 | { |
||
| 293 | // Migrate the existing disabled customers first |
||
| 294 | $deactiveatedCustomers = Customers::where('active', 0); |
||
| 295 | foreach($deactiveatedCustomers as $cust) |
||
| 296 | { |
||
| 297 | // TODO: why is this not firing???? |
||
| 298 | Customers::find($cust->cust_id)->delete(); |
||
| 299 | } |
||
| 300 | |||
| 301 | Schema::table('customers', function (Blueprint $table) { |
||
| 302 | $table->dropColumn('active'); |
||
| 303 | }); |
||
| 304 | } |
||
| 305 | } |
||
| 306 | |||
| 307 | // Remove the user roles tables |
||
| 308 | private function removeUserRolesTables() |
||
| 318 | } |
||
| 319 | } |
||
| 320 | |||
| 321 | // Remove the File Link Instructions Table |
||
| 322 | private function removeFileLinkInstructionsTable() |
||
| 331 | } |
||
| 332 | } |
||
| 333 | |||
| 334 | // Remove the file link files note table |
||
| 335 | private function removeFileLinkNotesTable() |
||
| 336 | { |
||
| 337 | if(Schema::hasTable('file_link_notes')) |
||
| 344 | } |
||
| 345 | } |
||
| 346 | |||
| 347 | // Remove the system files and system file types table |
||
| 348 | private function removeSystemFilesTables() |
||
| 349 | { |
||
| 350 | if (Schema::hasTable('system_files')) { |
||
| 351 | Schema::table('system_files', function (Blueprint $table) { |
||
| 352 | $table->dropForeign(['sys_id']); |
||
| 353 | $table->dropForeign(['type_id']); |
||
| 354 | $table->dropForeign(['file_id']); |
||
| 355 | $table->dropForeign(['user_id']); |
||
| 356 | }); |
||
| 357 | Schema::dropIfExists('system_files'); |
||
| 358 | Schema::dropIfExists('system_file_types'); |
||
| 359 | } |
||
| 360 | } |
||
| 361 | |||
| 362 | // Rename the tables that hold customer system information to make more sense |
||
| 363 | private function modifySystemDataTableNames() |
||
| 364 | { |
||
| 365 | if(Schema::hasTable('customer_system_fields')) |
||
| 366 | { |
||
| 367 | Schema::dropIfExists('customer_system_data'); |
||
| 368 | Schema::rename('customer_system_fields', 'customer_system_data'); |
||
| 369 | } |
||
| 370 | if(Schema::hasTable('system_cust_data_fields')) |
||
| 371 | { |
||
| 372 | Schema::dropIfExists('system_data_fields'); |
||
| 373 | Schema::rename('system_cust_data_fields', 'system_data_fields'); |
||
| 374 | } |
||
| 375 | if(Schema::hasTable('system_cust_data_types')) |
||
| 376 | { |
||
| 377 | Schema::dropIfExists('system_data_field_types'); |
||
| 378 | Schema::rename('system_cust_data_types', 'system_data_field_types'); |
||
| 379 | } |
||
| 380 | } |
||
| 381 | |||
| 382 | // Add soft deletes to customer systems table to prevent accidental deletes |
||
| 383 | private function addSoftDeleteToCustomerSystems() |
||
| 390 | }); |
||
| 391 | } |
||
| 392 | } |
||
| 393 | } |
||
| 394 |