Completed
Push — master ( 1af3cf...ff825a )
by Arjay
27:57 queued 13:02
created

HasRoleAndPermission::permissions()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
1
<?php
2
3
namespace Yajra\Acl\Traits;
4
5
use Illuminate\Database\Eloquent\Relations\BelongsToMany;
6
use Yajra\Acl\Models\Permission;
7
8
/**
9
 * @mixin \Illuminate\Database\Eloquent\Model
10
 * @property Permission[]|\Illuminate\Database\Eloquent\Collection $permissions
11
 */
12
trait HasRoleAndPermission
13
{
14
    use HasRole {
15
        HasRole::getPermissions as getRolePermissions;
16
    }
17
18
    private $permissionClass;
19
20
    /**
21
     * Grant user permission by slug.
22
     *
23
     * @param  string|array  $slug
24
     */
25
    public function grantPermissionBySlug($slug)
26
    {
27
        $this->getPermissionClass()
28
            ->newQuery()
29
            ->whereIn('slug', (array) $slug)
30
            ->each(function ($permission) {
31
                $this->grantPermission($permission);
32
            });
33
34
        $this->load('permissions');
0 ignored issues
show
Documentation Bug introduced by
The method load does not exist on object<Yajra\Acl\Traits\HasRoleAndPermission>? Since you implemented __call, maybe consider adding a @method annotation.

If you implement __call and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.

This is often the case, when __call is implemented by a parent class and only the child class knows which methods exist:

class ParentClass {
    private $data = array();

    public function __call($method, array $args) {
        if (0 === strpos($method, 'get')) {
            return $this->data[strtolower(substr($method, 3))];
        }

        throw new \LogicException(sprintf('Unsupported method: %s', $method));
    }
}

/**
 * If this class knows which fields exist, you can specify the methods here:
 *
 * @method string getName()
 */
class SomeClass extends ParentClass { }
Loading history...
35
    }
36
37
    /**
38
     * Get Permission class.
39
     *
40
     * @return \Yajra\Acl\Models\Permission
41
     */
42 View Code Duplication
    public function getPermissionClass(): Permission
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...
43
    {
44
        if (!isset($this->permissionClass)) {
45
            $this->permissionClass = resolve(config('acl.permission'));
46
        }
47
48
        return $this->permissionClass;
49
    }
50
51
    /**
52
     * Grants the given permission to the user.
53
     *
54
     * @param  \Illuminate\Support\Collection|\Illuminate\Database\Eloquent\Model|array  $id
55
     * @param  array  $attributes
56
     * @param  bool  $touch
57
     * @return void
58
     */
59
    public function grantPermission($id, array $attributes = [], $touch = true)
60
    {
61
        $this->permissions()->attach($id, $attributes, $touch);
62
        $this->load('permissions');
0 ignored issues
show
Documentation Bug introduced by
The method load does not exist on object<Yajra\Acl\Traits\HasRoleAndPermission>? Since you implemented __call, maybe consider adding a @method annotation.

If you implement __call and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.

This is often the case, when __call is implemented by a parent class and only the child class knows which methods exist:

class ParentClass {
    private $data = array();

    public function __call($method, array $args) {
        if (0 === strpos($method, 'get')) {
            return $this->data[strtolower(substr($method, 3))];
        }

        throw new \LogicException(sprintf('Unsupported method: %s', $method));
    }
}

/**
 * If this class knows which fields exist, you can specify the methods here:
 *
 * @method string getName()
 */
class SomeClass extends ParentClass { }
Loading history...
63
    }
64
65
    /**
66
     * Get related permissions.
67
     *
68
     * @return \Illuminate\Database\Eloquent\Relations\BelongsToMany
69
     */
70
    public function permissions(): BelongsToMany
71
    {
72
        return $this->belongsToMany(config('acl.permission', Permission::class))->withTimestamps();
0 ignored issues
show
Documentation Bug introduced by
The method belongsToMany does not exist on object<Yajra\Acl\Traits\HasRoleAndPermission>? Since you implemented __call, maybe consider adding a @method annotation.

If you implement __call and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.

This is often the case, when __call is implemented by a parent class and only the child class knows which methods exist:

class ParentClass {
    private $data = array();

    public function __call($method, array $args) {
        if (0 === strpos($method, 'get')) {
            return $this->data[strtolower(substr($method, 3))];
        }

        throw new \LogicException(sprintf('Unsupported method: %s', $method));
    }
}

/**
 * If this class knows which fields exist, you can specify the methods here:
 *
 * @method string getName()
 */
class SomeClass extends ParentClass { }
Loading history...
73
    }
74
75
    /**
76
     * Grant user permissions by resource.
77
     *
78
     * @param  string|array  $resource
79
     */
80
    public function grantPermissionByResource($resource)
81
    {
82
        $this->getPermissionClass()
83
            ->newQuery()
84
            ->whereIn('resource', (array) $resource)
85
            ->each(function ($permission) {
86
                $this->grantPermission($permission);
87
            });
88
89
        $this->load('permissions');
0 ignored issues
show
Documentation Bug introduced by
The method load does not exist on object<Yajra\Acl\Traits\HasRoleAndPermission>? Since you implemented __call, maybe consider adding a @method annotation.

If you implement __call and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.

This is often the case, when __call is implemented by a parent class and only the child class knows which methods exist:

class ParentClass {
    private $data = array();

    public function __call($method, array $args) {
        if (0 === strpos($method, 'get')) {
            return $this->data[strtolower(substr($method, 3))];
        }

        throw new \LogicException(sprintf('Unsupported method: %s', $method));
    }
}

/**
 * If this class knows which fields exist, you can specify the methods here:
 *
 * @method string getName()
 */
class SomeClass extends ParentClass { }
Loading history...
90
    }
91
92
    /**
93
     * Revokes the given permission from the user.
94
     *
95
     * @param  mixed  $ids
96
     * @param  bool  $touch
97
     * @return int
98
     */
99
    public function revokePermission($ids = null, $touch = true): int
100
    {
101
        return $this->permissions()->detach($ids, $touch);
102
    }
103
104
    /**
105
     * Syncs the given permission(s) with the user.
106
     *
107
     * @param  \Illuminate\Support\Collection|\Illuminate\Database\Eloquent\Model|array  $ids
108
     * @param  bool  $detaching
109
     * @return array
110
     */
111
    public function syncPermissions($ids, $detaching = true): array
112
    {
113
        return $this->permissions()->sync($ids, $detaching);
114
    }
115
116
    /**
117
     * Revokes all permissions from the user.
118
     *
119
     * @return int
120
     */
121
    public function revokeAllPermissions(): int
122
    {
123
        return $this->permissions()->detach();
124
    }
125
126
    /**
127
     * Get all user permissions slug.
128
     *
129
     * @return array|null
130
     */
131
    public function getPermissions(): array
132
    {
133
        $rolePermissions = $this->getRolePermissions();
0 ignored issues
show
Bug introduced by
The method getRolePermissions() does not exist on Yajra\Acl\Traits\HasRoleAndPermission. Did you maybe mean permissions()?

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...
134
        $userPermissions = $this->permissions->pluck('slug')->toArray();
135
136
        return collect($userPermissions)->merge($rolePermissions)->unique()->toArray();
137
    }
138
}
139