Passed
Push — master ( f51c1b...16736d )
by Arthur
05:36
created

AuthorizationSeeder::createRoles()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 4
nc 2
nop 0
dl 0
loc 8
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Modules\Authorization\Database\Seeders;
4
5
use Illuminate\Database\Seeder;
6
use Modules\Authorization\Attributes\PermissionAttributes;
7
use Modules\Authorization\Attributes\RolePermissions;
8
use Modules\Authorization\Contracts\AuthorizationContract;
9
use Modules\Authorization\Entities\Permission;
10
use Modules\Authorization\Entities\Role;
11
use Modules\Authorization\Services\AuthorizationService;
12
13
class AuthorizationSeeder extends Seeder
14
{
15
    public $priority = 0;
16
17
    /**
18
     * @var AuthorizationService
19
     */
20
    protected $service;
21
22
    /**
23
     * AuthorizationSeeder constructor.
24
     *
25
     * @param $service
26
     */
27
    public function __construct(AuthorizationContract $service)
28
    {
29
        $this->service = $service;
0 ignored issues
show
Documentation Bug introduced by
$service is of type Modules\Authorization\Co...s\AuthorizationContract, but the property $service was declared to be of type Modules\Authorization\Se...es\AuthorizationService. Are you sure that you always receive this specific sub-class here, or does it make sense to add an instanceof check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a given class or a super-class is assigned to a property that is type hinted more strictly.

Either this assignment is in error or an instanceof check should be added for that assignment.

class Alien {}

class Dalek extends Alien {}

class Plot
{
    /** @var  Dalek */
    public $villain;
}

$alien = new Alien();
$plot = new Plot();
if ($alien instanceof Dalek) {
    $plot->villain = $alien;
}
Loading history...
30
    }
31
32
    /**
33
     * Run the database seeds.
34
     *
35
     * @return void
36
     */
37
    public function run()
38
    {
39
        $this->service->clearPermissionCache();
40
        $this->createPermissions();
41
        $this->createRoles();
42
    }
43
44
45
    protected function createPermissions(): void
46
    {
47
        $permissions = get_class_constants(PermissionAttributes::class);
48
        $this->service->createPermissions(collect($permissions)->flatten()->toArray());
49
    }
50
51
    protected function createRoles(): void
52
    {
53
        $roles = get_class_constants(RolePermissions::class);
54
        foreach ($roles as $role => $permissions) {
55
            $this->service->createRole(strtolower($role), $permissions);
56
        }
57
58
        $this->createAdminRole();
59
    }
60
61
    protected function createAdminRole()
62
    {
63
        /* Create the admin role with all possible permissions */
64
        $permissions = get_class_constants(PermissionAttributes::class);
65
        $this->service->createRole(Role::ADMIN, collect($permissions)->flatten()->toArray());
66
    }
67
}
68