Completed
Push — master ( 013627...ed6b20 )
by Freek
02:39
created

HasPermissions   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 88
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 2
Bugs 0 Features 1
Metric Value
wmc 10
c 2
b 0
f 1
lcom 1
cbo 1
dl 0
loc 88
rs 10

5 Methods

Rating   Name   Duplication   Size   Complexity  
A revokePermissionTo() 0 8 1
A getStoredPermission() 0 12 3
A usingMultipleArguments() 0 4 1
A givePermissionTo() 0 16 3
A savePermissions() 0 10 2
1
<?php
2
3
namespace Spatie\Permission\Traits;
4
5
use Spatie\Permission\Contracts\Permission;
6
use Spatie\Permission\Exceptions\PermissionMustNotBeEmpty;
7
8
trait HasPermissions
9
{
10
    /**
11
     * Grant the given permission(s) to a role.
12
     *
13
     * @param string|array|Permission|\Illuminate\Support\Collection $permissions
14
     *
15
     * @return HasPermissions
0 ignored issues
show
Comprehensibility Bug introduced by
The return type HasPermissions is a trait, and thus cannot be used for type-hinting in PHP. Maybe consider adding an interface and use that for type-hinting?

In PHP traits cannot be used for type-hinting as they do not define a well-defined structure. This is because any class that uses a trait can rename that trait’s methods.

If you would like to return an object that has a guaranteed set of methods, you could create a companion interface that lists these methods explicitly.

Loading history...
16
     */
17
    public function givePermissionTo(...$permissions)
18
    {
19
        if(count($permissions) < 1) {
20
            throw new PermissionMustNotBeEmpty();
21
        }
22
23
        if (!$this->usingMultipleArguments($permissions)) {
24
            $permissions = current($permissions);
25
        }
26
27
        $this->savePermissions($permissions);
28
29
        $this->forgetCachedPermissions();
0 ignored issues
show
Bug introduced by
It seems like forgetCachedPermissions() 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...
30
31
        return $this;
32
    }
33
    
34
    /**
35
     * Save the given permissions to a role.
36
     *
37
     * @param array|Permission|\Illuminate\Support\Collection $permissions
38
     * 
39
     * @return array|\Illuminate\Database\Eloquent\Model
40
     */
41
    protected function savePermissions($permissions)
42
    {
43
        $permissions = $this->getStoredPermission($permissions);
44
45
        if($permissions instanceof \Illuminate\Database\Eloquent\Collection) {
46
            return $this->permissions()->saveMany($permissions);
0 ignored issues
show
Bug introduced by
The method permissions() does not exist on Spatie\Permission\Traits\HasPermissions. Did you maybe mean savePermissions()?

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...
47
        }
48
49
        return $this->permissions()->save($permissions);
0 ignored issues
show
Bug introduced by
The method permissions() does not exist on Spatie\Permission\Traits\HasPermissions. Did you maybe mean savePermissions()?

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...
50
    }
51
52
    /**
53
     * Revoke the given permission.
54
     *
55
     * @param $permission
56
     *
57
     * @return HasPermissions
0 ignored issues
show
Comprehensibility Bug introduced by
The return type HasPermissions is a trait, and thus cannot be used for type-hinting in PHP. Maybe consider adding an interface and use that for type-hinting?

In PHP traits cannot be used for type-hinting as they do not define a well-defined structure. This is because any class that uses a trait can rename that trait’s methods.

If you would like to return an object that has a guaranteed set of methods, you could create a companion interface that lists these methods explicitly.

Loading history...
58
     */
59
    public function revokePermissionTo($permission)
60
    {
61
        $this->permissions()->detach($this->getStoredPermission($permission));
0 ignored issues
show
Bug introduced by
The method permissions() does not exist on Spatie\Permission\Traits\HasPermissions. Did you maybe mean savePermissions()?

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...
62
63
        $this->forgetCachedPermissions();
0 ignored issues
show
Bug introduced by
It seems like forgetCachedPermissions() 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...
64
65
        return $this;
66
    }
67
68
    /**
69
     * @param string|array|Permission|\Illuminate\Support\Collection $permissions
70
     *
71
     * @return Permission
72
     */
73
    protected function getStoredPermission($permissions)
74
    {
75
        if (is_string($permissions)) {
76
            return app(Permission::class)->findByName($permissions);
77
        }
78
79
        if (is_array($permissions)) {
80
            return app(Permission::class)->whereIn('name', $permissions)->get();
81
        }
82
83
        return $permissions;
84
    }
85
86
    /**
87
     * @param $params
88
     * @return bool
89
     */
90
    private function usingMultipleArguments($params)
91
    {
92
        return count($params) > 1;
93
    }
94
95
}
96