Completed
Push — dev5 ( f3ede3...502bc5 )
by Ron
08:16
created

UpdatesForVersion50::up()   F

Complexity

Conditions 16
Paths 192

Size

Total Lines 203
Code Lines 122

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 16
eloc 122
c 1
b 0
f 0
nc 192
nop 0
dl 0
loc 203
rs 3.84

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
use App\User;
4
use App\TechTips;
5
use App\TechTipFiles;
6
use App\TechTipSystems;
7
use App\UserPermissions;
8
use App\PhoneNumberTypes;
9
use Illuminate\Support\Facades\Schema;
10
use Illuminate\Database\Schema\Blueprint;
11
use Illuminate\Database\Migrations\Migration;
12
13
class UpdatesForVersion50 extends Migration
14
{
15
    /**
16
     * Run the migrations.
17
     *
18
     * @return void
19
     */
20
    public function up()
21
    {
22
        //  Remove the Navbar view
23
        DB::statement('DROP VIEW IF EXISTS `navbar_view`');
24
        
25
        //  Update the icons in the phone number types table
26
        PhoneNumberTypes::find(1)->update(
27
        [
28
            'icon_class' => 'ti-home'
29
        ]);
30
        PhoneNumberTypes::find(2)->update(
31
        [
32
            'icon_class' => 'ti-briefcase'
33
        ]);
34
        PhoneNumberTypes::find(3)->update(
35
        [
36
            'icon_class' => 'ti-mobile'
37
        ]);
38
        
39
        //  Add the 'password expires' column to the users table
40
        if(!Schema::hasColumn('users', 'password_expires'))
41
        {
42
            Schema::table('users', function(Blueprint $table) {
43
                $table->timestamp('password_expires')
44
                    ->nullable()
45
                    ->after('active');
46
            });
47
        }
48
        
49
        //  Add the 'hidden' column to the system_cust_data_types table
50
        if(!Schema::hasColumn('system_cust_data_types', 'hidden'))
51
        {
52
            Schema::table('system_cust_data_types', function(Blueprint $table) {
53
                $table->boolean('hidden')
54
                    ->default(0)
55
                    ->after('name');
56
            });
57
        }
58
        
59
        //  Add the cust id and note colunns to the file_links table
60
        if(!Schema::hasColumn('file_links', 'cust_id'))
61
        {
62
            Schema::table('file_links', function(Blueprint $table) {
63
                $table->integer('cust_id')
64
                    ->unsigned()
65
                    ->nullable()
66
                    ->after('user_id');
67
                $table->foreign('cust_id')->references('cust_id')->on('customers')->onUpdate('cascade')->onDelete('cascade');
68
            });
69
        }
70
        if(!Schema::hasColumn('file_links', 'note'))
71
        {
72
            Schema::table('file_links', function(Blueprint $table) {
73
                $table->longText('note')
74
                    ->nullable()
75
                    ->after('link_name');
76
            });
77
        }
78
        
79
        //  Add the 'documentation' column in the tech_tips table
80
        if(!Schema::hasColumn('tech_tips', 'documentation'))
81
        {
82
            Schema::table('tech_tips', function(Blueprint $table) 
83
            {
84
                $table->boolean('documentation')->default(0)->nullable()->after('public');
85
            });
86
            
87
            //  Move all of the system files over to the tech tips table
88
            $sysFiles = DB::select('SELECT * FROM `system_files`');
89
            foreach($sysFiles as $sysFile)
90
            {
91
                $newTip = TechTips::create([
92
                    'user_id'       => $sysFile->user_id,
93
                    'public'        => 0,
94
                    'documentation' => 1,
95
                    'subject'       => $sysFile->name,
96
                    'description'   => empty($sysFile->description) ? $sysFile->name : $sysFile->description,
97
                    'created_at'    => $sysFile->created_at,
98
                    'updated_at'    => $sysFile->updated_at
99
                ]);
100
101
                $tipId = $newTip->tip_id;
102
                TechTipFiles::create([
103
                    'tip_id'  => $tipId,
104
                    'file_id' => $sysFile->file_id
105
                ]);
106
                TechTipSystems::create([
107
                    'tip_id' => $tipId,
108
                    'sys_id' => $sysFile->sys_id
109
                ]);
110
            }
111
        }
112
        
113
        //  Add the 'is_installer' column in the users table
114
        if(!Schema::hasColumn('users', 'is_installer'))
115
        {
116
            Schema::table('users', function(Blueprint $table)
117
            {
118
                $table->boolean('is_installer')->default(0)->after('active');
119
            });
120
            
121
            //  Migrate user roles from the 'user roles' table to the new 'user permissions' table
122
            if(Schema::hasTable('user_permissions') && (UserPermissions::all()->isEmpty()))
123
            {
124
                $userRoles = DB::select('SELECT * FROM `user_role` LEFT JOIN `roles` ON `user_role`.`role_id` = `roles`.`role_id`');
125
                
126
                foreach($userRoles as $user)
127
                {
128
                    if($user->name === 'Installer')
129
                    {
130
                        User::find($user->user_id)->update(
131
                        [
132
                            'is_installer' => 1
133
                        ]);
134
                        UserPermissions::create(
135
                        [
136
                            'user_id'             => $user->user_id,
137
                            'manage_users'        => 1,
138
                            'run_reports'         => 1,
139
                            'add_customer'        => 1,
140
                            'deactivate_customer' => 1,
141
                            'use_file_links'      => 1,
142
                            'create_tech_tip'     => 1,
143
                            'edit_tech_tip'       => 1,
144
                            'delete_tech_tip'     => 1,
145
                            'create_category'     => 1,
146
                            'modify_category'     => 1
147
                        ]);
148
                    }
149
                    else if($user->name === 'Admin')
150
                    {
151
                        UserPermissions::create(
152
                        [
153
                            'user_id'             => $user->user_id,
154
                            'manage_users'        => 1,
155
                            'run_reports'         => 1,
156
                            'add_customer'        => 1,
157
                            'deactivate_customer' => 1,
158
                            'use_file_links'      => 1,
159
                            'create_tech_tip'     => 1,
160
                            'edit_tech_tip'       => 1,
161
                            'delete_tech_tip'     => 1,
162
                            'create_category'     => 1,
163
                            'modify_category'     => 1
164
                        ]);
165
                    }
166
                    else if($user->name === 'Report')
167
                    {
168
                        UserPermissions::create(
169
                        [
170
                            'user_id'             => $user->user_id,
171
                            'manage_users'        => 0,
172
                            'run_reports'         => 1,
173
                            'add_customer'        => 1,
174
                            'deactivate_customer' => 0,
175
                            'use_file_links'      => 1,
176
                            'create_tech_tip'     => 1,
177
                            'edit_tech_tip'       => 0,
178
                            'delete_tech_tip'     => 0,
179
                            'create_category'     => 0,
180
                            'modify_category'     => 0
181
                        ]);
182
                    }
183
                    else
184
                    {
185
                        UserPermissions::create(
186
                        [
187
                            'user_id'             => $user->user_id,
188
                            'manage_users'        => 0,
189
                            'run_reports'         => 0,
190
                            'add_customer'        => 1,
191
                            'deactivate_customer' => 0,
192
                            'use_file_links'      => 1,
193
                            'create_tech_tip'     => 1,
194
                            'edit_tech_tip'       => 0,
195
                            'delete_tech_tip'     => 0,
196
                            'create_category'     => 0,
197
                            'modify_category'     => 0
198
                        ]);
199
                    }
200
                }
201
                Schema::table('user_role', function(Blueprint $table)
202
                {
203
                    $table->dropForeign(['user_id']);
204
                    $table->dropForeign(['role_id']);
205
                });
206
                Schema::dropIfExists('user_role');
207
                Schema::dropIfExists('roles');
208
            }
209
        }
210
        
211
        //  Remove the system_files and system_file_types table
212
        if(Schema::hasTable('system_files'))
213
        {
214
            Schema::table('system_files', function(Blueprint $table)
215
            {
216
                $table->dropForeign(['sys_id']);
217
                $table->dropForeign(['type_id']);
218
                $table->dropForeign(['file_id']);
219
                $table->dropForeign(['user_id']);
220
            });
221
            Schema::dropIfExists('system_files');
222
            Schema::dropIfExists('system_file_types');
223
        }
224
        
225
    }
226
227
    /**
228
     * Reverse the migrations.
229
     *
230
     * @return void
231
     */
232
    public function down()
233
    {   
234
        if(Schema::hasColumn('file_links', 'cust_id'))
235
        {
236
            Schema::table('file_links', function(Blueprint $table) {
237
                $table->dropForeign(['cust_id']);
238
                $table->dropColumn('cust_id');
239
            });
240
        }
241
        
242
        if(!Schema::hasColumn('file_links', 'note'))
243
        {
244
            Schema::table('file_links', function(Blueprint $table) {
245
                $table->dropColumn('note');
246
            });
247
        }
248
        
249
        if(Schema::hasColumn('tech_tips', 'documentation'))
250
        {
251
            Schema::table('tech_tips', function(Blueprint $table) 
252
            {
253
                $table->dropColumn('documentation');
254
            });
255
        }
256
    }
257
}
258