Completed
Push — master ( 8cf912...d192c7 )
by Alexey
05:24
created

Access::checkAccess()   C

Complexity

Conditions 12
Paths 48

Size

Total Lines 26
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 12
eloc 16
c 2
b 0
f 0
nc 48
nop 2
dl 0
loc 26
rs 5.1612

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
2
3
/**
4
 * Access module
5
 *
6
 * @author Alexey Krupskiy <[email protected]>
7
 * @link http://inji.ru/
8
 * @copyright 2015 Alexey Krupskiy
9
 * @license https://github.com/injitools/cms-Inji/blob/master/LICENSE
10
 */
11
class Access extends Module
12
{
13
    public $accessGetters = [];
14
15
    public function init()
16
    {
17
        $this->accessCheckers = $this->getSnippets('accessGetter');
0 ignored issues
show
Bug introduced by
The property accessCheckers does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
18
    }
19
20
    public function getDeniedRedirect($app = false)
21
    {
22
        if (!$app) {
23
            $app = $this->app->type;
24
        }
25
        if (!empty($this->config['access']['accessTree'][$app]['deniedUrl']))
26
            return $this->config['access']['accessTree'][$app]['deniedUrl'];
27
28
        return '/';
29
    }
30
31
    public function checkAccess($element, $user = null)
32
    {
33
        $access = NULL;
34
        foreach ($this->accessCheckers as $getter) {
35
            foreach ($getter['classes'] as $className) {
36
                if ($element instanceof $className) {
37
                    $access = $getter['get']($element);
38
                    break;
39
                }
40
            }
41
        }
42
        if (is_null($access)) {
43
            $access = [];
44
        }
45
        if (is_null($user)) {
46
            $user = Users\User::$cur;
47
        }
48
        if (empty($access)) {
49
            return true;
50
        }
51
52
        if ((!$user->group_id && !empty($access)) || ($user->group_id && !empty($access) && !in_array($user->group_id, $access)))
53
            return false;
54
55
        return true;
56
    }
57
58
    public function resolvePath($array, $path, $element)
59
    {
60
        while ($path) {
61
            $result = $this->pathWalker($array, array_merge($path, [$element]));
62
            if ($result !== null) {
63
                return $result;
64
            }
65
            $path = array_slice($path, 0, -1);
66
        }
67
        return null;
68
    }
69
70
    public function pathWalker($array, $path)
71
    {
72
        if ($path && isset($array[$path[0]])) {
73
            return $this->pathWalker($array[$path[0]], array_slice($path, 1));
74
        } elseif (!$path) {
75
            return $array;
76
        } else {
77
            return NULL;
78
        }
79
    }
80
81
}
82