Permission::createResource()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 48

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 0
Metric Value
dl 0
loc 48
ccs 0
cts 27
cp 0
rs 9.1344
c 0
b 0
f 0
cc 3
nc 3
nop 2
crap 12
1
<?php
2
3
namespace Yajra\Acl\Models;
4
5
use Exception;
6
use Illuminate\Database\Eloquent\Collection;
7
use Illuminate\Database\Eloquent\Model;
8
use Illuminate\Database\Eloquent\Relations\BelongsToMany;
9
use Illuminate\Support\Str;
10
use Yajra\Acl\Traits\InteractsWithRole;
11
use Yajra\Acl\Traits\RefreshCache;
12
13
/**
14
 * @property string resource
15
 * @property string name
16
 * @property string slug
17
 * @property bool system
18
 */
19
class Permission extends Model
20
{
21
    use InteractsWithRole, RefreshCache;
22
23
    /** @var string */
24
    protected $table = 'permissions';
25
26
    /** @var array */
27
    protected $fillable = ['name', 'slug', 'resource', 'system'];
28
29
    /** @var array */
30
    protected $casts = ['system' => 'bool'];
31
32
    /**
33
     * Find a permission by slug.
34
     *
35
     * @param  string  $slug
36
     * @return \Illuminate\Database\Eloquent\Builder|\Illuminate\Database\Eloquent\Model
37
     */
38
    public static function findBySlug(string $slug)
39
    {
40
        return static::query()->where('slug', $slug)->firstOrFail();
41
    }
42
43
    /**
44
     * Create a permissions for a resource.
45
     *
46
     * @param  string  $resource
47
     * @param  bool  $system
48
     * @return \Illuminate\Support\Collection
49
     */
50
    public static function createResource(string $resource, $system = false)
51
    {
52
        $group = Str::title($resource);
53
        $slug = Str::slug($group);
54
        $permissions = [
55
            [
56
                'slug' => 'viewAny-'.$slug,
57
                'resource' => $group,
58
                'name' => 'View Any '.$group,
59
                'system' => $system,
60
            ],
61
            [
62
                'slug' => 'view-'.$slug,
63
                'resource' => $group,
64
                'name' => 'View '.$group,
65
                'system' => $system,
66
            ],
67
            [
68
                'slug' => 'create-'.$slug,
69
                'resource' => $group,
70
                'name' => 'Create '.$group,
71
                'system' => $system,
72
            ],
73
            [
74
                'slug' => 'update-'.$slug,
75
                'resource' => $group,
76
                'name' => 'Update '.$group,
77
                'system' => $system,
78
            ],
79
            [
80
                'slug' => 'delete-'.$slug,
81
                'resource' => $group,
82
                'name' => 'Delete '.$group,
83
                'system' => $system,
84
            ],
85
        ];
86
87
        $collection = new Collection;
88
        foreach ($permissions as $permission) {
89
            try {
90
                $collection->push(static::create($permission));
0 ignored issues
show
Bug introduced by
The method create() does not exist on Yajra\Acl\Models\Permission. Did you maybe mean createResource()?

This check marks calls to methods that do not seem to exist on an object.

This is most likely the result of a method being renamed without all references to it being renamed likewise.

Loading history...
91
            } catch (Exception $e) {
92
                // permission already exists.
93
            }
94
        }
95
96
        return $collection;
97
    }
98
99
    /**
100
     * Permission can belong to many users.
101
     *
102
     * @return \Illuminate\Database\Eloquent\Relations\BelongsToMany
103
     */
104
    public function users(): BelongsToMany
105
    {
106
        return $this->belongsToMany(config('acl.user', config('auth.providers.users.model')))
107
            ->withTimestamps();
108
    }
109
}
110