Passed
Push — developer ( e5c82c...bcac4b )
by Mariusz
32:34
created

Module   A

Complexity

Total Complexity 20

Size/Duplication

Total Lines 153
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 6

Importance

Changes 0
Metric Value
wmc 20
lcom 2
cbo 6
dl 0
loc 153
rs 10
c 0
b 0
f 0

9 Methods

Rating   Name   Duplication   Size   Complexity  
A getInstance() 0 8 2
A __construct() 0 4 1
A isPermitted() 0 15 5
A getFields() 0 12 3
A getFieldsFromApi() 0 10 2
A getField() 0 10 3
A getFieldModel() 0 7 2
A getDefaultView() 0 4 1
A getDefaultUrl() 0 4 1
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 in module.
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 fields and blocks in module.
104
	 *
105
	 * @return array
106
	 */
107
	public function getFieldsFromApi(): array
108
	{
109
		if (\App\Cache::has('moduleFields', $this->moduleName)) {
110
			$data = \App\Cache::get('moduleFields', $this->moduleName);
111
		} else {
112
			$data = Api::getInstance()->call($this->moduleName . '/Fields');
113
			\App\Cache::save('moduleFields', $this->moduleName, $data);
114
		}
115
		return $data;
116
	}
117
118
	/**
119
	 * Get field by name.
120
	 *
121
	 * @param string $name
122
	 *
123
	 * @return array
124
	 */
125
	public function getField(string $name): array
126
	{
127
		if (empty($this->fields)) {
128
			$this->getFields();
129
		}
130
		if (empty($this->fields[$name])) {
131
			throw new \App\Exceptions\AppException("Field not found: {$name}");
132
		}
133
		return $this->fields[$name];
134
	}
135
136
	/**
137
	 * Get field by name.
138
	 *
139
	 * @param string $name
140
	 *
141
	 * @return \YF\Modules\Base\FieldTypes\BaseField
142
	 */
143
	public function getFieldModel(string $name): \YF\Modules\Base\FieldTypes\BaseField
144
	{
145
		if (!isset($this->fieldsModels[$name])) {
146
			$this->fieldsModels[$name] = Field::getInstance($this->moduleName, $this->getField($name));
147
		}
148
		return $this->fieldsModels[$name];
149
	}
150
151
	/**
152
	 * Returns default view for module.
153
	 *
154
	 * @return string
155
	 */
156
	public function getDefaultView(): string
157
	{
158
		return $this->defaultView;
159
	}
160
161
	/**
162
	 * Returns default address url.
163
	 *
164
	 * @return string
165
	 */
166
	public function getDefaultUrl(): string
167
	{
168
		return "index.php?module={$this->moduleName}&view={$this->defaultView}";
169
	}
170
}
171