Passed
Pull Request — developer (#15351)
by Arkadiusz
17:36
created

getFieldInstanceByName()   C

Complexity

Conditions 15
Paths 24

Size

Total Lines 70
Code Lines 61

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 61
dl 0
loc 70
rs 5.9166
c 0
b 0
f 0
cc 15
nc 24
nop 1

How to fix   Long Method    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
3
/**
4
 * WebservicePremium Record Model.
5
 *
6
 * @package Settings
7
 *
8
 * @copyright YetiForce Sp. z o.o
9
 * @license   YetiForce Public License 4.0 (licenses/LicenseEN.txt or yetiforce.com)
10
 * @author    Radosław Skrzypczak <[email protected]>
11
 * @author  Mariusz Krzaczkowski <[email protected]>
12
 */
13
14
/**
15
 * Settings_WebserviceUsers_WebservicePremium_Service class.
16
 */
17
class Settings_WebserviceUsers_WebservicePremium_Service extends Settings_WebserviceUsers_WebserviceStandard_Service
18
{
19
	/** {@inheritdoc} */
20
	public $baseTable = 'w_#__portal_user';
21
22
	/** {@inheritdoc} */
23
	public $editFields = [
24
		'server_id' => 'FL_SERVER',
25
		'status' => 'FL_STATUS',
26
		'password' => 'FL_PASSWORD',
27
		'type' => 'FL_TYPE',
28
		'crmid' => 'FL_RECORD_NAME',
29
		'user_id' => 'FL_USER',
30
		'login_method' => 'FL_LOGIN_METHOD',
31
		'authy_methods' => 'FL_AUTHY_METHODS',
32
		'istorage' => 'FL_STORAGE',
33
		'language' => 'FL_LANGUAGE',
34
	];
35
36
	/** {@inheritdoc} */
37
	public $listFields = [
38
		'server_id' => 'FL_SERVER',
39
		'user_name' => 'FL_LOGIN',
40
		'crmid' => 'FL_RECORD_NAME',
41
		'type' => 'FL_TYPE',
42
		'user_id' => 'FL_USER',
43
		'status' => 'FL_STATUS',
44
		'istorage' => 'FL_STORAGE',
45
		'login_method' => 'FL_LOGIN_METHOD',
46
		'login_time' => 'FL_LOGIN_TIME',
47
		'custom_params' => 'FL_CUSTOM_PARAMS',
48
	];
49
50
	/** @var array Columns to show on the list session. */
51
	public $columnsToShow = [
52
		'time' => 'FL_LOGIN_TIME',
53
		'status' => 'FL_STATUS',
54
		'agent' => 'LBL_USER_AGENT',
55
		'ip' => 'LBL_IP_ADDRESS',
56
		'device_id' => 'LBL_DEVICE_ID',
57
	];
58
59
	/** {@inheritdoc} */
60
	public function getFieldInstanceByName($name)
61
	{
62
		$moduleName = $this->getModule()->getName(true);
63
		$fieldsLabel = $this->getEditFields();
64
		$params = ['uitype' => 1, 'column' => $name, 'name' => $name, 'label' => $fieldsLabel[$name], 'displaytype' => 1, 'typeofdata' => 'V~M', 'presence' => 0, 'isEditableReadOnly' => false];
65
		switch ($name) {
66
			case 'crmid':
67
				$params['uitype'] = 10;
68
				$params['referenceList'] = ['Contacts'];
69
				$params['fieldparams'] = [
70
					'searchParams' => '[[["email","ny",""]]]',
71
				];
72
				break;
73
			case 'istorage':
74
				$params['uitype'] = 10;
75
				$params['referenceList'] = ['IStorages'];
76
				$params['typeofdata'] = 'V~O';
77
				break;
78
			case 'status':
79
				$params['uitype'] = 16;
80
				$params['picklistValues'] = [1 => \App\Language::translate('FL_ACTIVE'), 0 => \App\Language::translate('FL_INACTIVE')];
81
				break;
82
			case 'server_id':
83
				$servers = Settings_WebserviceApps_Module_Model::getActiveServers($this->getModule()->typeApi);
84
				$params['uitype'] = 16;
85
				$params['picklistValues'] = [];
86
				foreach ($servers as $key => $value) {
87
					$params['picklistValues'][$key] = $value['name'];
88
				}
89
				break;
90
			case 'type':
91
				$params['uitype'] = 16;
92
				$params['picklistValues'] = [];
93
				foreach ($this->getTypeValues() as $key => $value) {
0 ignored issues
show
Bug introduced by
The expression $this->getTypeValues() of type string is not traversable.
Loading history...
94
					$params['picklistValues'][$key] = \App\Language::translate($value, $moduleName);
95
				}
96
				break;
97
			case 'language':
98
				$params['typeofdata'] = 'V~O';
99
				$params['uitype'] = 32;
100
				$params['picklistValues'] = \App\Language::getAll();
101
				break;
102
			case 'user_id':
103
				$params['uitype'] = 16;
104
				$params['picklistValues'] = \App\Fields\Owner::getInstance($moduleName)->getAccessibleUsers('', 'owner');
105
				break;
106
			case 'login_method':
107
				$params['uitype'] = 16;
108
				$params['picklistValues'] = [
109
					'PLL_PASSWORD' => \App\Language::translate('PLL_PASSWORD', 'Users'),
110
					'PLL_PASSWORD_2FA' => \App\Language::translate('PLL_PASSWORD_2FA', 'Users'),
111
				];
112
				break;
113
			case 'authy_methods':
114
				$params['uitype'] = 16;
115
				$params['typeofdata'] = 'V~O';
116
				$params['picklistValues'] = [
117
					'-' => \App\Language::translate('LBL_NONE'),
118
					'PLL_AUTHY_TOTP' => \App\Language::translate('PLL_AUTHY_TOTP', 'Users'),
119
				];
120
				break;
121
			case 'password':
122
				$params['typeofdata'] = 'P~M';
123
				if ($this->has('id')) {
124
					$params = null;
125
				}
126
				break;
127
			default: break;
128
		}
129
		return $params ? Settings_Vtiger_Field_Model::init($moduleName, $params) : null;
130
	}
131
132
	/** {@inheritdoc} */
133
	public function setDataFromRequest(App\Request $request)
134
	{
135
		foreach (array_keys($this->getEditFields()) as $field) {
136
			if ($request->has($field)) {
137
				switch ($field) {
138
					case 'server_id':
139
					case 'status':
140
					case 'type':
141
					case 'user_id':
142
					case 'istorage':
143
						$value = $request->getInteger($field);
144
						break;
145
					case 'crmid':
146
						$value = $request->isEmpty('crmid') ? '' : $request->getInteger('crmid');
147
						break;
148
					case 'user_name':
149
					case 'language':
150
					case 'login_method':
151
					case 'authy_methods':
152
						$value = $request->getByType($field, 'Text');
153
						break;
154
					case 'password':
155
						if (!$this->isNew()) {
156
							throw new \App\Exceptions\Security("ERR_ILLEGAL_FIELD_VALUE||{$field}", 406);
157
						}
158
						$value = $request->getRaw($field, null);
159
						parent::set($field, $value);
160
						break;
161
					default:
162
					throw new \App\Exceptions\Security("ERR_ILLEGAL_FIELD_VALUE||{$field}", 406);
163
						break;
164
				}
165
				$this->set($field, $this->getValueToSave($field, $value));
166
			}
167
		}
168
	}
169
170
	/** {@inheritdoc} */
171
	public function getValueToSave($key, $value)
172
	{
173
		switch ($key) {
174
			case 'server_id':
175
			case 'istorage':
176
			case 'status':
177
			case 'type':
178
			case 'crmid':
179
			case 'user_id':
180
				$value = (int) $value;
181
				break;
182
			case 'password':
183
				$value = App\Encryption::createPasswordHash($value, 'WebservicePremium');
184
				break;
185
			default: break;
186
		}
187
		return $value;
188
	}
189
}
190