Completed
Pull Request — master (#677)
by Chris
01:39
created

Role::findById()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 12
Code Lines 6

Duplication

Lines 12
Ratio 100 %

Importance

Changes 0
Metric Value
dl 12
loc 12
c 0
b 0
f 0
cc 2
eloc 6
nc 2
nop 2
rs 9.4285
1
<?php
2
3
namespace Spatie\Permission\Models;
4
5
use Spatie\Permission\Guard;
6
use Illuminate\Database\Eloquent\Model;
7
use Spatie\Permission\Traits\HasPermissions;
8
use Spatie\Permission\Exceptions\RoleDoesNotExist;
9
use Spatie\Permission\Exceptions\GuardDoesNotMatch;
10
use Spatie\Permission\Exceptions\RoleAlreadyExists;
11
use Spatie\Permission\Contracts\Role as RoleContract;
12
use Spatie\Permission\Traits\RefreshesPermissionCache;
13
use Illuminate\Database\Eloquent\Relations\MorphToMany;
14
use Illuminate\Database\Eloquent\Relations\BelongsToMany;
15
16
class Role extends Model implements RoleContract
17
{
18
    use HasPermissions;
19
    use RefreshesPermissionCache;
20
21
    public $guarded = ['id'];
22
23 View Code Duplication
    public function __construct(array $attributes = [])
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
24
    {
25
        $attributes['guard_name'] = $attributes['guard_name'] ?? config('auth.defaults.guard');
26
27
        parent::__construct($attributes);
28
29
        $this->setTable(config('permission.table_names.roles'));
30
    }
31
32 View Code Duplication
    public static function create(array $attributes = [])
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
33
    {
34
        $attributes['guard_name'] = $attributes['guard_name'] ?? Guard::getDefaultName(static::class);
35
36
        if (static::where('name', $attributes['name'])->where('guard_name', $attributes['guard_name'])->first()) {
37
            throw RoleAlreadyExists::create($attributes['name'], $attributes['guard_name']);
38
        }
39
40
        if (isNotLumen() && app()::VERSION < '5.4') {
41
            return parent::create($attributes);
42
        }
43
44
        return static::query()->create($attributes);
45
    }
46
47
    /**
48
     * A role may be given various permissions.
49
     */
50
    public function permissions(): BelongsToMany
51
    {
52
        return $this->belongsToMany(
53
            config('permission.models.permission'),
54
            config('permission.table_names.role_has_permissions')
55
        );
56
    }
57
58
    /**
59
     * A role belongs to some users of the model associated with its guard.
60
     */
61
    public function users(): MorphToMany
62
    {
63
        return $this->morphedByMany(
64
            getModelForGuard($this->attributes['guard_name']),
65
            'model',
66
            config('permission.table_names.model_has_roles'),
67
            'role_id',
68
            'model_id'
69
        );
70
    }
71
72
    /**
73
     * Find a role by its name and guard name.
74
     *
75
     * @param string $name
76
     * @param string|null $guardName
77
     *
78
     * @return \Spatie\Permission\Contracts\Role|\Spatie\Permission\Models\Role
79
     *
80
     * @throws \Spatie\Permission\Exceptions\RoleDoesNotExist
81
     */
82 View Code Duplication
    public static function findByName(string $name, $guardName = null): RoleContract
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
83
    {
84
        $guardName = $guardName ?? Guard::getDefaultName(static::class);
85
86
        $role = static::where('name', $name)->where('guard_name', $guardName)->first();
87
88
        if (! $role) {
89
            throw RoleDoesNotExist::named($name);
90
        }
91
92
        return $role;
93
    }
94
95 View Code Duplication
    public static function findById(int $id, $guardName = null): RoleContract
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
96
    {
97
        $guardName = $guardName ?? Guard::getDefaultName(static::class);
98
99
        $role = static::where('id', $id)->where('guard_name', $guardName)->first();
100
101
        if (! $role) {
102
            throw RoleDoesNotExist::withId($id);
103
        }
104
105
        return $role;
106
    }
107
108
    /**
109
     * Find or create role by its name (and optionally guardName).
110
     *
111
     * @param string $name
112
     * @param string|null $guardName
113
     *
114
     * @return \Spatie\Permission\Contracts\Role
115
     */
116 View Code Duplication
    public static function findOrCreate(string $name, $guardName = null): RoleContract
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
117
    {
118
        $guardName = $guardName ?? Guard::getDefaultName(static::class);
119
120
        $role = static::where('name', $name)->where('guard_name', $guardName)->first();
121
122
        if (! $role) {
123
            return static::create(['name' => $name, 'guard_name' => $guardName]);
124
        }
125
126
        return $role;
127
    }
128
129
    /**
130
     * Determine if the user may perform the given permission.
131
     *
132
     * @param string|Permission $permission
133
     *
134
     * @return bool
135
     *
136
     * @throws \Spatie\Permission\Exceptions\GuardDoesNotMatch
137
     */
138
    public function hasPermissionTo($permission): bool
139
    {
140
        if (is_string($permission)) {
141
            $permission = app(Permission::class)->findByName($permission, $this->getDefaultGuardName());
142
        }
143
144
        if (is_int($permission)) {
145
            $permission = app(Permission::class)->findById($permission, $this->getDefaultGuardName());
146
        }
147
148
        if (! $this->getGuardNames()->contains($permission->guard_name)) {
149
            throw GuardDoesNotMatch::create($permission->guard_name, $this->getGuardNames());
150
        }
151
152
        return $this->permissions->contains('id', $permission->id);
153
    }
154
}
155