Total Complexity | 45 |
Total Lines | 333 |
Duplicated Lines | 0 % |
Changes | 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->migrateSystemDocumentation(); |
||
30 | $this->migrateUserRoles(); |
||
31 | |||
32 | // DB Adds |
||
33 | $this->addSoftDeleteToCustomerSystems(); |
||
34 | $this->addSoftDeleteToTechTips(); |
||
35 | $this->addSoftDeleteToUsers(); |
||
36 | $this->addPasswordExpiresColumn(); |
||
37 | $this->addHiddenColumn(); |
||
38 | $this->addColumnsToFileLinksTable(); |
||
39 | $this->addNotesColumnToFileLinkFiles(); |
||
40 | |||
41 | // Remove Unneeded Tables |
||
42 | $this->removeNavBarView(); |
||
43 | $this->removeUserRolesTables(); |
||
44 | $this->removeFileLinkInstructionsTable(); |
||
45 | $this->removeFileLinkNotesTable(); |
||
46 | $this->removeSystemFilesTables(); |
||
47 | $this->removeActiveFromCustomers(); |
||
48 | |||
49 | } |
||
50 | |||
51 | /** |
||
52 | * Reverse the migrations. |
||
53 | * |
||
54 | * @return void |
||
55 | */ |
||
56 | public function down() |
||
57 | { |
||
58 | return 'Unable to Process. Please Downgrade app and load backup.'; |
||
59 | } |
||
60 | |||
61 | /* |
||
62 | * Database Modifications |
||
63 | */ |
||
64 | // Update the icon class to change from font awesome, to theymify icons |
||
65 | private function updatePhoneIcons() |
||
66 | { |
||
67 | $newIcons = [ |
||
68 | ['description' => 'Home', 'icon_class' => 'ti-home'], |
||
69 | ['description' => 'Work', 'icon_class' => 'ti-briefcase'], |
||
70 | ['description' => 'Mobile', 'icon_class' => 'ti-mobile'], |
||
71 | ]; |
||
72 | |||
73 | foreach($newIcons as $new) |
||
74 | { |
||
75 | PhoneNumberTypes::where('description', $new['description'])->update(['icon_class' => $new['icon_class']]); |
||
76 | } |
||
77 | } |
||
78 | |||
79 | /* |
||
80 | * New Columns and Tables |
||
81 | */ |
||
82 | // Added the ability to set an expiration date for user passwords - will force them to change after this expires |
||
83 | private function addPasswordExpiresColumn() |
||
84 | { |
||
85 | if(!Schema::hasColumn('users', 'password_expires')) { |
||
86 | Schema::table('users', function(Blueprint $table) { |
||
87 | $table->timestamp('password_expires') |
||
88 | ->nullable() |
||
89 | ->after('active'); |
||
90 | }); |
||
91 | } |
||
92 | } |
||
93 | |||
94 | // Add the is installer column to the users table |
||
95 | private function migrateUserRoles() |
||
96 | { |
||
97 | if(Schema::hasTable('user_role')) |
||
98 | { |
||
99 | if(!Schema::hasColumn('users', 'role_id')) |
||
100 | { |
||
101 | Schema::table('users', function (Blueprint $table) { |
||
102 | $table->integer('role_id')->after('user_id')->unsigned()->default(4); |
||
103 | $table->foreign('role_id')->references('role_id')->on('user_role_descriptions')->onUpdate('cascade'); |
||
104 | }); |
||
105 | } |
||
106 | |||
107 | $roleData = DB::select('SELECT * FROM `user_role`'); |
||
108 | |||
109 | foreach($roleData as $data) |
||
110 | { |
||
111 | User::where('user_id', $data->user_id)->update([ |
||
112 | 'role_id' => $data->role_id, |
||
113 | ]); |
||
114 | } |
||
115 | } |
||
116 | } |
||
117 | |||
118 | // Added a 'hidden' attribute to system customer data types to allow passwords to not be viewed unless clicked or focus |
||
119 | private function addHiddenColumn() |
||
120 | { |
||
121 | if(!Schema::hasColumn('system_data_field_types', 'hidden')) { |
||
122 | Schema::table('system_cust_data_types', function(Blueprint $table) { |
||
123 | $table->boolean('hidden') |
||
124 | ->default(0) |
||
125 | ->after('name'); |
||
126 | }); |
||
127 | } |
||
128 | } |
||
129 | |||
130 | // Update the File links table - add cust_id and note column |
||
131 | private function addColumnsToFileLinksTable() |
||
132 | { |
||
133 | if(!Schema::hasColumn('file_links', 'cust_id')) { |
||
134 | Schema::table('file_links', function(Blueprint $table) { |
||
135 | $table->integer('cust_id') |
||
136 | ->unsigned() |
||
137 | ->nullable() |
||
138 | ->after('user_id'); |
||
139 | $table->foreign('cust_id')->references('cust_id')->on('customers')->onUpdate('cascade')->onDelete('cascade'); |
||
140 | }); |
||
141 | } |
||
142 | if(!Schema::hasColumn('file_links', 'note')) { |
||
143 | Schema::table('file_links', function(Blueprint $table) { |
||
144 | $table->longText('note') |
||
145 | ->nullable() |
||
146 | ->after('link_name'); |
||
147 | }); |
||
148 | // Migrate the instructions from the old table to the new column |
||
149 | $instructions = DB::select('SELECT * FROM `file_link_instructions`'); |
||
150 | foreach($instructions as $ins) { |
||
151 | FileLinks::find($ins->link_id)->update([ |
||
152 | 'note' => $ins->instruction |
||
153 | ]); |
||
154 | } |
||
155 | } |
||
156 | } |
||
157 | |||
158 | // Add Notes column to the file links files table |
||
159 | private function addNotesColumnToFileLinkFiles() |
||
160 | { |
||
161 | if(!Schema::hasColumn('file_link_files', 'note')) { |
||
162 | Schema::table('file_link_files', function(Blueprint $table) { |
||
163 | $table->longText('note') |
||
164 | ->nullable() |
||
165 | ->after('upload'); |
||
166 | }); |
||
167 | // Migrate the existing notes to the new table |
||
168 | $notes = DB::select('SELECT * FROM `file_link_notes`'); |
||
169 | foreach($notes as $note) { |
||
170 | FileLinkFiles::where('file_id', $note->file_id)->update([ |
||
171 | 'note' => $note->note |
||
172 | ]); |
||
173 | } |
||
174 | } |
||
175 | } |
||
176 | |||
177 | // Add the documentation column to the tech tips table |
||
178 | private function migrateSystemDocumentation() |
||
179 | { |
||
180 | if(!Schema::hasColumn('tech_tips', 'tip_type_id')) { |
||
181 | Schema::table('tech_tips', function(Blueprint $table) { |
||
182 | $table->bigInteger('tip_type_id')->unsigned()->after('public')->default(1); |
||
183 | $table->foreign('tip_type_id')->references('tip_type_id')->on('tech_tip_types')->onUpdate('cascade'); |
||
184 | }); |
||
185 | |||
186 | // Move all of the system files over to the tech tips table |
||
187 | $sysFiles = DB::select('SELECT * FROM `system_files`'); |
||
188 | foreach($sysFiles as $sysFile) { |
||
189 | $newTip = TechTips::create([ |
||
190 | 'user_id' => $sysFile->user_id, |
||
191 | 'public' => 0, |
||
192 | 'tip_type_id' => 2, |
||
193 | 'subject' => $sysFile->name, |
||
194 | 'description' => empty($sysFile->description) ? $sysFile->name : $sysFile->description, |
||
195 | 'created_at' => $sysFile->created_at, |
||
196 | 'updated_at' => $sysFile->updated_at |
||
197 | ]); |
||
198 | |||
199 | $tipId = $newTip->tip_id; |
||
200 | TechTipFiles::create([ |
||
201 | 'tip_id' => $tipId, |
||
202 | 'file_id' => $sysFile->file_id |
||
203 | ]); |
||
204 | TechTipSystems::create([ |
||
205 | 'tip_id' => $tipId, |
||
206 | 'sys_id' => $sysFile->sys_id |
||
207 | ]); |
||
208 | } |
||
209 | } |
||
210 | } |
||
211 | |||
212 | /* |
||
213 | * Database Cleanup |
||
214 | */ |
||
215 | // Remove the NavBar view if it exists - We no longer need it to sort out the system types |
||
216 | private function removeNavBarView() |
||
217 | { |
||
218 | DB::statement('DROP VIEW IF EXISTS `navbar_view`'); |
||
219 | } |
||
220 | |||
221 | // Remove the active column from the customers table |
||
222 | private function removeActiveFromCustomers() |
||
223 | { |
||
224 | if(Schema::hasColumn('customers', 'active')) |
||
225 | { |
||
226 | // Migrate the existing disabled customers first |
||
227 | $deactivatedCustomers = Customers::where('active', 0)->get(); |
||
228 | foreach($deactivatedCustomers as $cust) |
||
229 | { |
||
230 | Customers::find($cust->cust_id)->delete(); |
||
231 | } |
||
232 | |||
233 | Schema::table('customers', function(Blueprint $table) { |
||
234 | $table->dropColumn('active'); |
||
235 | }); |
||
236 | } |
||
237 | } |
||
238 | |||
239 | // Remove the user roles tables |
||
240 | private function removeUserRolesTables() |
||
250 | } |
||
251 | } |
||
252 | |||
253 | // Remove the File Link Instructions Table |
||
254 | private function removeFileLinkInstructionsTable() |
||
263 | } |
||
264 | } |
||
265 | |||
266 | // Remove the file link files note table |
||
267 | private function removeFileLinkNotesTable() |
||
268 | { |
||
269 | if(Schema::hasTable('file_link_notes')) |
||
270 | { |
||
271 | Schema::table('file_link_notes', function(Blueprint $table) { |
||
272 | $table->dropForeign(['link_id']); |
||
273 | $table->dropForeign(['file_id']); |
||
274 | }); |
||
275 | Schema::dropIfExists('file_link_notes'); |
||
276 | } |
||
277 | } |
||
278 | |||
279 | // Remove the system files and system file types table |
||
280 | private function removeSystemFilesTables() |
||
281 | { |
||
282 | if(Schema::hasTable('system_files')) { |
||
283 | Schema::table('system_files', function(Blueprint $table) { |
||
284 | $table->dropForeign(['sys_id']); |
||
285 | $table->dropForeign(['type_id']); |
||
286 | $table->dropForeign(['file_id']); |
||
287 | $table->dropForeign(['user_id']); |
||
288 | }); |
||
289 | Schema::dropIfExists('system_files'); |
||
290 | Schema::dropIfExists('system_file_types'); |
||
291 | } |
||
292 | } |
||
293 | |||
294 | // Rename the tables that hold customer system information to make more sense |
||
295 | private function modifySystemDataTableNames() |
||
296 | { |
||
297 | if(Schema::hasTable('customer_system_fields')) |
||
298 | { |
||
299 | Schema::dropIfExists('customer_system_data'); |
||
300 | Schema::rename('customer_system_fields', 'customer_system_data'); |
||
301 | } |
||
302 | if(Schema::hasTable('system_cust_data_fields')) |
||
303 | { |
||
304 | Schema::dropIfExists('system_data_fields'); |
||
305 | Schema::rename('system_cust_data_fields', 'system_data_fields'); |
||
306 | } |
||
307 | if(Schema::hasTable('system_cust_data_types')) |
||
308 | { |
||
309 | Schema::dropIfExists('system_data_field_types'); |
||
310 | Schema::rename('system_cust_data_types', 'system_data_field_types'); |
||
311 | } |
||
312 | } |
||
313 | |||
314 | // Add soft deletes to customer systems table to prevent accidental deletes |
||
315 | private function addSoftDeleteToCustomerSystems() |
||
316 | { |
||
317 | if(!Schema::hasColumn('customer_systems', 'deleted_at')) |
||
318 | { |
||
319 | Schema::table('customer_systems', function(Blueprint $table) { |
||
320 | $table->softDeletes()->after('sys_id'); |
||
321 | }); |
||
322 | } |
||
323 | } |
||
324 | |||
325 | // Add soft deletes to tech tips table to prevent accidental deletes |
||
326 | private function addSoftDeleteToTechTips() |
||
331 | }); |
||
332 | } |
||
333 | } |
||
334 | |||
335 | // Swap out the active column for deleted at column on users table |
||
336 | public function addSoftDeleteToUsers() |
||
337 | { |
||
338 | if(!Schema::hasColumn('users', 'deleted_at')) |
||
339 | { |
||
353 |