Passed
Push — developer ( 9daaa0...7262d5 )
by Mariusz
107:10 queued 72:05
created

Module::isPermitted()   A

Complexity

Conditions 5
Paths 8

Size

Total Lines 15

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 15
rs 9.4555
c 0
b 0
f 0
cc 5
nc 8
nop 2
1
<?php
2
/**
3
 * Basic module model class.
4
 *
5
 * @package Model
6
 *
7
 * @copyright YetiForce Sp. z o.o.
8
 * @license   YetiForce Public License 3.0 (licenses/LicenseEN.txt or yetiforce.com)
9
 * @author    Mariusz Krzaczkowski <[email protected]>
10
 * @author    Radosław Skrzypczak <[email protected]>
11
 */
12
13
namespace YF\Modules\Base\Model;
14
15
use App;
16
use App\Api;
17
18
class Module
19
{
20
	/** @var YF\Modules\Base\Model\Module[] Module model cache. */
21
	protected static $cache;
22
23
	/** @var string Module name. */
24
	protected $moduleName;
25
26
	/** @var array Fields. */
27
	protected $fields;
28
29
	/** @var array Fields models. */
30
	protected $fieldsModels;
31
32
	protected $defaultView = 'ListView';
33
34
	/**
35
	 * Get module model instance.
36
	 *
37
	 * @param string $moduleName
38
	 *
39
	 * @return self
0 ignored issues
show
Documentation introduced by
Should the return type not be \self?

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

Loading history...
40
	 */
41
	public static function getInstance(string $moduleName): self
42
	{
43
		if (isset(self::$cache[$moduleName])) {
44
			return self::$cache[$moduleName];
45
		}
46
		$handlerModule = App\Loader::getModuleClassName($moduleName, 'Model', 'Module');
47
		return self::$cache[$moduleName] = new $handlerModule($moduleName);
48
	}
49
50
	/**
51
	 * Constructor  function.
52
	 *
53
	 * @param string $moduleName
54
	 */
55
	public function __construct(string $moduleName)
56
	{
57
		$this->moduleName = $moduleName;
58
	}
59
60
	/**
61
	 * Function to check permission for a Module/Action.
62
	 *
63
	 * @param string $module
64
	 * @param string $action
65
	 *
66
	 * @return bool
67
	 */
68
	public static function isPermitted(string $module, string $action)
69
	{
70
		if (!\App\Session::has('modulePermissions')) {
71
			\App\Session::set('modulePermissions', []);
72
		}
73
		$data = \App\Session::get('modulePermissions');
74
		if (!isset($data[$module])) {
75
			$data[$module] = Api::getInstance()->call($module . '/Privileges');
76
			\App\Session::set('modulePermissions', $data);
77
		}
78
		if (isset($data[$module][$action]) && !empty($data[$module][$action])) {
0 ignored issues
show
Unused Code introduced by
This if statement, and the following return statement can be replaced with return isset($data[$modu...ata[$module][$action]);.
Loading history...
79
			return true;
80
		}
81
		return false;
82
	}
83
84
	/**
85
	 * Get all fields.
86
	 *
87
	 * @return array
88
	 */
89
	public function getFields(): array
90
	{
91
		if (isset($this->fields)) {
92
			return $this->fields;
93
		}
94
		$data = $this->getFieldsFromApi();
95
		$fields = [];
96
		foreach ($data['fields'] as $field) {
97
			$fields[$field['name']] = $field;
98
		}
99
		return $this->fields = $fields;
100
	}
101
102
	/**
103
	 * Get all fields models.
104
	 *
105
	 * @return \YF\Modules\Base\FieldTypes\BaseField[]
106
	 */
107
	public function getFieldsModels(): array
108
	{
109
		$fields = [];
110
		foreach (array_keys($this->getFields()) as $fieldName) {
111
			$fields[$fieldName] = $this->getFieldModel($fieldName);
112
		}
113
		return $fields;
114
	}
115
116
	/**
117
	 * Get fields and blocks.
118
	 *
119
	 * @return array
120
	 */
121
	public function getFieldsFromApi(): array
122
	{
123
		if (\App\Cache::has('moduleFields', $this->moduleName)) {
124
			$data = \App\Cache::get('moduleFields', $this->moduleName);
125
		} else {
126
			$data = Api::getInstance()->call($this->moduleName . '/Fields');
127
			\App\Cache::save('moduleFields', $this->moduleName, $data);
128
		}
129
		return $data;
130
	}
131
132
	/**
133
	 * Get field by name.
134
	 *
135
	 * @param string $name
136
	 *
137
	 * @return array
138
	 */
139
	public function getField(string $name): array
140
	{
141
		if (empty($this->fields)) {
142
			$this->getFields();
143
		}
144
		if (empty($this->fields[$name])) {
145
			throw new \App\Exceptions\AppException("Field not found: {$name}");
146
		}
147
		return $this->fields[$name];
148
	}
149
150
	/**
151
	 * Get field model by name.
152
	 *
153
	 * @param string $name
154
	 *
155
	 * @return \YF\Modules\Base\FieldTypes\BaseField
156
	 */
157
	public function getFieldModel(string $name): \YF\Modules\Base\FieldTypes\BaseField
158
	{
159
		if (!isset($this->fieldsModels[$name])) {
160
			$this->fieldsModels[$name] = Field::getInstance($this->moduleName, $this->getField($name));
161
		}
162
		return $this->fieldsModels[$name];
163
	}
164
165
	/**
166
	 * Returns default view for module.
167
	 *
168
	 * @return string
169
	 */
170
	public function getDefaultView(): string
171
	{
172
		return $this->defaultView;
173
	}
174
175
	/**
176
	 * Returns default address url.
177
	 *
178
	 * @return string
179
	 */
180
	public function getDefaultUrl(): string
181
	{
182
		return "index.php?module={$this->moduleName}&view={$this->getDefaultView()}";
183
	}
184
}
185