Passed
Push — master ( f218e6...f51c1b )
by Arthur
04:59
created

AuthorizationSeeder::createRoles()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 10
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 5
nc 2
nop 0
dl 0
loc 10
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\Role;
10
use Modules\Authorization\Services\AuthorizationService;
11
12
class AuthorizationSeeder extends Seeder
13
{
14
    public $priority = 0;
15
16
    /**
17
     * @var AuthorizationService
18
     */
19
    protected $service;
20
21
    /**
22
     * AuthorizationSeeder constructor.
23
     *
24
     * @param $service
25
     */
26
    public function __construct(AuthorizationContract $service)
27
    {
28
        $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...
29
    }
30
31
    /**
32
     * Run the database seeds.
33
     *
34
     * @return void
35
     */
36
    public function run()
37
    {
38
        $this->service->clearPermissionCache();
39
        $this->createPermissions();
40
        $this->createRoles();
41
    }
42
43
44
    private function createPermissions(): void
45
    {
46
        $permissions = get_class_constants(PermissionAttributes::class);
47
        $this->service->createPermissions(collect($permissions)->flatten()->toArray());
48
    }
49
50
    private function createRoles(): void
51
    {
52
        $roles = get_class_constants(RolePermissions::class);
53
        foreach ($roles as $role => $permissions) {
54
            $this->service->createRole(strtolower($role), $permissions);
55
        }
56
57
        /* Create the admin role with all possible permissions */
58
        $permissions = get_class_constants(PermissionAttributes::class);
59
        $this->service->createRole(Role::ADMIN, collect($permissions)->flatten()->toArray());
60
    }
61
}
62