Passed
Push — main ( 25dcc1...8769af )
by Jean-Christophe
02:22
created

AclControllerParser::addAllows()   C

Complexity

Conditions 14
Paths 7

Size

Total Lines 19
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 10
CRAP Score 18.5707

Importance

Changes 3
Bugs 0 Features 0
Metric Value
cc 14
eloc 14
c 3
b 0
f 0
nc 7
nop 5
dl 0
loc 19
ccs 10
cts 14
cp 0.7143
crap 18.5707
rs 6.2666

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
namespace Ubiquity\security\acl\cache;
3
4
use Ubiquity\controllers\Controller;
5
use Ubiquity\orm\parser\Reflexion;
6
use Ubiquity\security\acl\AclManager;
7
use Ubiquity\cache\ClassUtils;
8
use Ubiquity\exceptions\AclException;
9
10
/**
11
 * Ubiquity\security\acl\cache$AclControllerParser
12
 * This class is part of Ubiquity
13
 *
14
 * @author jc
15
 * @version 1.0.0
16
 *
17
 */
18
class AclControllerParser {
19
20
	protected $controllerClass;
21
22
	protected $mainResource;
23
24
	protected $mainPermission;
25
26
	protected $permissionMap;
27
28 2
	public function __construct() {
29 2
		$this->permissionMap = new PermissionsMap();
30 2
	}
31
32 2
	public function init() {
33 2
		$this->permissionMap->init();
34 2
	}
35
36 2
	public function parse($controllerClass) {
37 2
		$this->controllerClass = $controllerClass;
38 2
		$reflect = new \ReflectionClass($controllerClass);
39 2
		if (! $reflect->isAbstract() && $reflect->isSubclassOf(Controller::class)) {
40
			try {
41 2
				$annotsResource = Reflexion::getAnnotationClass($controllerClass, '@resource');
42 2
				$annotsPermission = Reflexion::getAnnotationClass($controllerClass, '@permission');
43 2
				$annotAllows = Reflexion::getAnnotationClass($controllerClass, '@allow');
44
			} catch (\Exception $e) {
45
				// When controllerClass generates an exception
46
			}
47 2
			$this->mainResource = $annotsResource[0] ?? null;
48 2
			$this->mainPermission = $annotsPermission[0] ?? null;
49 2
			if (\is_array($annotAllows) && \count($annotAllows) > 0) {
50
				$resource = $this->mainResource ? $this->mainResource->name : $reflect->getShortName();
51
				$permission = $this->mainPermission ? $this->mainPermission->name : 'ALL';
52
				$this->addAllows($annotAllows, $controllerClass, null, $resource, $permission);
53
				$this->permissionMap->addAction($controllerClass, '*', $resource, $permission);
54
			}
55 2
			$methods = Reflexion::getMethods($controllerClass, \ReflectionMethod::IS_PUBLIC);
56 2
			$this->parseMethods($methods);
57
		}
58 2
	}
59
60 2
	protected function parseMethods($methods) {
61 2
		$hasPermission = false;
62 2
		$controllerClass = $this->controllerClass;
63 2
		$controller = ClassUtils::getClassSimpleName($controllerClass);
64 2
		foreach ($methods as $method) {
65 2
			$this->parseMethod($method, $hasPermission, $controller);
66
		}
67 2
		if ($hasPermission || $this->mainResource != null || $this->mainPermission != null) {
68 2
			$permission = 'ALL';
69 2
			$resource = $this->mainResource ? $this->mainResource->name : $controller;
70 2
			$this->permissionMap->addAction($controllerClass, '*', $resource, $this->mainPermission ? $this->mainPermission->name : 'ALL');
71 2
			AclManager::addResource($resource, $controller . '.*');
72 2
			if (isset($this->mainPermission)) {
73
				$permission = $this->mainPermission->name;
74
				AclManager::addPermission($this->mainPermission->name, ($this->mainPermission->level) ?? 0);
75
			}
76 2
			$annotsAllow = Reflexion::getAnnotationClass($controllerClass, '@allow');
77 2
			if (\is_array($annotsAllow) && \count($annotsAllow) > 0) {
78
				$this->addAllows($annotsAllow, $controller, '*', $resource, $permission);
79
			}
80
		}
81 2
	}
82
83 2
	protected function parseMethod(\ReflectionMethod $method, bool &$hasPermission, $controller) {
84 2
		$action = $method->name;
85 2
		$permission = NULL;
86 2
		$resource = NULL;
87 2
		$controllerClass = $this->controllerClass;
88 2
		if ($method->getDeclaringClass()->getName() === $controllerClass) {
89
			try {
90 2
				$annotResource = Reflexion::getAnnotationMethod($controllerClass, $action, '@resource');
91 2
				$annotPermission = Reflexion::getAnnotationMethod($controllerClass, $action, '@permission');
92 2
				if ($annotResource) {
93 2
					$resource = $annotResource->name;
94 2
					AclManager::addResource($annotResource->name, $controller . '.' . $action);
95
				}
96 2
				if ($annotPermission) {
97 2
					$permission = $annotPermission->name;
98 2
					AclManager::addPermission($annotPermission->name, $annotPermission->level ?? 0);
99 2
					$hasPermission = true;
100
				}
101 2
				$resource ??= $this->mainResource ? $this->mainResource->name : ($controller . '.' . $action);
102
103 2
				$annotsAllow = Reflexion::getAnnotationsMethod($controllerClass, $action, '@allow');
104 2
				if (\is_array($annotsAllow) && \count($annotsAllow) > 0) {
105 2
					$this->addAllows($annotsAllow, $controller, $action, $resource, $permission);
106 2
					$this->permissionMap->addAction($controllerClass, $action, $resource, $permission ?? 'ALL');
107 2
				} elseif ($permission !== null && $resource !== null) {
108 2
					$this->permissionMap->addAction($controllerClass, $action, $resource, $permission ?? 'ALL');
109
				}
110
			} catch (\Exception $e) {
111
				// Exception in controller code
112
			}
113
		}
114 2
	}
115
116 2
	protected function addAllows($annotsAllow, $controller, $action, &$resource, &$permission) {
117 2
		foreach ($annotsAllow as $annotAllow) {
118 2
			if (isset($annotAllow->resource) && isset($resource) && \strpos($resource, '.') === FALSE && $resource !== $annotAllow->resource && $permission != null) {
119
				throw new AclException("Resources {$resource} and {$annotAllow->resource} are in conflict for action {$controller}.{$action}");
120
			}
121 2
			if (isset($annotAllow->permission) && isset($permission) && $permission !== $annotAllow->permission) {
122
				throw new AclException("Permissions {$permission} and {$annotAllow->permission} are in conflict for action {$controller}.{$action}");
123
			}
124 2
			if ($permission === null && $annotAllow->permission === null) {
125 2
				$resource = $controller . '.' . $action;
126
			}
127 2
			$resource = $annotAllow->resource ?? $resource;
128 2
			$permission = $annotAllow->permission ?? $permission;
129 2
			if (\is_array($annotAllow->role)) {
130
				foreach ($annotAllow->role as $role) {
131
					AclManager::addAndAllow($role, $resource, $permission);
132
				}
133
			} else {
134 2
				AclManager::addAndAllow($annotAllow->role, $resource, $permission);
135
			}
136
		}
137 2
	}
138
139 2
	public function save() {
140 2
		$this->permissionMap->save();
141 2
		AclManager::saveAll();
142 2
	}
143
}
144
145