RoleTrait::attachPermission()   A
last analyzed

Complexity

Conditions 3
Paths 4

Size

Total Lines 12
Code Lines 6

Duplication

Lines 12
Ratio 100 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 0
Metric Value
cc 3
eloc 6
nc 4
nop 1
dl 12
loc 12
ccs 0
cts 10
cp 0
crap 12
rs 9.4285
c 0
b 0
f 0
1
<?php
2
/*
3
 * This file is part of the Laravel Platfourm package.
4
 *
5
 * (c) Avtandil Kikabidze aka LONGMAN <[email protected]>
6
 *
7
 * For the full copyright and license information, please view the LICENSE
8
 * file that was distributed with this source code.
9
 */
10
11
namespace Longman\Platfourm\User\Models\Eloquent\Traits;
12
13
use Cache;
14
use Config;
15
16
trait RoleTrait
17
{
18
19 View Code Duplication
    public function cachedPermissions()
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...
20
    {
21
        $cacheKey = 'entrust_permissions_for_role_' . $this->getKey();
0 ignored issues
show
Bug introduced by
It seems like getKey() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
22
        return Cache::/*tags(Config::get('entrust.permission_role_table'))->*/
0 ignored issues
show
Unused Code Comprehensibility introduced by
70% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
23
        remember($cacheKey, Config::get('cache.ttl'), function () {
24
            return $this->perms()->get();
25
        });
26
    }
27
28 View Code Duplication
    public static function bootEntrustRoleTrait()
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...
29
    {
30
        static::saved(function ($item) {
31
            //Cache::tags(Config::get('entrust.permission_role_table'))->flush();
0 ignored issues
show
Unused Code Comprehensibility introduced by
69% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
32
            Cache::forget('entrust_permissions_for_role_' . $item->getKey());
33
        });
34
        static::deleted(function ($item) {
35
            //Cache::tags(Config::get('entrust.permission_role_table'))->flush();
0 ignored issues
show
Unused Code Comprehensibility introduced by
69% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
36
            Cache::forget('entrust_permissions_for_role_' . $item->getKey());
37
        });
38
        if (method_exists(static::class, 'restored')) {
39
            static::restored(function ($item) {
40
                //Cache::tags(Config::get('entrust.permission_role_table'))->flush();
0 ignored issues
show
Unused Code Comprehensibility introduced by
69% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
41
                Cache::forget('entrust_permissions_for_role_' . $item->getKey());
42
            });
43
        }
44
    }
45
46
    /**
47
     * Many-to-Many relations with the user model.
48
     *
49
     * @return \Illuminate\Database\Eloquent\Relations\BelongsToMany
50
     */
51
    public function users()
52
    {
53
        return $this->hasMany(Config::get('auth.model'));
0 ignored issues
show
Bug introduced by
It seems like hasMany() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
54
        // return $this->belongsToMany(Config::get('auth.model'), Config::get('entrust.role_user_table'));
0 ignored issues
show
Unused Code Comprehensibility introduced by
66% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
55
    }
56
57
    /**
58
     * Many-to-Many relations with the permission model.
59
     * Named "perms" for backwards compatibility. Also because "perms" is short and sweet.
60
     *
61
     * @return \Illuminate\Database\Eloquent\Relations\BelongsToMany
62
     */
63
    public function perms()
64
    {
65
        return $this->belongsToMany(Config::get('entrust.permission'), Config::get('entrust.permission_role_table'));
0 ignored issues
show
Bug introduced by
It seems like belongsToMany() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
66
    }
67
68
    /**
69
     * Boot the role model
70
     * Attach event listener to remove the many-to-many records when trying to delete
71
     * Will NOT delete any records if the role model uses soft deletes.
72
     *
73
     * @return void|bool
74
     */
75
    public static function boot()
76
    {
77
        parent::boot();
78
79
        static::deleting(function ($role) {
80
            if (!method_exists(Config::get('entrust.role'), 'bootSoftDeletes')) {
81
                $role->users()->sync([]);
82
                $role->perms()->sync([]);
83
            }
84
85
            return true;
86
        });
87
    }
88
89
    /**
90
     * Checks if the role has a permission by its name.
91
     *
92
     * @param  string|array $name       Permission name or array of permission names.
93
     * @param  bool         $requireAll All permissions in the array are required.
94
     * @return bool
95
     */
96 View Code Duplication
    public function hasPermission($name, $requireAll = false)
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...
97
    {
98
        if (is_array($name)) {
99
            foreach ($name as $permissionName) {
100
                $hasPermission = $this->hasPermission($permissionName);
101
102
                if ($hasPermission && !$requireAll) {
103
                    return true;
104
                } elseif (!$hasPermission && $requireAll) {
105
                    return false;
106
                }
107
            }
108
109
            // If we've made it this far and $requireAll is FALSE, then NONE of the permissions were found
110
            // If we've made it this far and $requireAll is TRUE, then ALL of the permissions were found.
111
            // Return the value of $requireAll;
112
            return $requireAll;
113
        } else {
114
            foreach ($this->cachedPermissions() as $permission) {
115
                if ($permission->name == $name) {
116
                    return true;
117
                }
118
            }
119
        }
120
121
        return false;
122
    }
123
124
    /**
125
     * Save the inputted permissions.
126
     *
127
     * @param  mixed $inputPermissions
128
     * @return void
129
     */
130
    public function savePermissions($inputPermissions)
131
    {
132
        if (!empty($inputPermissions)) {
133
            $this->perms()->sync($inputPermissions);
134
        } else {
135
            $this->perms()->detach();
136
        }
137
    }
138
139
    /**
140
     * Attach permission to current role.
141
     *
142
     * @param  object|array $permission
143
     * @return void
144
     */
145 View Code Duplication
    public function attachPermission($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...
146
    {
147
        if (is_object($permission)) {
148
            $permission = $permission->getKey();
149
        }
150
151
        if (is_array($permission)) {
152
            $permission = $permission['id'];
153
        }
154
155
        $this->perms()->attach($permission);
156
    }
157
158
    /**
159
     * Detach permission from current role.
160
     *
161
     * @param  object|array $permission
162
     * @return void
163
     */
164 View Code Duplication
    public function detachPermission($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...
165
    {
166
        if (is_object($permission)) {
167
            $permission = $permission->getKey();
168
        }
169
170
        if (is_array($permission)) {
171
            $permission = $permission['id'];
172
        }
173
174
        $this->perms()->detach($permission);
175
    }
176
177
    /**
178
     * Attach multiple permissions to current role.
179
     *
180
     * @param  mixed $permissions
181
     * @return void
182
     */
183
    public function attachPermissions($permissions)
184
    {
185
        foreach ($permissions as $permission) {
186
            $this->attachPermission($permission);
187
        }
188
    }
189
190
    /**
191
     * Detach multiple permissions from current role
192
     *
193
     * @param  mixed $permissions
194
     * @return void
195
     */
196
    public function detachPermissions($permissions)
197
    {
198
        foreach ($permissions as $permission) {
199
            $this->detachPermission($permission);
200
        }
201
    }
202
}
203