Passed
Push — developer ( 6b5868...bed0f9 )
by Radosław
22:42 queued 03:39
created

Settings_ConfigEditor_Module_Model   B

Complexity

Total Complexity 43

Size/Duplication

Total Lines 313
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 43
eloc 205
c 0
b 0
f 0
dl 0
loc 313
rs 8.96

8 Methods

Rating   Name   Duplication   Size   Complexity  
A getDisplayValue() 0 11 2
A getEditFields() 0 20 5
F getFieldInstanceByName() 0 138 27
A getEditViewUrl() 0 5 1
A getMenuItem() 0 3 1
A getInstance() 0 3 1
A getDetailViewUrl() 0 5 1
A init() 0 17 5

How to fix   Complexity   

Complex Class

Complex classes like Settings_ConfigEditor_Module_Model often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.

Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.

While breaking up the class, it is a good idea to analyze how other classes use Settings_ConfigEditor_Module_Model, and based on these observations, apply Extract Interface, too.

1
<?php
2
/**
3
 * Config editor basic module file.
4
 *
5
 * @package   Settings.Model
6
 *
7
 * @copyright YetiForce S.A.
8
 * @license   YetiForce Public License 6.5 (licenses/LicenseEN.txt or yetiforce.com)
9
 */
10
/**
11
 * Config editor basic module class.
12
 */
13
class Settings_ConfigEditor_Module_Model extends Settings_Vtiger_Module_Model
14
{
15
	/** {@inheritdoc} */
16
	public $name = 'ConfigEditor';
17
	/** {@inheritdoc} */
18
	public $listFields = [
19
		'upload_maxsize' => 'LBL_MAX_UPLOAD_SIZE',
20
		'default_module' => 'LBL_DEFAULT_MODULE',
21
		'listview_max_textlength' => 'LBL_MAX_TEXT_LENGTH_IN_LISTVIEW',
22
		'list_max_entries_per_page' => 'LBL_MAX_ENTRIES_PER_PAGE_IN_LISTVIEW',
23
		'defaultLayout' => 'LBL_DEFAULT_LAYOUT',
24
		'breadcrumbs' => 'LBL_SHOWING_BREADCRUMBS',
25
		'title_max_length' => 'LBL_TITLE_MAX_LENGTH',
26
		'MINIMUM_CRON_FREQUENCY' => 'LBL_MINIMUM_CRON_FREQUENCY',
27
		'listMaxEntriesMassEdit' => 'LBL_LIST_MAX_ENTRIES_MASSEDIT',
28
		'backgroundClosingModal' => 'LBL_BG_CLOSING_MODAL',
29
		'href_max_length' => 'LBL_HREF_MAX_LEGTH',
30
		'langInLoginView' => 'LBL_SHOW_LANG_IN_LOGIN_PAGE',
31
		'layoutInLoginView' => 'LBL_SHOW_LAYOUT_IN_LOGIN_PAGE',
32
	];
33
	/** @var array Fields for performance */
34
	public $performanceFields = [
35
		'MAX_NUMBER_EXPORT_RECORDS' => 'LBL_MAX_NUMBER_EXPORT_RECORDS'
36
	];
37
38
	/** @var array Fields for relation */
39
	public $relationFields = [
40
		'SHOW_RELATED_MODULE_NAME' => 'LBL_RELATION_SHOW_RELATED_MODULE_NAME',
41
		'SHOW_RELATED_ICON' => 'LBL_RELATION_SHOW_RELATED_ICON',
42
		'SHOW_RECORDS_COUNT' => 'LBL_RELATION_SHOW_RECORDS_COUNT',
43
		'COMMENT_MAX_LENGTH' => 'LBL_RELATION_COMMENT_MAX_LENGTH',
44
		'separateChangeRelationButton' => 'LBL_RELATION_SEPARATE_CHANGE_RELATION_BUTTON',
45
	];
46
47
	/** @var array Fields for branding */
48
	public $brandingFields = [
49
		'footerName' => 'LBL_BRAND_DATA_NAME',
50
		'urlFacebook' => 'LBL_FACEBOOK',
51
		'urlLinkedIn' => 'LBL_LINKEDIN',
52
		'urlTwitter' => 'LBL_TWITTER'
53
	];
54
55
	/** @var string Configuration type */
56
	public $type;
57
58
	/**
59
	 * Function to initiation.
60
	 *
61
	 * @param string $type
62
	 *
63
	 * @throws \ReflectionException
64
	 */
65
	public function init(string $type = 'Main')
66
	{
67
		$this->type = $type;
68
		foreach (array_keys($this->getEditFields()) as $fieldName) {
69
			$source = $this->getFieldInstanceByName($fieldName)->get('source');
70
			[$type, $component] = strpos($source, ':') ? explode(':', $source, 2) : [$source, ''];
71
			if ($component) {
72
				$value = \App\Config::{$type}($component, $fieldName);
73
			} else {
74
				$value = \App\Config::{$type}($fieldName);
75
			}
76
			if ('upload_maxsize' === $fieldName) {
77
				$value /= 1048576;
78
			}
79
			$this->set($fieldName, $value);
80
		}
81
		return $this;
82
	}
83
84
	/**
85
	 * Gets fields for edit.
86
	 *
87
	 * @param mixed|null $configName
88
	 *
89
	 * @return array
90
	 */
91
	public function getEditFields($configName = null): array
92
	{
93
		$fields = [];
94
		switch ($configName ?? $this->type) {
95
			case 'Main':
96
				$fields = $this->listFields;
97
				break;
98
			case 'Relation':
99
				$fields = $this->relationFields;
100
				break;
101
			case 'Performance':
102
				$fields = $this->performanceFields;
103
				break;
104
			case 'Branding':
105
				$fields = $this->brandingFields;
106
				break;
107
			default:
108
				break;
109
		}
110
		return $fields;
111
	}
112
113
	/**
114
	 * Function to get CompanyDetails Menu item.
115
	 *
116
	 * @return menu item Model
0 ignored issues
show
Bug introduced by
The type menu was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
117
	 */
118
	public function getMenuItem()
119
	{
120
		return Settings_Vtiger_MenuItem_Model::getInstance('LBL_CONFIG_EDITOR');
0 ignored issues
show
Bug Best Practice introduced by
The expression return Settings_Vtiger_M...ce('LBL_CONFIG_EDITOR') returns the type Settings_Vtiger_MenuItem_Model which is incompatible with the documented return type menu.
Loading history...
121
	}
122
123
	/**
124
	 * Function to get Edit view Url.
125
	 *
126
	 * @return string Url
127
	 */
128
	public function getEditViewUrl()
129
	{
130
		$menuItem = $this->getMenuItem();
131
132
		return 'index.php?module=ConfigEditor&parent=Settings&view=Edit&block=' . $menuItem->get('blockid') . '&fieldid=' . $menuItem->get('fieldid');
133
	}
134
135
	/**
136
	 * Function to get Detail view Url.
137
	 *
138
	 * @return string Url
139
	 */
140
	public function getDetailViewUrl()
141
	{
142
		$menuItem = $this->getMenuItem();
143
144
		return 'index.php?module=ConfigEditor&parent=Settings&view=Detail&block=' . $menuItem->get('blockid') . '&fieldid=' . $menuItem->get('fieldid');
145
	}
146
147
	/**
148
	 * Function to get the instance of Config module model.
149
	 *
150
	 * @param mixed $name
151
	 *
152
	 * @throws \ReflectionException
153
	 *
154
	 * @return self
155
	 */
156
	public static function getInstance($name = 'Settings:Vtiger')
157
	{
158
		return new self();
159
	}
160
161
	/**
162
	 * Function determines fields available in edition view.
163
	 *
164
	 * @param string $name
165
	 *
166
	 * @return \Settings_Vtiger_Field_Model
167
	 */
168
	public function getFieldInstanceByName($name)
169
	{
170
		$moduleName = $this->getName(true);
171
		$params = ['uitype' => 7, 'column' => $name, 'name' => $name,  'displaytype' => 1, 'typeofdata' => 'I~M', 'presence' => 0, 'isEditableReadOnly' => false, 'maximumlength' => '', 'validator' => [['name' => 'NumberRange100']], 'source' => 'main'];
172
		switch ($name) {
173
			case 'MAX_NUMBER_EXPORT_RECORDS':
174
				$params['label'] = $this->performanceFields[$name];
175
				$params['validator'] = [['name' => 'WholeNumberGreaterThanZero']];
176
				$params['uitype'] = 7;
177
				$params['maximumlength'] = '99999999';
178
				$params['source'] = 'performance';
179
				$params['purifyType'] = \App\Purifier::TEXT;
180
				$params['fieldvalue'] = $this->get($name);
181
				break;
182
			case 'listMaxEntriesMassEdit':
183
				$params['maximumlength'] = '5000';
184
				$params['validator'] = [['name' => 'WholeNumberGreaterThanZero']];
185
				$params['label'] = $this->listFields[$name];
186
				$params['purifyType'] = \App\Purifier::TEXT;
187
				break;
188
			case 'upload_maxsize':
189
				$params['label'] = $this->listFields[$name];
190
				$params['purifyType'] = \App\Purifier::TEXT;
191
				$params['maximumlength'] = (string) round(\App\Config::getMaxUploadSize(false, true), 0) ?: '';
192
				unset($params['validator']);
193
				break;
194
			case 'layoutInLoginView':
195
			case 'langInLoginView':
196
			case 'backgroundClosingModal':
197
				$params['label'] = $this->listFields[$name];
198
				$params['uitype'] = 56;
199
				$params['typeofdata'] = 'C~M';
200
				$params['purifyType'] = \App\Purifier::BOOL;
201
				unset($params['validator']);
202
				break;
203
			case 'breadcrumbs':
204
				$params['label'] = $this->listFields[$name];
205
				$params['uitype'] = 56;
206
				$params['typeofdata'] = 'C~M';
207
				$params['source'] = 'layout';
208
				$params['purifyType'] = \App\Purifier::BOOL;
209
				unset($params['validator']);
210
				break;
211
			case 'default_module':
212
				$params['label'] = $this->listFields[$name];
213
				$params['uitype'] = 16;
214
				$params['maximumlength'] = '40';
215
				unset($params['validator']);
216
				$params['picklistValues'] = ['Home' => \App\Language::translate('Home')];
217
				$params['purifyType'] = \App\Purifier::TEXT;
218
				foreach (\vtlib\Functions::getAllModules(true, false, 0) as $module) {
219
					$params['picklistValues'][$module['name']] = \App\Language::translate($module['name'], $module['name']);
220
				}
221
				break;
222
			case 'defaultLayout':
223
				$params['label'] = $this->listFields[$name];
224
				$params['uitype'] = 16;
225
				$params['maximumlength'] = '50';
226
				$params['picklistValues'] = \App\Layout::getAllLayouts();
227
				$params['purifyType'] = \App\Purifier::TEXT;
228
				unset($params['validator']);
229
				break;
230
				// Realtion
231
			case 'COMMENT_MAX_LENGTH':
232
				$params['label'] = $this->relationFields[$name];
233
				$params['uitype'] = 7;
234
				$params['maximumlength'] = '200';
235
				$params['validator'] = [['name' => 'WholeNumberGreaterThanZero']];
236
				$params['source'] = 'relation';
237
				$params['tooltip'] = 'LBL_RELATION_COMMENT_MAX_LENGTH_DESC';
238
				$params['fieldvalue'] = $this->get($name);
239
				$params['purifyType'] = \App\Purifier::TEXT;
240
				break;
241
			case 'SHOW_RELATED_MODULE_NAME':
242
			case 'SHOW_RELATED_ICON':
243
			case 'SHOW_RECORDS_COUNT':
244
				$params['label'] = $this->relationFields[$name];
245
				$params['uitype'] = 56;
246
				$params['typeofdata'] = 'C~M';
247
				$params['source'] = 'relation';
248
				$params['fieldvalue'] = $this->get($name);
249
				$params['purifyType'] = \App\Purifier::BOOL;
250
				unset($params['validator']);
251
				break;
252
			case 'separateChangeRelationButton':
253
				$params['label'] = $this->relationFields[$name];
254
				$params['uitype'] = 56;
255
				$params['typeofdata'] = 'C~M';
256
				$params['source'] = 'relation';
257
				$params['fieldvalue'] = $this->get($name);
258
				$params['tooltip'] = 'LBL_RELATION_SEPARATE_CHANGE_RELATION_BUTTON_DESC';
259
				$params['purifyType'] = \App\Purifier::BOOL;
260
				unset($params['validator']);
261
				break;
262
			case 'title_max_length':
263
			case 'MINIMUM_CRON_FREQUENCY':
264
				$params['uitype'] = 7;
265
				$params['purifyType'] = \App\Purifier::TEXT;
266
				$params['maximumlength'] = '0,100';
267
				break;
268
			case 'listview_max_textlength':
269
			case 'list_max_entries_per_page':
270
			case 'href_max_length':
271
				$params['uitype'] = 7;
272
				$params['purifyType'] = \App\Purifier::INTEGER;
273
				$params['maximumlength'] = '255';
274
				break;
275
				// Branding
276
			case 'footerName':
277
				$params['label'] = $this->brandingFields[$name];
278
				$params['uitype'] = 1;
279
				$params['typeofdata'] = 'V~O';
280
				$params['purifyType'] = \App\Purifier::TEXT;
281
				$params['maximumlength'] = '255';
282
				$params['source'] = 'component:Branding';
283
				$params['fieldvalue'] = $this->get($name);
284
				$params['isEditableReadOnly'] = !\App\YetiForce\Shop::check('YetiForceDisableBranding');
285
				unset($params['validator']);
286
				break;
287
			case 'urlFacebook':
288
			case 'urlLinkedIn':
289
			case 'urlTwitter':
290
				$config = new \App\ConfigFile('component', 'Branding');
291
				$defaullt = $config->getTemplate($name)['default'];
292
				$params['label'] = $this->brandingFields[$name];
293
				$params['uitype'] = 17;
294
				$params['typeofdata'] = 'V~O';
295
				$params['purifyType'] = \App\Purifier::URL;
296
				$params['maximumlength'] = '255';
297
				$params['source'] = 'component:Branding';
298
				$params['fieldvalue'] = $defaullt === $this->get($name) ? '' : $this->get($name);
299
				$params['isEditableReadOnly'] = !\App\YetiForce\Shop::check('YetiForceDisableBranding');
300
				unset($params['validator']);
301
				break;
302
			default:
303
				break;
304
		}
305
		return Settings_Vtiger_Field_Model::init($moduleName, $params);
306
	}
307
308
	/**
309
	 * Function to getDisplay value of every field.
310
	 *
311
	 * @param string $name field name
312
	 *
313
	 * @return mixed
314
	 */
315
	public function getDisplayValue($name)
316
	{
317
		switch ($name) {
318
			case 'upload_maxsize':
319
				$value = $this->get($name) . ' ' . \App\Language::translate('LBL_MB', $this->getName(true));
320
				break;
321
			default:
322
				$value = $this->getFieldInstanceByName($name)->getDisplayValue($this->get($name));
323
				break;
324
		}
325
		return $value;
326
	}
327
}
328