Passed
Branch dev5 (d8a4d3)
by Ron
10:02
created

UpdatesForVersion50   B

Complexity

Total Complexity 52

Size/Duplication

Total Lines 382
Duplicated Lines 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 160
c 2
b 0
f 0
dl 0
loc 382
rs 7.44
wmc 52

How to fix   Complexity   

Complex Class

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
2
3
use App\User;
4
use App\TechTips;
5
use App\Customers;
6
use App\FileLinks;
7
use Carbon\Carbon;
8
use App\TechTipFiles;
9
use App\FileLinkFiles;
10
use App\TechTipSystems;
11
use App\PhoneNumberTypes;
12
use Illuminate\Support\Facades\Schema;
13
use Illuminate\Database\Schema\Blueprint;
14
use Illuminate\Database\Migrations\Migration;
15
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 Adds
27
        $this->addSoftDeleteToUsers();
28
        $this->addSoftDeleteToCustomerSystems();
29
        $this->addSoftDeleteToTechTips();
30
        $this->addPasswordExpiresColumn();
31
        $this->addHiddenColumn();
32
        $this->addColumnsToFileLinksTable();
33
        $this->addNotesColumnToFileLinkFiles();
34
35
        //  DB Modifications
36
        $this->updatePhoneIcons();
37
        $this->modifySystemDataTableNames();
38
        $this->migrateSystemDocumentation();
39
        $this->migrateUserRoles();
40
        $this->updateCustomersTable();
41
        $this->dropUserSettingsTrigger();
42
43
        //  Remove Unneeded Tables
44
        $this->removeNavBarView();
45
        $this->removeUserRolesTables();
46
        $this->removeFileLinkInstructionsTable();
47
        $this->removeFileLinkNotesTable();
48
        $this->removeSystemFilesTables();
49
        $this->removeActiveFromCustomers();
50
51
    }
52
53
    /**
54
     * Reverse the migrations.
55
     *
56
     * @return void
57
     */
58
    public function down()
59
    {
60
        return 'Unable to Process.  Please Downgrade app and load backup.';
61
    }
62
63
    /*
64
    *   Database Modifications
65
    */
66
    //  Update the icon class for Font Awesome 5.0
67
    private function updatePhoneIcons()
68
    {
69
        $newIcons = [
70
            ['description' => 'Home', 'icon_class' => 'fas fa-home'],
71
            ['description' => 'Work', 'icon_class' => 'fas fa-briefcase'],
72
            ['description' => 'Mobile', 'icon_class' => 'fas fa-mobile-alt'],
73
        ];
74
75
        foreach($newIcons as $new)
76
        {
77
            PhoneNumberTypes::where('description', $new['description'])->update(['icon_class' => $new['icon_class']]);
78
        }
79
    }
80
81
    /*
82
    *   New Columns and Tables
83
    */
84
    //  Added the ability to set an expiration date for user passwords - will force them to change after this expires
85
    private function addPasswordExpiresColumn()
86
    {
87
        if(!Schema::hasColumn('users', 'password_expires'))
88
        {
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 migrateUserRoles()
99
    {
100
        if(Schema::hasTable('user_role'))
101
        {
102
            if(!Schema::hasColumn('users', 'role_id'))
103
            {
104
                Schema::table('users', function(Blueprint $table) {
105
                    $table->integer('role_id')->after('user_id')->unsigned()->default(4);
106
                    $table->foreign('role_id')->references('role_id')->on('user_role_descriptions')->onUpdate('cascade');
107
                });
108
            }
109
110
            $roleData = DB::select('SELECT * FROM `user_role`');
111
112
            foreach($roleData as $data)
113
            {
114
                User::where('user_id', $data->user_id)->update([
115
                    'role_id' => $data->role_id,
116
                ]);
117
            }
118
        }
119
    }
120
121
    //  Added a 'hidden' attribute to system customer data types to allow passwords to not be viewed unless clicked or focus
122
    private function addHiddenColumn()
123
    {
124
        if(!Schema::hasColumn('system_data_field_types', 'hidden'))
125
        {
126
            Schema::table('system_cust_data_types', function(Blueprint $table) {
127
                $table->boolean('hidden')
128
                    ->default(0)
129
                    ->after('name');
130
            });
131
        }
132
    }
133
134
    //  Update the File links table - add cust_id and note column
135
    private function addColumnsToFileLinksTable()
136
    {
137
        if(!Schema::hasColumn('file_links', 'cust_id'))
138
        {
139
            Schema::table('file_links', function(Blueprint $table) {
140
                $table->integer('cust_id')
141
                    ->unsigned()
142
                    ->nullable()
143
                    ->after('user_id');
144
                $table->foreign('cust_id')->references('cust_id')->on('customers')->onUpdate('cascade')->onDelete('cascade');
145
            });
146
        }
147
        if(!Schema::hasColumn('file_links', 'note'))
148
        {
149
            Schema::table('file_links', function(Blueprint $table) {
150
                $table->longText('note')
151
                    ->nullable()
152
                    ->after('link_name');
153
            });
154
            //  Migrate the instructions from the old table to the new column
155
            $instructions = DB::select('SELECT * FROM `file_link_instructions`');
156
            foreach($instructions as $ins)
157
            {
158
                FileLinks::find($ins->link_id)->update([
159
                    'note' => $ins->instruction
160
                ]);
161
            }
162
        }
163
    }
164
165
    //  Add Notes column to the file links files table
166
    private function addNotesColumnToFileLinkFiles()
167
    {
168
        if(!Schema::hasColumn('file_link_files', 'note'))
169
        {
170
            Schema::table('file_link_files', function(Blueprint $table) {
171
                $table->longText('note')
172
                    ->nullable()
173
                    ->after('upload');
174
            });
175
            //  Migrate the existing notes to the new table
176
            $notes = DB::select('SELECT * FROM `file_link_notes`');
177
            foreach($notes as $note)
178
            {
179
                FileLinkFiles::where('file_id', $note->file_id)->update([
180
                    'note' => $note->note
181
                ]);
182
            }
183
        }
184
    }
185
186
    //  Add the documentation column to the tech tips table
187
    private function migrateSystemDocumentation()
188
    {
189
        if(!Schema::hasColumn('tech_tips', 'tip_type_id'))
190
        {
191
            Schema::table('tech_tips', function(Blueprint $table) {
192
                $table->bigInteger('tip_type_id')->unsigned()->after('public')->default(1);
193
                $table->foreign('tip_type_id')->references('tip_type_id')->on('tech_tip_types')->onUpdate('cascade');
194
            });
195
196
            //  Move all of the system files over to the tech tips table
197
            $sysFiles = DB::select('SELECT * FROM `system_files`');
198
            foreach($sysFiles as $sysFile)
199
            {
200
                $newTip = TechTips::create([
201
                    'user_id'       => $sysFile->user_id,
202
                    'public'        => 0,
203
                    'tip_type_id'   => 2,
204
                    'subject'       => $sysFile->name,
205
                    'description'   => empty($sysFile->description) ? $sysFile->name : $sysFile->description,
206
                    'created_at'    => $sysFile->created_at,
207
                    'updated_at'    => $sysFile->updated_at
208
                ]);
209
210
                $tipId = $newTip->tip_id;
211
                TechTipFiles::create([
212
                    'tip_id'  => $tipId,
213
                    'file_id' => $sysFile->file_id
214
                ]);
215
                TechTipSystems::create([
216
                    'tip_id' => $tipId,
217
                    'sys_id' => $sysFile->sys_id
218
                ]);
219
            }
220
        }
221
    }
222
223
    /*
224
    *   Database Cleanup
225
    */
226
    //  Remove the NavBar view if it exists - We no longer need it to sort out the system types
227
    private function removeNavBarView()
228
    {
229
        DB::statement('DROP VIEW IF EXISTS `navbar_view`');
230
    }
231
232
    //  Remove the active column from the customers table
233
    private function removeActiveFromCustomers()
234
    {
235
        if(Schema::hasColumn('customers', 'active'))
236
        {
237
            //  Migrate the existing disabled customers first
238
            $deactivatedCustomers = Customers::where('active', 0)->get();
239
            foreach($deactivatedCustomers as $cust)
240
            {
241
                Customers::find($cust->cust_id)->delete();
242
            }
243
244
            Schema::table('customers', function(Blueprint $table) {
245
                $table->dropColumn('active');
246
            });
247
        }
248
    }
249
250
    //  Remove the user roles tables
251
    private function removeUserRolesTables()
252
    {
253
        if(Schema::hasTable('user_role'))
254
        {
255
            Schema::table('user_role', function(Blueprint $table) {
256
                $table->dropForeign(['user_id']);
257
                $table->dropForeign(['role_id']);
258
            });
259
            Schema::dropIfExists('user_role');
260
            Schema::dropIfExists('roles');
261
        }
262
    }
263
264
    //  Remove the File Link Instructions Table
265
    private function removeFileLinkInstructionsTable()
266
    {
267
        if(Schema::hasTable('file_link_instructions'))
268
        {
269
            //  Remove the foreign key to allow for dropping the table
270
            Schema::table('file_link_instructions', function(Blueprint $table) {
271
                $table->dropForeign(['link_id']);
272
            });
273
            Schema::dropIfExists('file_link_instructions');
274
        }
275
    }
276
277
    //  Remove the file link files note table
278
    private function removeFileLinkNotesTable()
279
    {
280
        if(Schema::hasTable('file_link_notes'))
281
        {
282
            Schema::table('file_link_notes', function(Blueprint $table) {
283
                $table->dropForeign(['link_id']);
284
                $table->dropForeign(['file_id']);
285
            });
286
            Schema::dropIfExists('file_link_notes');
287
        }
288
    }
289
290
    //  Remove the system files and system file types table
291
    private function removeSystemFilesTables()
292
    {
293
        if(Schema::hasTable('system_files'))
294
        {
295
            Schema::table('system_files', function(Blueprint $table) {
296
                $table->dropForeign(['sys_id']);
297
                $table->dropForeign(['type_id']);
298
                $table->dropForeign(['file_id']);
299
                $table->dropForeign(['user_id']);
300
            });
301
            Schema::dropIfExists('system_files');
302
            Schema::dropIfExists('system_file_types');
303
        }
304
    }
305
306
    //  Rename the tables that hold customer system information to make more sense
307
    private function modifySystemDataTableNames()
308
    {
309
        if(Schema::hasTable('customer_system_fields'))
310
        {
311
            Schema::dropIfExists('customer_system_data');
312
            Schema::rename('customer_system_fields', 'customer_system_data');
313
        }
314
        if(Schema::hasTable('system_cust_data_fields'))
315
        {
316
            Schema::dropIfExists('system_data_fields');
317
            Schema::rename('system_cust_data_fields', 'system_data_fields');
318
        }
319
        if(Schema::hasTable('system_cust_data_types'))
320
        {
321
            Schema::dropIfExists('system_data_field_types');
322
            Schema::rename('system_cust_data_types', 'system_data_field_types');
323
        }
324
    }
325
326
    //  Add soft deletes to customer systems table to prevent accidental deletes
327
    private function addSoftDeleteToCustomerSystems()
328
    {
329
        if(!Schema::hasColumn('customer_systems', 'deleted_at'))
330
        {
331
            Schema::table('customer_systems', function(Blueprint $table) {
332
                $table->softDeletes()->after('sys_id');
333
            });
334
        }
335
    }
336
337
    //  Add soft deletes to tech tips table to prevent accidental deletes
338
    private function addSoftDeleteToTechTips()
339
    {
340
        if(!Schema::hasColumn('tech_tips', 'deleted_at')) {
341
            Schema::table('tech_tips', function(Blueprint $table) {
342
                $table->softDeletes()->after('description');
343
            });
344
        }
345
    }
346
347
    //  Swap out the active column for deleted at column on users table
348
    public function addSoftDeleteToUsers()
349
    {
350
        if(!Schema::hasColumn('users', 'deleted_at'))
351
        {
352
            Schema::table('users', function(Blueprint $table) {
353
                $table->softDeletes()->after('password_expires');
354
            });
355
            //  Migrate over all deactivated users
356
            DB::update('UPDATE `users` SET `deleted_at` = "'.Carbon::now().'" WHERE `active` = 0');
357
            //  Remove the Active column
358
            Schema::table('users', function(Blueprint $table) {
359
                $table->dropColumn('active');
360
            });
361
        }
362
    }
363
364
    //  Add the Parent id column to the customers table
365
    public function updateCustomersTable()
366
    {
367
        if(!Schema::hasColumn('customers', 'parent_id'))
368
        {
369
            Schema::table('customers', function(Blueprint $table)
370
            {
371
                $table->integer('parent_id')->after('cust_id')->nullable()->unsigned();
372
                $table->foreign('parent_id')->references('cust_id')->on('customers')->onUpdate('cascade');
373
            });
374
        }
375
        if(!Schema::hasColumn('customer_systems', 'shared'))
376
        {
377
            Schema::table('customer_systems', function(Blueprint $table)
378
            {
379
                $table->boolean('shared')->default(0)->after('sys_id');
380
            });
381
        }
382
        if(!Schema::hasColumn('customer_contacts', 'shared'))
383
        {
384
            Schema::table('customer_contacts', function(Blueprint $table)
385
            {
386
                $table->boolean('shared')->default(0)->after('cust_id');
387
            });
388
        }
389
        if(!Schema::hasColumn('customer_notes', 'shared'))
390
        {
391
            Schema::table('customer_notes', function(Blueprint $table)
392
            {
393
                $table->boolean('shared')->default(0)->after('user_id');
394
            });
395
        }
396
        if(!Schema::hasColumn('customer_files', 'shared')) {
397
            Schema::table('customer_files', function(Blueprint $table) {
398
                $table->boolean('shared')->default(0)->after('user_id');
399
            });
400
        }
401
    }
402
403
    //  Drop the create user settings trigger
404
    public function dropUserSettingsTrigger()
405
    {
406
        DB::unprepared('DROP TRIGGER IF EXISTS `tr_user_settings`');
407
    }
408
}
409