Completed
Pull Request — master (#245)
by Sebastian
05:44 queued 03:45
created

Role   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 74
Duplicated Lines 13.51 %

Coupling/Cohesion

Components 0
Dependencies 5

Importance

Changes 1
Bugs 0 Features 0
Metric Value
dl 10
loc 74
c 1
b 0
f 0
wmc 8
lcom 0
cbo 5
rs 10

4 Methods

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