Completed
Push — master ( d62dde...2f9ece )
by Sherif
04:27
created

Initialize::up()   B

Complexity

Conditions 2
Paths 2

Size

Total Lines 251
Code Lines 43

Duplication

Lines 0
Ratio 0 %

Importance

Changes 5
Bugs 0 Features 0
Metric Value
dl 0
loc 251
rs 8.2857
c 5
b 0
f 0
cc 2
eloc 43
nc 2
nop 0

How to fix   Long Method   

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 Illuminate\Database\Schema\Blueprint;
4
use Illuminate\Database\Migrations\Migration;
5
6
class Initialize extends Migration
0 ignored issues
show
Coding Style Compatibility introduced by
PSR1 recommends that each class must be in a namespace of at least one level to avoid collisions.

You can fix this by adding a namespace to your class:

namespace YourVendor;

class YourClass { }

When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.

Loading history...
7
{
8
	/**
9
	 * Run the migrations.
10
	 *
11
	 * @return void
12
	 */
13
	public function up()
14
	{
15
        /**
16
         * Delete previous permissions.
17
         */
18
		DB::table('permissions')->whereIn('model', ['users', 'permissions', 'groups'])->delete();
19
20
		/**
21
         * Insert the permissions related to this module.
22
         */
23
        DB::table('permissions')->insert(
24
        	[
25
        		/**
26
        		 * Users model permissions.
27
        		 */
28
	        	[
29
	        	'name'       => 'save',
30
	        	'model'      => 'users',
31
	        	'created_at' => \DB::raw('NOW()'),
32
	        	'updated_at' => \DB::raw('NOW()')
33
	        	],
34
	        	[
35
	        	'name'       => 'delete',
36
	        	'model'      => 'users',
37
	        	'created_at' => \DB::raw('NOW()'),
38
	        	'updated_at' => \DB::raw('NOW()')
39
	        	],
40
	        	[
41
	        	'name'       => 'find',
42
	        	'model'      => 'users',
43
	        	'created_at' => \DB::raw('NOW()'),
44
	        	'updated_at' => \DB::raw('NOW()')
45
	        	],
46
	        	[
47
	        	'name'       => 'list',
48
	        	'model'      => 'users',
49
	        	'created_at' => \DB::raw('NOW()'),
50
	        	'updated_at' => \DB::raw('NOW()')
51
	        	],
52
	        	[
53
	        	'name'       => 'search',
54
	        	'model'      => 'users',
55
	        	'created_at' => \DB::raw('NOW()'),
56
	        	'updated_at' => \DB::raw('NOW()')
57
	        	],
58
	        	[
59
	        	'name'       => 'findby',
60
	        	'model'      => 'users',
61
	        	'created_at' => \DB::raw('NOW()'),
62
	        	'updated_at' => \DB::raw('NOW()')
63
	        	],
64
	        	[
65
	        	'name'       => 'first',
66
	        	'model'      => 'users',
67
	        	'created_at' => \DB::raw('NOW()'),
68
	        	'updated_at' => \DB::raw('NOW()')
69
	        	],
70
	        	[
71
	        	'name'       => 'paginate',
72
	        	'model'      => 'users',
73
	        	'created_at' => \DB::raw('NOW()'),
74
	        	'updated_at' => \DB::raw('NOW()')
75
	        	],
76
	        	[
77
	        	'name'       => 'paginateby',
78
	        	'model'      => 'users',
79
	        	'created_at' => \DB::raw('NOW()'),
80
	        	'updated_at' => \DB::raw('NOW()')
81
	        	],
82
	        	[
83
	        	'name'       => 'assigngroup',
84
	        	'model'      => 'users',
85
	        	'created_at' => \DB::raw('NOW()'),
86
	        	'updated_at' => \DB::raw('NOW()')
87
	        	],
88
89
	        	/**
90
        		 * Permissions model permissions.
91
        		 */
92
        		[
93
	        	'name'       => 'find',
94
	        	'model'      => 'permissions',
95
	        	'created_at' => \DB::raw('NOW()'),
96
	        	'updated_at' => \DB::raw('NOW()')
97
	        	],
98
	        	[
99
	        	'name'       => 'search',
100
	        	'model'      => 'permissions',
101
	        	'created_at' => \DB::raw('NOW()'),
102
	        	'updated_at' => \DB::raw('NOW()')
103
	        	],
104
	        	[
105
	        	'name'       => 'list',
106
	        	'model'      => 'permissions',
107
	        	'created_at' => \DB::raw('NOW()'),
108
	        	'updated_at' => \DB::raw('NOW()')
109
	        	],
110
	        	[
111
	        	'name'       => 'findby',
112
	        	'model'      => 'permissions',
113
	        	'created_at' => \DB::raw('NOW()'),
114
	        	'updated_at' => \DB::raw('NOW()')
115
	        	],
116
	        	[
117
	        	'name'       => 'first',
118
	        	'model'      => 'permissions',
119
	        	'created_at' => \DB::raw('NOW()'),
120
	        	'updated_at' => \DB::raw('NOW()')
121
	        	],
122
	        	[
123
	        	'name'       => 'paginate',
124
	        	'model'      => 'permissions',
125
	        	'created_at' => \DB::raw('NOW()'),
126
	        	'updated_at' => \DB::raw('NOW()')
127
	        	],
128
	        	[
129
	        	'name'       => 'paginateby',
130
	        	'model'      => 'permissions',
131
	        	'created_at' => \DB::raw('NOW()'),
132
	        	'updated_at' => \DB::raw('NOW()')
133
	        	],
134
135
	        	/**
136
        		 * Groups model permissions.
137
        		 */
138
	        	[
139
	        	'name'       => 'save',
140
	        	'model'      => 'groups',
141
	        	'created_at' => \DB::raw('NOW()'),
142
	        	'updated_at' => \DB::raw('NOW()')
143
	        	],
144
	        	[
145
	        	'name'       => 'delete',
146
	        	'model'      => 'groups',
147
	        	'created_at' => \DB::raw('NOW()'),
148
	        	'updated_at' => \DB::raw('NOW()')
149
	        	],
150
	        	[
151
	        	'name'       => 'find',
152
	        	'model'      => 'groups',
153
	        	'created_at' => \DB::raw('NOW()'),
154
	        	'updated_at' => \DB::raw('NOW()')
155
	        	],
156
	        	[
157
	        	'name'       => 'search',
158
	        	'model'      => 'groups',
159
	        	'created_at' => \DB::raw('NOW()'),
160
	        	'updated_at' => \DB::raw('NOW()')
161
	        	],
162
	        	[
163
	        	'name'       => 'list',
164
	        	'model'      => 'groups',
165
	        	'created_at' => \DB::raw('NOW()'),
166
	        	'updated_at' => \DB::raw('NOW()')
167
	        	],
168
	        	[
169
	        	'name'       => 'findby',
170
	        	'model'      => 'groups',
171
	        	'created_at' => \DB::raw('NOW()'),
172
	        	'updated_at' => \DB::raw('NOW()')
173
	        	],
174
	        	[
175
	        	'name'       => 'first',
176
	        	'model'      => 'groups',
177
	        	'created_at' => \DB::raw('NOW()'),
178
	        	'updated_at' => \DB::raw('NOW()')
179
	        	],
180
	        	[
181
	        	'name'       => 'paginate',
182
	        	'model'      => 'groups',
183
	        	'created_at' => \DB::raw('NOW()'),
184
	        	'updated_at' => \DB::raw('NOW()')
185
	        	],
186
	        	[
187
	        	'name'       => 'paginateby',
188
	        	'model'      => 'groups',
189
	        	'created_at' => \DB::raw('NOW()'),
190
	        	'updated_at' => \DB::raw('NOW()')
191
	        	],
192
	        	[
193
	        	'name'       => 'assignpermissions',
194
	        	'model'      => 'groups',
195
	        	'created_at' => \DB::raw('NOW()'),
196
	        	'updated_at' => \DB::raw('NOW()')
197
	        	],
198
        	]
199
        );
200
201
		/**
202
		 * Delete previous data.
203
		 */
204
		$adminGroupId = DB::table('groups')->where('name', 'Admin')->first()->id;
205
		$adminUserId  = DB::table('users')->where('email', '[email protected]')->first()->id;
206
		DB::table('users_groups')->where('user_id', $adminUserId)->where('group_id', $adminGroupId)->delete();
207
		DB::table('users')->where('email', '[email protected]')->delete();
208
		DB::table('groups')->where('name', 'admin')->delete();
209
210
		/**
211
		 * Create Default groups.
212
		 */
213
		$adminGroupId = DB::table('groups')->insertGetId(
214
			[
215
			'name'       => 'Admin',
216
			'created_at' => \DB::raw('NOW()'),
217
			'updated_at' => \DB::raw('NOW()')
218
			]
219
		);
220
221
		/**
222
		 * Create Default users.
223
		 */
224
		$adminUserId = DB::table('users')->insertGetId(
225
            [
226
			'email'      => '[email protected]',
227
			'password'   => bcrypt('123456'),
228
			'created_at' => \DB::raw('NOW()'),
229
			'updated_at' => \DB::raw('NOW()')
230
			]
231
        );
232
233
		/**
234
		 * Assign users to groups.
235
		 */
236
		DB::table('users_groups')->insert(
237
        	[
238
	            [
239
				'user_id'    => $adminUserId,
240
				'group_id'   => $adminGroupId,
241
				'created_at' => \DB::raw('NOW()'),
242
				'updated_at' => \DB::raw('NOW()')
243
	            ]
244
        	]
245
        );
246
247
        /**
248
		 * Assign the permissions to the admin group.
249
		 */
250
		$permissionIds = DB::table('permissions')->whereIn('model', ['users', 'permissions', 'groups'])->select('id')->lists('id');
251
		$adminGroupId  = DB::table('groups')->where('name', 'Admin')->first()->id;
252
		foreach ($permissionIds as $permissionId) 
253
		{
254
			DB::table('groups_permissions')->insert(
255
				[
256
				'permission_id' => $permissionId,
257
				'group_id'      => $adminGroupId,
258
				'created_at'    => \DB::raw('NOW()'),
259
				'updated_at'    => \DB::raw('NOW()')
260
				]
261
			);
262
		}
263
	}
264
265
	/**
266
	 * Reverse the migrations.
267
	 *
268
	 * @return void
269
	 */
270
	public function down()
271
	{
272
		$adminGroupId = DB::table('groups')->where('name', 'Admin')->first()->id;
273
		$adminUserId  = DB::table('users')->where('email', '[email protected]')->first()->id;
274
275
		DB::table('permissions')->whereIn('model', ['users', 'permissions', 'groups'])->delete();
276
		DB::table('users_groups')->where('user_id', $adminUserId)->where('group_id', $adminGroupId)->delete();
277
		DB::table('users')->where('email', '[email protected]')->delete();
278
		DB::table('groups')->where('name', 'admin')->delete();
279
	}
280
}