Permit   A
last analyzed

Complexity

Total Complexity 11

Size/Duplication

Total Lines 64
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Importance

Changes 12
Bugs 5 Features 0
Metric Value
wmc 11
c 12
b 5
f 0
lcom 1
cbo 0
dl 0
loc 64
rs 10

5 Methods

Rating   Name   Duplication   Size   Complexity  
A access() 0 4 1
A to() 0 4 1
A getRole() 0 12 3
A hasPermission() 0 12 4
A hasNeededRole() 0 4 2
1
<?php namespace Mascame\Artificer\Permit;
2
3
use Auth;
4
5
abstract class Permit extends Auth
6
{
7
8
    protected static $role = null;
9
10
    /**
11
     * @param $to
12
     */
13
    public static function access($to)
14
    {
15
        return false;
16
    }
17
18
    /**
19
     * @param $action
20
     */
21
    public static function to($action)
22
    {
23
        return false;
24
    }
25
26
    /**
27
     * @param string $role_column
28
     * @return null
29
     */
30
    public static function getRole($role_column = 'role')
31
    {
32
        if (static::$role) {
33
            return static::$role;
34
        }
35
36
        if (isset(Auth::user()->$role_column)) {
37
            return static::$role = Auth::user()->$role_column;
38
        }
39
40
        return null;
41
    }
42
43
    /**
44
     * @param null $permissions
45
     * @return bool
46
     */
47
    public static function hasPermission($permissions = null)
48
    {
49
        if (!$permissions) {
50
            return true;
51
        }
52
53
        if (is_array($permissions) && self::hasNeededRole($permissions)) {
54
            return true;
55
        }
56
57
        return false;
58
    }
59
60
    /**
61
     * @param $permissions
62
     * @return bool
63
     */
64
    private static function hasNeededRole($permissions)
65
    {
66
        return (in_array(self::getRole(), $permissions) || $permissions[0] == '*');
67
    }
68
}