Completed
Push — master ( e43e24...a5d827 )
by ARCANEDEV
05:31
created

RolesSeeder   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 58
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 6

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 6
c 1
b 0
f 0
lcom 0
cbo 6
dl 0
loc 58
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A seed() 0 8 1
A prepareRoles() 0 14 4
A syncAdminRole() 0 8 1
1
<?php namespace Arcanesoft\Auth\Seeds;
2
3
use Arcanesoft\Auth\Bases\Seeder;
4
use Arcanesoft\Auth\Models\Permission;
5
use Arcanesoft\Auth\Models\Role;
6
use Carbon\Carbon;
7
8
/**
9
 * Class     RolesSeeder
10
 *
11
 * @package  Arcanesoft\Auth\Seeds
12
 * @author   ARCANEDEV <[email protected]>
13
 */
14
abstract class RolesSeeder extends Seeder
15
{
16
    /* ------------------------------------------------------------------------------------------------
17
     |  Main Functions
18
     | ------------------------------------------------------------------------------------------------
19
     */
20
    /**
21
     * Seed roles.
22
     *
23
     * @param  array  $roles
24
     */
25
    public function seed(array $roles)
26
    {
27
        $roles = $this->prepareRoles($roles);
28
29
        Role::insert($roles);
0 ignored issues
show
Bug introduced by
The method insert() does not exist on Arcanesoft\Auth\Models\Role. Did you maybe mean insertAndSetId()?

This check marks calls to methods that do not seem to exist on an object.

This is most likely the result of a method being renamed without all references to it being renamed likewise.

Loading history...
30
31
        $this->syncAdminRole();
32
    }
33
34
    /* ------------------------------------------------------------------------------------------------
35
     |  Other Functions
36
     | ------------------------------------------------------------------------------------------------
37
     */
38
    /**
39
     * Prepare roles to seed.
40
     *
41
     * @param  array  $roles
42
     *
43
     * @return array
44
     */
45
    protected function prepareRoles(array $roles)
46
    {
47
        $now = Carbon::now();
48
49
        foreach ($roles as $key => $role) {
50
            $roles[$key]['slug']       = str_slug($role['name']);
51
            $roles[$key]['is_active']  = isset($role['is_active']) ? $role['is_active'] : true;
52
            $roles[$key]['is_locked']  = isset($role['is_locked']) ? $role['is_locked'] : true;
53
            $roles[$key]['created_at'] = $now;
54
            $roles[$key]['updated_at'] = $now;
55
        }
56
57
        return $roles;
58
    }
59
60
    /**
61
     * Sync the admin role with all permissions.
62
     */
63
    protected function syncAdminRole()
64
    {
65
        /** @var Role $admin */
66
        $admin = Role::where('slug', 'administrator')->first();
67
        $ids   = Permission::all()->lists('id')->toArray();
68
69
        $admin->permissions()->sync($ids);
70
    }
71
}
72