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

Permission::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

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