Passed
Push — feature/code_refactoring ( b8a37a...0d31b4 )
by Alessandro
02:17
created

searchForApplyViewRules()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 10
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 3

Importance

Changes 0
Metric Value
dl 0
loc 10
ccs 6
cts 6
cp 1
rs 9.4285
c 0
b 0
f 0
cc 3
eloc 5
nc 3
nop 2
crap 3
1
<?php
2
namespace UserPermissions\Controller\Component;
3
4
use Cake\Controller\Component;
5
use Cake\Controller\ComponentRegistry;
6
use Cake\Datasource\ConnectionManager;
7
use Cake\ORM\TableRegistry;
8
use Cake\Controller\Component\FlashComponent;
9
10
class UserPermissionsComponent extends Component {
11
12
/**
13
 * Controller name
14
 *
15
 * @var string
16
 */
17
	public $controller = null;
18
19
/**
20
 * Session 
21
 *
22
 * @var string
23
 */
24
	public $session = null;
25
26
/**
27
 * Components array
28
 *
29
 * @var array
30
 */
31
   	public $components = ['Flash'];
32
33
34
/**
35
* Initialization to get controller variable
36
*
37
* @param string $event The event to use.
0 ignored issues
show
Bug introduced by
There is no parameter named $event. 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...
38
*/
39 5
    public function initialize(array $config)
40
    {
41 5
        parent::initialize($config);
42
        
43 5
        $this->controller = $this->_registry->getController();
0 ignored issues
show
Documentation Bug introduced by
It seems like $this->_registry->getController() of type object<Cake\Controller\Controller> is incompatible with the declared type string of property $controller.

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...
44 5
        $this->session = $this->controller->request->session();
45
46 5
        $this->actions 		= array();
0 ignored issues
show
Bug introduced by
The property actions 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...
47 5
		$this->allow 		= true;
0 ignored issues
show
Bug introduced by
The property allow 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...
48 5
		$this->redirect 	= '';
0 ignored issues
show
Bug introduced by
The property redirect 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...
49 5
		$this->params 		= '';
0 ignored issues
show
Bug introduced by
The property params 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...
50 5
		$this->message 		= '';
0 ignored issues
show
Bug introduced by
The property message 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...
51 5
		$this->userType 	= '';
0 ignored issues
show
Bug introduced by
The property userType 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...
52 5
		$this->action   	= null;
0 ignored issues
show
Bug introduced by
The property action does not seem to exist. Did you mean actions?

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
53 5
    }
54
55
/**
56
* Initialization to get controller variable
57
*
58
* @param array $rules Array of rules for permissions.
59
* @return string '0' if user / group doesn't have permission, 1 if has permission
60
*/
61 5
    public function allow ($rules) {
62 5
    	$this->setUserValues();
63 5
    	$this->bindConfiguration($rules);
64
65 5
		if (!$this->applyGroupsRules($rules)) {
66 5
			$this->applyViewsRules($rules);
67
		}
68
69 5
		return $this->allow;
70
    }
71
72 5
    private function setUserValues()
73
    {
74 5
    	$userId = $this->session->read('Auth.User.id');
0 ignored issues
show
Bug introduced by
The method read cannot be called on $this->session (of type string).

Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.

Loading history...
75
76 5
    	if (!isset($userId)) {
77 5
			$this->userType = 'guest';
78
		}
79 5
    }
80
81 5
    private function bindConfiguration(array $rules) 
82
    {
83 5
    	foreach($rules as $key => $value){
84
			switch($key){
85 5
				case "user_type":
86 5
			        $this->userType = $value;
87 5
			        break;
88 5
			    case "redirect":
89 5
			        $this->redirect = $value;
90 5
			        break;
91 5
			    case "action":
92 5
			        $this->action = $value;
0 ignored issues
show
Bug introduced by
The property action does not seem to exist. Did you mean actions?

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
93 5
			        break;
94 5
			    case "controller":
95 5
			        $this->controller = $value;
96 5
			        break;
97 5
			    case "message":
98 5
			        $this->message = $value;
99 5
			        break;
100
			}
101
		}
102
103 5
		foreach($rules['groups']  as $key => $value){
104 5
			if($key == $this->userType){
105 5
				foreach($value as $v){
106 5
					array_push($this->actions, $v);
107
				}
108
			}
109
		}
110 5
    }
111
112 5
    private function applyGroupsRules(array $rules) : bool
113
    {
114 5
    	$existRulesForGroups = false;
115
116 5
    	if(isset($rules['groups'])){
117 5
			foreach($rules['groups'] as $key => $value){
118 5
				$this->searchForApplyGroupRules($key, $value);
119
			}
120
		}
121
122 5
		return $existRulesForGroups;
123
    }
124
125 5
    private function searchForApplyGroupRules($key, $value)
0 ignored issues
show
Unused Code introduced by
The parameter $value is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
126
    {
127 5
    	if($key == $this->userType){
128 5
    		if ($this->notInArrayAction()) {
129 2
				$existRulesForGroups = true;
0 ignored issues
show
Unused Code introduced by
$existRulesForGroups is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
130 2
				$this->redirectIfIsSet();
131
				
132 2
				$this->allow = false;
133
			}
134
		}
135 5
    }
136
137 5
    private function notInArrayAction()
138
    {
139 5
    	return ((!in_array('*', $this->actions)) && (!in_array($this->action, $this->actions)));
0 ignored issues
show
Bug introduced by
The property action does not seem to exist. Did you mean actions?

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
140
    }
141
142 5
    private function applyViewsRules(array $rules)
143
    {
144 5
    	if(isset($rules['views'])){
145 2
			foreach($rules['views'] as $key => $value){
146 2
				$this->searchForApplyViewRules($key, $value);
147
			}
148
		}
149 5
    }
150
151 2
    private function searchForApplyViewRules($key, $value)
152
    {
153 2
    	if($key == $this->action){
0 ignored issues
show
Bug introduced by
The property action does not seem to exist. Did you mean actions?

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
154 2
			if(!$this->controller->$value()){
0 ignored issues
show
Bug introduced by
The method $value cannot be called on $this->controller (of type string).

Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.

Loading history...
155 1
				$this->redirectIfIsSet();
156
				
157 1
				$this->allow = false;
158
			}
159
		}
160 2
    }
161
162 3
    private function redirectIfIsSet()
163
    {
164 3
    	if($this->redirect != ''){
165
			if($this->message != ''){
166
				$this->Flash->set($this->message);
0 ignored issues
show
Documentation introduced by
The property Flash does not exist on object<UserPermissions\C...erPermissionsComponent>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
167
			}
168
			
169
			header("Location: " . $this->redirect);
170
			exit;
171
		}
172
	}
173
}