Guardian   A
last analyzed

Complexity

Total Complexity 16

Size/Duplication

Total Lines 138
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 0
Metric Value
wmc 16
lcom 1
cbo 1
dl 0
loc 138
rs 10
c 0
b 0
f 0

8 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A hasRole() 0 6 2
A hasAnyRole() 0 6 2
A hasAllRoles() 0 6 2
A hasCapability() 0 7 2
A hasAnyCapability() 0 6 2
A hasAllCapabilities() 0 6 2
A init() 0 20 3
1
<?php namespace Usman\Guardian\AccessControl;
2
3
use Illuminate\Auth\AuthManager;
4
5
class Guardian {
6
7
    /**
8
     * The current logged in user
9
     * 
10
     * @var User
11
     */
12
    protected $user;
13
14
    /**
15
     * The auth driver manager instance
16
     * 
17
     * @var Illuminate\Auth\AuthManager
18
     */
19
    protected $auth;
20
21
    /**
22
     * @var boolean
23
     */
24
    protected $loggedIn;
25
26
    /**
27
     * Creates an instance of Guardian
28
     * 
29
     * @param Illuminate\Auth\AuthManager $auth
30
     */
31
    public function __construct(AuthManager $auth)
32
    {
33
        $this->auth = $auth;
0 ignored issues
show
Documentation Bug introduced by
It seems like $auth of type object<Illuminate\Auth\AuthManager> is incompatible with the declared type object<Usman\Guardian\Ac...inate\Auth\AuthManager> of property $auth.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
34
        $this->init();
35
    }
36
37
    /**
38
     * Checks for a role
39
     * 
40
     * @param  string  $roleName
0 ignored issues
show
Bug introduced by
There is no parameter named $roleName. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
41
     * @return boolean
42
     */
43
    public function hasRole($role)
44
    {
45
        if( ! $this->loggedIn) return false;
46
        
47
        return $this->user->hasRole($role);
48
    }
49
50
    /**
51
     * Checks for any role
52
     * 
53
     * @param  array   $roleNames
54
     * @return boolean
55
     */
56
    public function hasAnyRole(array $roleNames)
57
    {
58
        if( ! $this->loggedIn) return false;
59
60
        return $this->user->hasAnyRole($roleNames);
61
    }
62
63
    /**
64
     * Checks for all roles
65
     * 
66
     * @param  array   $roleNames
67
     * @return boolean
68
     */
69
    public function hasAllRoles(array $roleNames)
70
    {
71
        if ( ! $this->loggedIn) return false;
72
73
        return $this->user->hasAllRoles($roleNames);
74
    }
75
76
    /**
77
     * Checks if a user has a capability
78
     * 
79
     * @param  string  $capabilityName
0 ignored issues
show
Documentation introduced by
There is no parameter named $capabilityName. Did you maybe mean $capability?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function. It has, however, found a similar but not annotated parameter which might be a good fit.

Consider the following example. The parameter $ireland is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $ireland
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was changed, but the annotation was not.

Loading history...
80
     * @return boolean
81
     */
82
    public function hasCapability($capability)
83
    {
84
        if( ! $this->loggedIn) return false;
85
86
        return $this->user->hasCapability($capability);
87
88
    }
89
90
    /**
91
     * Checks if a user has any capability
92
     * 
93
     * @param  array   $capabilityNames
94
     * @return boolean
95
     */
96
    public function hasAnyCapability(array $capabilityNames)
97
    {
98
        if( ! $this->loggedIn) return false;
99
100
        return $this->user->hasAnyCapability($capabilityNames);
101
    }
102
103
    /**
104
     * Checks if a user has all the capabilities
105
     * 
106
     * @param  array   $capabilityNames
107
     * @return boolean
108
     */
109
    public function hasAllCapabilities(array $capabilityNames)
110
    {
111
        if( ! $this->loggedIn) return false;
112
113
        return $this->user->hasAllCapabilities($capabilityNames);
114
    }
115
116
    /**
117
     * Initializes the Guardian instance.
118
     * 
119
     * @return void
120
     */
121
    protected function init()
122
    {
123
        $user = $this->auth->user();
124
        
125
        if(is_null($user))
126
        {
127
            return $this->loggedIn = false;
128
        }
129
        else
130
        {
131
            if( ! ($user instanceof AccessControlInterface))
132
            {
133
                throw new InvalidUserInstanceException('User Model Should Implement Usman\Guardian\AccessControl\AccessControlInterface');
134
            }
135
            
136
            $this->user = $user;
0 ignored issues
show
Documentation Bug introduced by
It seems like $user of type object<Usman\Guardian\Ac...AccessControlInterface> is incompatible with the declared type object<Usman\Guardian\AccessControl\User> of property $user.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
137
            $this->loggedIn = true;
138
        }
139
140
    }
141
142
}