RoleSeeder::run()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 51
Code Lines 35

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 35
c 0
b 0
f 0
dl 0
loc 51
rs 9.36
cc 1
nc 1
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
namespace Database\Seeders;
3
4
use Illuminate\Database\Seeder;
5
use Itstructure\LaRbac\Models\{Role, Permission};
6
7
/**
8
 * Class RoleSeeder
9
 *
10
 * @author Andrey Girnik <[email protected]>
11
 */
12
class RoleSeeder extends Seeder
13
{
14
    /**
15
     * Run the database seeds.
16
     * @return void
17
     */
18
    public function run()
19
    {
20
        $totalPermissionId   = Permission::where('slug', Permission::ADMINISTRATE_PERMISSION)->firstOrFail()->id;
21
        $viewPermissionId    = Permission::where('slug', Permission::VIEW_RECORD_PERMISSION)->firstOrFail()->id;
22
        $createPermissionId  = Permission::where('slug', Permission::CREATE_RECORD_PERMISSION)->firstOrFail()->id;
23
        $updatePermissionId  = Permission::where('slug', Permission::UPDATE_RECORD_PERMISSION)->firstOrFail()->id;
24
        $deletePermissionId  = Permission::where('slug', Permission::DELETE_RECORD_PERMISSION)->firstOrFail()->id;
25
        $publishPermissionId = Permission::where('slug', Permission::PUBLISH_RECORD_PERMISSION)->firstOrFail()->id;
26
27
        $this->createRecord(
28
            Role::ADMIN_ROLE,
29
            'Administrator',
30
            [
31
                $totalPermissionId,
32
                $viewPermissionId,
33
                $createPermissionId,
34
                $updatePermissionId,
35
                $deletePermissionId,
36
                $publishPermissionId
37
            ]
38
        );
39
40
        $this->createRecord(
41
            Role::MANAGER_ROLE,
42
            'Content manager',
43
            [
44
                $viewPermissionId,
45
                $createPermissionId,
46
                $updatePermissionId,
47
                $deletePermissionId,
48
                $publishPermissionId
49
            ]
50
        );
51
52
        $this->createRecord(
53
            Role::EDITOR_ROLE,
54
            'Record editor',
55
            [
56
                $viewPermissionId,
57
                $createPermissionId,
58
                $updatePermissionId,
59
                $deletePermissionId,
60
                $publishPermissionId
61
            ]
62
        );
63
64
        $this->createRecord(
65
            Role::USER_ROLE,
66
            'Simple user',
67
            [
68
                $viewPermissionId
69
            ]
70
        );
71
    }
72
73
    /**
74
     * Create role record in database.
75
     * @param string $slug
76
     * @param string $description
77
     * @param array $permissions
78
     * @return void
79
     */
80
    private function createRecord(string $slug, string $description, array $permissions): void
81
    {
82
        Role::create([
83
            'name' => ucfirst($slug),
84
            'slug' => $slug,
85
            'description' => $description,
86
        ])->permissions()
87
            ->attach($permissions);
88
    }
89
}
90