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

PermissionUtil::hasAccessArray()   B

Complexity

Conditions 11
Paths 16

Size

Total Lines 16
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
c 2
b 0
f 0
dl 0
loc 16
rs 7.1162
cc 11
eloc 12
nc 16
nop 3

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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
}