Completed
Push — master ( 96b827...e9016c )
by ARCANEDEV
15:51
created

RolesTableSeeder   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 53
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 0
Metric Value
wmc 4
c 0
b 0
f 0
lcom 1
cbo 3
dl 0
loc 53
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A run() 0 15 1
A syncRoles() 0 15 3
1
<?php namespace Arcanesoft\Seo\Seeds;
2
3
use Arcanesoft\Auth\Models\Role;
4
use Arcanesoft\Auth\Models\Permission;
5
use Arcanesoft\Auth\Seeds\RolesSeeder;
6
use Illuminate\Support\Str;
7
8
/**
9
 * Class     RolesTableSeeder
10
 *
11
 * @package  Arcanesoft\Seo\Seeds
12
 * @author   ARCANEDEV <[email protected]>
13
 */
14
class RolesTableSeeder extends RolesSeeder
15
{
16
    /* -----------------------------------------------------------------
17
     |  Main Methods
18
     | -----------------------------------------------------------------
19
     */
20
21
    /**
22
     * Run the database seeds.
23
     */
24
    public function run()
25
    {
26
        $this->seed([
27
            [
28
                'name'        => 'SEO Moderators',
29
                'description' => 'The SEO moderators role.',
30
                'is_locked'   => true,
31
            ],
32
        ]);
33
34
        $this->syncAdminRole();
35
        $this->syncRoles([
36
            'seo-moderators' => 'seo.',
37
        ]);
38
    }
39
40
    /* -----------------------------------------------------------------
41
     |  Other Methods
42
     | -----------------------------------------------------------------
43
     */
44
45
    /**
46
     * Sync the roles.
47
     * @todo: Refactor this method
48
     *
49
     * @param  array  $roles
50
     */
51
    protected function syncRoles(array $roles)
52
    {
53
        $permissions = Permission::all();
54
55
        foreach ($roles as $roleSlug => $permissionSlug) {
56
            $ids  = $permissions->filter(function (Permission $permission) use ($permissionSlug) {
57
                return Str::startsWith($permission->slug, $permissionSlug);
58
            })->pluck('id');
59
60
            /** @var  \Arcanesoft\Auth\Models\Role  $role */
61
            if ($role = Role::where('slug', $roleSlug)->first()) {
62
                $role->permissions()->sync($ids);
63
            }
64
        }
65
    }
66
}
67