Completed
Push — master ( d7b6b6...22c11c )
by Chris
01:50 queued 01:43
created

Permission::findOrCreate()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 12
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 6
nc 2
nop 2
dl 0
loc 12
rs 9.4285
c 0
b 0
f 0
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 Illuminate\Database\Eloquent\Relations\MorphToMany;
10
use Spatie\Permission\Exceptions\PermissionDoesNotExist;
11
use Illuminate\Database\Eloquent\Relations\BelongsToMany;
12
use Spatie\Permission\Exceptions\PermissionAlreadyExists;
13
use Spatie\Permission\Contracts\Permission as PermissionContract;
14
15
class Permission extends Model implements PermissionContract
16
{
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.permissions'));
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::getPermissions()->where('name', $attributes['name'])->where('guard_name', $attributes['guard_name'])->first()) {
35
            throw PermissionAlreadyExists::create($attributes['name'], $attributes['guard_name']);
36
        }
37
38
        if (isNotLumen() && app()::VERSION < '5.4') {
39
            return parent::create($attributes);
40
        }
41
42
        return static::query()->create($attributes);
43
    }
44
45
    /**
46
     * A permission can be applied to roles.
47
     */
48
    public function roles(): BelongsToMany
49
    {
50
        return $this->belongsToMany(
51
            config('permission.models.role'),
52
            config('permission.table_names.role_has_permissions')
53
        );
54
    }
55
56
    /**
57
     * A permission belongs to some users of the model associated with its guard.
58
     */
59 View Code Duplication
    public function users(): MorphToMany
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...
60
    {
61
        $permissionsForeignKeyName = str_singular(config('permission.table_names.permissions')).'_id';
62
63
        return $this->morphedByMany(
64
            getModelForGuard($this->attributes['guard_name']),
65
            'model',
66
            config('permission.table_names.model_has_permissions'),
67
            $permissionsForeignKeyName,
68
            'model_id'
69
        );
70
    }
71
72
    /**
73
     * Find a permission by its name (and optionally guardName).
74
     *
75
     * @param string $name
76
     * @param string|null $guardName
77
     *
78
     * @throws \Spatie\Permission\Exceptions\PermissionDoesNotExist
79
     *
80
     * @return \Spatie\Permission\Contracts\Permission
81
     */
82
    public static function findByName(string $name, $guardName = null): PermissionContract
83
    {
84
        $guardName = $guardName ?? config('auth.defaults.guard');
85
86
        $permission = static::getPermissions()->where('name', $name)->where('guard_name', $guardName)->first();
87
88
        if (! $permission) {
89
            throw PermissionDoesNotExist::create($name, $guardName);
90
        }
91
92
        return $permission;
93
    }
94
95
    /**
96
     * Find or create permission by its name (and optionally guardName).
97
     *
98
     * @param string $name
99
     * @param string|null $guardName
100
     *
101
     * @return \Spatie\Permission\Contracts\Permission
102
     */
103
    public static function findOrCreate(string $name, $guardName = null): PermissionContract
104
    {
105
        $guardName = $guardName ?? config('auth.defaults.guard');
106
107
        $permission = static::getPermissions()->where('name', $name)->where('guard_name', $guardName)->first();
108
109
        if (! $permission) {
110
            return static::create(['name' => $name, 'guard_name' => $guardName]);
111
        }
112
113
        return $permission;
114
    }
115
116
    /**
117
     * Get the current cached permissions.
118
     */
119
    protected static function getPermissions(): Collection
120
    {
121
        return app(PermissionRegistrar::class)->getPermissions();
122
    }
123
}
124