Passed
Push — dev5 ( b08d9a...c43caf )
by Ron
04:08
created

UpdatesForVersion50::up()   F

Complexity

Conditions 19
Paths 384

Size

Total Lines 235

Duplication

Lines 38
Ratio 16.17 %

Importance

Changes 0
Metric Value
dl 38
loc 235
rs 1.1066
c 0
b 0
f 0
cc 19
nc 384
nop 0

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
use App\FileLinks;
13
use App\FileLinkFiles;
14
15
class UpdatesForVersion50 extends Migration
16
{
17
    /**
18
     * Run the migrations.
19
     *
20
     * @return void
21
     */
22
    public function up()
23
    {
24
        //  Remove the Navbar view
25
        DB::statement('DROP VIEW IF EXISTS `navbar_view`');
26
27
        //  Update the icons in the phone number types table
28
        PhoneNumberTypes::find(1)->update(
29
        [
30
            'icon_class' => 'ti-home'
31
        ]);
32
        PhoneNumberTypes::find(2)->update(
33
        [
34
            'icon_class' => 'ti-briefcase'
35
        ]);
36
        PhoneNumberTypes::find(3)->update(
37
        [
38
            'icon_class' => 'ti-mobile'
39
        ]);
40
41
        //  Add the 'password expires' column to the users table
42
        if(!Schema::hasColumn('users', 'password_expires'))
43
        {
44
            Schema::table('users', function(Blueprint $table) {
45
                $table->timestamp('password_expires')
46
                    ->nullable()
47
                    ->after('active');
48
            });
49
        }
50
51
        //  Add the 'hidden' column to the system_cust_data_types table
52
        if(!Schema::hasColumn('system_cust_data_types', 'hidden'))
53
        {
54
            Schema::table('system_cust_data_types', function(Blueprint $table) {
55
                $table->boolean('hidden')
56
                    ->default(0)
57
                    ->after('name');
58
            });
59
        }
60
61
        //  Add the cust id and note colunns to the file_links table
62
        if(!Schema::hasColumn('file_links', 'cust_id'))
63
        {
64
            Schema::table('file_links', function(Blueprint $table) {
65
                $table->integer('cust_id')
66
                    ->unsigned()
67
                    ->nullable()
68
                    ->after('user_id');
69
                $table->foreign('cust_id')->references('cust_id')->on('customers')->onUpdate('cascade')->onDelete('cascade');
0 ignored issues
show
Bug introduced by
The method references does only exist in Illuminate\Database\Schema\ForeignKeyDefinition, but not in Illuminate\Support\Fluent.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
70
            });
71
        }
72 View Code Duplication
        if(!Schema::hasColumn('file_links', 'note'))
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
73
        {
74
            Schema::table('file_links', function(Blueprint $table) {
75
                $table->longText('note')
76
                    ->nullable()
77
                    ->after('link_name');
78
            });
79
            $instructions = DB::select('SELECT * FROM `file_link_instructions`');
80
            foreach($instructions as $ins)
81
            {
82
                FileLinksDB::find($ins->link_id)->update([
83
                    'note' => $ins->instruction
84
                ]);
85
            }
86
            Schema::table('file_link_instructions', function (Blueprint $table) {
87
                $table->dropForeign(['link_id']);
88
            });
89
            Schema::dropIfExists('file_link_instructions');
90
        }
91
        //  Add "notes" column to the file link files table
92 View Code Duplication
        if (!Schema::hasColumn('file_link_files', 'note')) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
93
            Schema::table('file_link_files', function (Blueprint $table) {
94
                $table->longText('note')
95
                    ->nullable()
96
                    ->after('upload');
97
            });
98
            $notes = DB::select('SELECT * FROM `file_link_notes`');
99
            foreach($notes as $note)
100
            {
101
                FileLinkFiles::where('file_id', $note->file_id)->update([
102
                    'note' => $note->note
103
                ]);
104
            }
105
            Schema::table('file_link_notes', function (Blueprint $table) {
106
                $table->dropForeign(['link_id']);
107
                $table->dropForeign(['file_id']);
108
            });
109
            Schema::dropIfExists('file_link_notes');
110
        }
111
112
        //  Add the 'documentation' column in the tech_tips table
113
        if(!Schema::hasColumn('tech_tips', 'documentation'))
114
        {
115
            Schema::table('tech_tips', function(Blueprint $table) {
116
                $table->boolean('documentation')->default(0)->nullable()->after('public');
117
            });
118
119
            //  Move all of the system files over to the tech tips table
120
            $sysFiles = DB::select('SELECT * FROM `system_files`');
121
            foreach($sysFiles as $sysFile)
122
            {
123
                $newTip = TechTips::create([
124
                    'user_id'       => $sysFile->user_id,
125
                    'public'        => 0,
126
                    'documentation' => 1,
127
                    'subject'       => $sysFile->name,
128
                    'description'   => empty($sysFile->description) ? $sysFile->name : $sysFile->description,
129
                    'created_at'    => $sysFile->created_at,
130
                    'updated_at'    => $sysFile->updated_at
131
                ]);
132
133
                $tipId = $newTip->tip_id;
134
                TechTipFiles::create([
135
                    'tip_id'  => $tipId,
136
                    'file_id' => $sysFile->file_id
137
                ]);
138
                TechTipSystems::create([
139
                    'tip_id' => $tipId,
140
                    'sys_id' => $sysFile->sys_id
141
                ]);
142
            }
143
        }
144
145
        //  Add the 'is_installer' column in the users table
146
        if(!Schema::hasColumn('users', 'is_installer'))
147
        {
148
            Schema::table('users', function(Blueprint $table) {
149
                $table->boolean('is_installer')->default(0)->after('active');
150
            });
151
152
            //  Migrate user roles from the 'user roles' table to the new 'user permissions' table
153
            if(Schema::hasTable('user_permissions') && (UserPermissions::all()->isEmpty()))
154
            {
155
                $userRoles = DB::select('SELECT * FROM `user_role` LEFT JOIN `roles` ON `user_role`.`role_id` = `roles`.`role_id`');
156
157
                foreach($userRoles as $user)
158
                {
159
                    if($user->name === 'Installer')
160
                    {
161
                        User::find($user->user_id)->update(
162
                        [
163
                            'is_installer' => 1
164
                        ]);
165
                        UserPermissions::create(
166
                        [
167
                            'user_id'             => $user->user_id,
168
                            'manage_users'        => 1,
169
                            'run_reports'         => 1,
170
                            'add_customer'        => 1,
171
                            'deactivate_customer' => 1,
172
                            'use_file_links'      => 1,
173
                            'create_tech_tip'     => 1,
174
                            'edit_tech_tip'       => 1,
175
                            'delete_tech_tip'     => 1,
176
                            'create_category'     => 1,
177
                            'modify_category'     => 1
178
                        ]);
179
                    }
180
                    else if($user->name === 'Admin')
181
                    {
182
                        UserPermissions::create(
183
                        [
184
                            'user_id'             => $user->user_id,
185
                            'manage_users'        => 1,
186
                            'run_reports'         => 1,
187
                            'add_customer'        => 1,
188
                            'deactivate_customer' => 1,
189
                            'use_file_links'      => 1,
190
                            'create_tech_tip'     => 1,
191
                            'edit_tech_tip'       => 1,
192
                            'delete_tech_tip'     => 1,
193
                            'create_category'     => 1,
194
                            'modify_category'     => 1
195
                        ]);
196
                    }
197
                    else if($user->name === 'Report')
198
                    {
199
                        UserPermissions::create(
200
                        [
201
                            'user_id'             => $user->user_id,
202
                            'manage_users'        => 0,
203
                            'run_reports'         => 1,
204
                            'add_customer'        => 1,
205
                            'deactivate_customer' => 0,
206
                            'use_file_links'      => 1,
207
                            'create_tech_tip'     => 1,
208
                            'edit_tech_tip'       => 0,
209
                            'delete_tech_tip'     => 0,
210
                            'create_category'     => 0,
211
                            'modify_category'     => 0
212
                        ]);
213
                    }
214
                    else
215
                    {
216
                        UserPermissions::create(
217
                        [
218
                            'user_id'             => $user->user_id,
219
                            'manage_users'        => 0,
220
                            'run_reports'         => 0,
221
                            'add_customer'        => 1,
222
                            'deactivate_customer' => 0,
223
                            'use_file_links'      => 1,
224
                            'create_tech_tip'     => 1,
225
                            'edit_tech_tip'       => 0,
226
                            'delete_tech_tip'     => 0,
227
                            'create_category'     => 0,
228
                            'modify_category'     => 0
229
                        ]);
230
                    }
231
                }
232
                Schema::table('user_role', function(Blueprint $table) {
233
                    $table->dropForeign(['user_id']);
234
                    $table->dropForeign(['role_id']);
235
                });
236
                Schema::dropIfExists('user_role');
237
                Schema::dropIfExists('roles');
238
            }
239
        }
240
241
        //  Remove the system_files and system_file_types table
242
        if(Schema::hasTable('system_files'))
243
        {
244
            Schema::table('system_files', function(Blueprint $table) {
245
                $table->dropForeign(['sys_id']);
246
                $table->dropForeign(['type_id']);
247
                $table->dropForeign(['file_id']);
248
                $table->dropForeign(['user_id']);
249
            });
250
            Schema::dropIfExists('system_files');
251
            Schema::dropIfExists('system_file_types');
252
        }
253
254
255
256
    }
257
258
    /**
259
     * Reverse the migrations.
260
     *
261
     * @return void
262
     */
263
    public function down()
264
    {
265
        if(Schema::hasColumn('file_links', 'cust_id'))
266
        {
267
            Schema::table('file_links', function(Blueprint $table) {
268
                $table->dropForeign(['cust_id']);
269
                $table->dropColumn('cust_id');
270
            });
271
        }
272
273
        if(!Schema::hasColumn('file_links', 'note'))
274
        {
275
            Schema::table('file_links', function(Blueprint $table) {
276
                $table->dropColumn('note');
277
            });
278
        }
279
280
        if(Schema::hasColumn('tech_tips', 'documentation'))
281
        {
282
            Schema::table('tech_tips', function(Blueprint $table) {
283
                $table->dropColumn('documentation');
284
            });
285
        }
286
    }
287
}
288