Completed
Push — master ( 6ee53d...bb5f21 )
by ARCANEDEV
04:30
created

RolesTableSeeder::syncRoles()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 18
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 18
rs 9.4285
ccs 0
cts 15
cp 0
cc 2
eloc 11
nc 2
nop 0
crap 6
1
<?php namespace Arcanesoft\Pages\Seeds;
2
3
use Arcanesoft\Auth\Models\Role;
4
use Arcanesoft\Auth\Models\Permission;
5
use Arcanesoft\Auth\Seeds\RolesSeeder;
6
7
/**
8
 * Class     RolesTableSeeder
9
 *
10
 * @package  Arcanesoft\Pages\Seeds
11
 * @author   ARCANEDEV <[email protected]>
12
 */
13
class RolesTableSeeder extends RolesSeeder
14
{
15
    /* ------------------------------------------------------------------------------------------------
16
     |  Main Functions
17
     | ------------------------------------------------------------------------------------------------
18
     */
19
    /**
20
     * Run the database seeds.
21
     */
22
    public function run()
23
    {
24
        $this->seed([
25
            [
26
                'name'        => 'Pages Moderators',
27
                'description' => 'The Pages moderators role.',
28
                'is_locked'   => true,
29
            ],[
30
                'name'        => 'Pages Authors',
31
                'description' => 'The Pages authors role.',
32
                'is_locked'   => true,
33
            ]
34
        ]);
35
        $this->syncAdminRole();
36
        $this->syncRoles();
37
    }
38
39
    /* ------------------------------------------------------------------------------------------------
40
     |  Other Functions
41
     | ------------------------------------------------------------------------------------------------
42
     */
43
    /**
44
     * Sync the roles.
45
     *
46
     * @todo: Refactor this method
47
     */
48
    private function syncRoles()
49
    {
50
        $permissions = Permission::all();
51
        $roles       = [
52
            'pages-moderators' => 'pages.',
53
            'pages-authors'    => 'pages.pages.',
54
        ];
55
56
        foreach ($roles as $roleSlug => $permissionSlug) {
57
            /** @var  \Arcanesoft\Auth\Models\Role  $role */
58
            $role = Role::where('slug', $roleSlug)->first();
59
            $ids  = $permissions->filter(function (Permission $permission) use ($permissionSlug) {
60
                return starts_with($permission->slug, $permissionSlug);
61
            })->lists('id')->toArray();
62
63
            $role->permissions()->sync($ids);
64
        }
65
    }
66
}
67