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

Permission::__construct()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 10
Code Lines 5

Duplication

Lines 10
Ratio 100 %

Importance

Changes 0
Metric Value
dl 10
loc 10
c 0
b 0
f 0
rs 9.4285
cc 2
eloc 5
nc 2
nop 1
1
<?php
2
3
namespace Spatie\Permission\Models;
4
5
use Illuminate\Database\Eloquent\Model;
6
use Spatie\Permission\Traits\RefreshesPermissionCache;
7
use Spatie\Permission\Exceptions\PermissionDoesNotExist;
8
use Illuminate\Database\Eloquent\Relations\BelongsToMany;
9
use Spatie\Permission\Contracts\Permission as PermissionContract;
10
11
class Permission extends Model implements PermissionContract
12
{
13
    use RefreshesPermissionCache;
14
15
    /**
16
     * The attributes that aren't mass assignable.
17
     *
18
     * @var array
19
     */
20
    public $guarded = ['id'];
21
22 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...
23
    {
24
        if (empty($attributes['guard_name'])) {
25
            $attributes['guard_name'] = config('auth.defaults.guard');
26
        }
27
28
        parent::__construct($attributes);
29
30
        $this->setTable(config('laravel-permission.table_names.permissions'));
31
    }
32
33
    /**
34
     * A permission can be applied to roles.
35
     */
36
    public function roles(): BelongsToMany
37
    {
38
        return $this->belongsToMany(
39
            config('laravel-permission.models.role'),
40
            config('laravel-permission.table_names.role_has_permissions')
41
        );
42
    }
43
44
    /**
45
     * Find a permission by its name (and optionally guardName).
46
     *
47
     * @param string $name
48
     * @param string|null $guardName
49
     *
50
     * @throws \Spatie\Permission\Exceptions\PermissionDoesNotExist
51
     *
52
     * @return \Spatie\Permission\Contracts\Permission
53
     */
54
    public static function findByName(string $name, $guardName = null): PermissionContract
55
    {
56
        $guardName = $guardName ?? config('auth.defaults.guard');
57
58
        $permission = static::getPermissions()->where('name', $name)->where('guard_name', $guardName)->first();
59
60
        if (! $permission) {
61
            throw new PermissionDoesNotExist();
62
        }
63
64
        return $permission;
65
    }
66
}
67