Completed
Push — master ( 7e8e6f...a8e4dc )
by Freek
02:12
created

Role   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 83
Duplicated Lines 12.05 %

Coupling/Cohesion

Components 0
Dependencies 4

Importance

Changes 0
Metric Value
wmc 9
lcom 0
cbo 4
dl 10
loc 83
rs 10
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 8 1
A create() 10 10 2
A permissions() 0 7 1
A findByName() 0 12 2
A hasPermissionTo() 0 12 3

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
3
namespace Spatie\Permission\Models;
4
5
use Illuminate\Database\Eloquent\Model;
6
use Spatie\Permission\Traits\HasPermissions;
7
use Spatie\Permission\Exceptions\RoleDoesNotExist;
8
use Spatie\Permission\Exceptions\GuardDoesNotMatch;
9
use Spatie\Permission\Exceptions\RoleAlreadyExists;
10
use Spatie\Permission\Contracts\Role as RoleContract;
11
use Spatie\Permission\Traits\RefreshesPermissionCache;
12
use Illuminate\Database\Eloquent\Relations\BelongsToMany;
13
14
class Role extends Model implements RoleContract
15
{
16
    use HasPermissions;
17
    use RefreshesPermissionCache;
18
19
    public $guarded = ['id'];
20
21
    public function __construct(array $attributes = [])
22
    {
23
        $attributes['guard_name'] = $attributes['guard_name'] ?? config('auth.defaults.guard');
24
25
        parent::__construct($attributes);
26
27
        $this->setTable(config('permission.table_names.roles'));
28
    }
29
30 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...
31
    {
32
        $attributes['guard_name'] = $attributes['guard_name'] ?? config('auth.defaults.guard');
33
34
        if (static::where('name', $attributes['name'])->where('guard_name', $attributes['guard_name'])->first()) {
35
            throw RoleAlreadyExists::create($attributes['name'], $attributes['guard_name']);
36
        }
37
38
        return static::query()->create($attributes);
39
    }
40
41
    /**
42
     * A role may be given various permissions.
43
     */
44
    public function permissions(): BelongsToMany
45
    {
46
        return $this->belongsToMany(
47
            config('permission.models.permission'),
48
            config('permission.table_names.role_has_permissions')
49
        );
50
    }
51
52
    /**
53
     * Find a role by its name and guard name.
54
     *
55
     * @param string $name
56
     * @param string|null $guardName
57
     *
58
     * @return \Spatie\Permission\Contracts\Role|\Spatie\Permission\Models\Role
59
     *
60
     * @throws \Spatie\Permission\Exceptions\RoleDoesNotExist
61
     */
62
    public static function findByName(string $name, $guardName = null): RoleContract
63
    {
64
        $guardName = $guardName ?? config('auth.defaults.guard');
65
66
        $role = static::where('name', $name)->where('guard_name', $guardName)->first();
67
68
        if (! $role) {
69
            throw RoleDoesNotExist::create($name);
70
        }
71
72
        return $role;
73
    }
74
75
    /**
76
     * Determine if the user may perform the given permission.
77
     *
78
     * @param string|Permission $permission
79
     *
80
     * @return bool
81
     *
82
     * @throws \Spatie\Permission\Exceptions\GuardMismatch
83
     */
84
    public function hasPermissionTo($permission): bool
85
    {
86
        if (is_string($permission)) {
87
            $permission = app(Permission::class)->findByName($permission, $this->getGuardName());
88
        }
89
90
        if ($permission->guard_name !== $this->getGuardName()) {
91
            throw GuardDoesNotMatch::create($permission->guard_name, $this->getGuardName());
92
        }
93
94
        return $this->permissions->contains('id', $permission->id);
95
    }
96
}
97