Completed
Push — master ( e710dc...abf8fe )
by Maxime
8s
created

PermissionUtil   A

Complexity

Total Complexity 18

Size/Duplication

Total Lines 55
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 3
Bugs 0 Features 1
Metric Value
wmc 18
c 3
b 0
f 1
lcom 1
cbo 1
dl 0
loc 55
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
B hasAccess() 0 20 6
B hasAccessArray() 0 16 11
1
<?php namespace Distilleries\PermissionUtil\Helpers;
2
3
use Distilleries\PermissionUtil\Contracts\PermissionUtilContract;
4
use Illuminate\Contracts\Auth\Guard;
5
6
class PermissionUtil implements PermissionUtilContract {
7
8
    protected $auth;
9
    protected $config;
10
11
    public function __construct(Guard $auth, array $config = []) {
12
        $this->auth   = $auth;
13
        $this->config = $config;
14
    }
15
16
    public function hasAccess($key)
17
    {
18
        if (empty($this->config['auth_restricted'])) {
19
            return true;
20
        }
21
22
        if ($this->auth->check()) {
23
24
            $user = $this->auth->user();
25
            $implement = class_implements($user, true);
26
27
            if (empty($implement) || empty($implement['Distilleries\PermissionUtil\Contracts\PermissionUtilContract'])) {
28
                return true;
29
            }
30
31
            return (!empty($user)) ? $user->hasAccess($key) : false;
0 ignored issues
show
Bug introduced by
The method hasAccess() does not seem to exist on object<Illuminate\Contracts\Auth\Authenticatable>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
32
        }
33
34
        return false;
35
    }
36
37
    /*
38
     * Checks each elements of the array if the access is granted
39
     * @param  Array $arrayKeys
40
     * @param  Boolean $isAndRelation: if true its a AND relation check, if false a OR relation
0 ignored issues
show
Documentation introduced by
There is no parameter named $isAndRelation:. Did you maybe mean $isAndRelation?

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...
41
     * @param  String $child: if set, the hasAccess method will be called using element[$child] instead of the array's element itself
0 ignored issues
show
Documentation introduced by
There is no parameter named $child:. Did you maybe mean $child?

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...
42
     * @return boolean
43
     */
44
    public function hasAccessArray($arrayKeys, $isAndRelation = false, $child = null) {
45
        $hasAccess = null;
46
        foreach ($arrayKeys as $key) {
47
            if ($hasAccess === null) {
48
                $hasAccess = $this->hasAccess(($child == null ? $key : $key[$child]));
49
            } else {
50
                if ($isAndRelation) {
51
                    $hasAccess = $hasAccess && $this->hasAccess(($child == null ? $key : $key[$child]));
52
                    if (!$hasAccess) break;
53
                } else {
54
                    $hasAccess = $hasAccess || $this->hasAccess(($child == null ? $key : $key[$child]));
55
                }
56
            }
57
        }
58
        return $hasAccess != null ?: false;
59
    }
60
}