Passed
Push — developer ( 4e3135...f5c82a )
by Radosław
30:25 queued 12:59
created

getFieldInstanceByName()   F

Complexity

Conditions 28
Paths 54

Size

Total Lines 323
Code Lines 290

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 812

Importance

Changes 0
Metric Value
eloc 290
dl 0
loc 323
rs 3.3333
c 0
b 0
f 0
ccs 0
cts 0
cp 0
cc 28
nc 54
nop 1
crap 812

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
 * MailSmtp module model class.
5
 *
6
 * @copyright YetiForce S.A.
7
 * @license YetiForce Public License 5.0 (licenses/LicenseEN.txt or yetiforce.com)
8
 * @author Adrian Koń <[email protected]>
9
 * @author Radosław Skrzypczak <[email protected]>
10
 */
11
class Settings_MailSmtp_Module_Model extends Settings_Vtiger_Module_Model
12
{
13
	public $baseTable = 's_#__mail_smtp';
14
	public $baseIndex = 'id';
15
	public $listFields = ['name' => 'LBL_NAME', 'host' => 'LBL_HOST', 'port' => 'LBL_PORT', 'username' => 'LBL_USERNAME', 'from_email' => 'LBL_FROM_EMAIL', 'default' => 'LBL_DEFAULT'];
16
	public $name = 'MailSmtp';
17
18
	/**
19
	 * Function to get the url for default view of the module.
20
	 *
21
	 * @return string URL
22
	 */
23
	public function getDefaultUrl()
24
	{
25
		return 'index.php?module=MailSmtp&parent=Settings&view=List';
26
	}
27
28
	/**
29
	 * Function to get the url for create view of the module.
30
	 *
31
	 * @return string URL
32
	 */
33
	public function getCreateRecordUrl()
34
	{
35
		return 'index.php?module=MailSmtp&parent=Settings&view=Edit';
36
	}
37
38
	public static function getSmtpNames()
39
	{
40
		return (new \App\Db\Query())->select(['id', 'name'])->from('s_#__mail_smtp')->all(\App\Db::getInstance('admin'));
41
	}
42
43
	/** @var string[] Fields name for edit view */
44
	public $editFields = [
45
		'name', 'mailer_type', 'default', 'mail_account', 'secure', 'host', 'port', 'username', 'password', 'from_name', 'from_email', 'reply_to', 'authentication', 'individual_delivery', 'priority', 'confirm_reading_to', 'organization', 'unsubscribe', 'options', 'save_send_mail', 'imap_host', 'imap_port', 'imap_username', 'imap_password', 'imap_folder', 'imap_validate_cert'
46
	];
47
48
	/**
49
	 * Get block icon.
50
	 *
51
	 * @param string $name
52
	 *
53
	 * @return string
54
	 */
55
	public function getBlockIcon($name): string
0 ignored issues
show
Unused Code introduced by
The parameter $name is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

55
	public function getBlockIcon(/** @scrutinizer ignore-unused */ $name): string

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
56
	{
57
		return '';
58
	}
59
60
	/**
61
	 * Get fields instance by name.
62
	 *
63
	 * @param string $name
64
	 *
65
	 * @return Vtiger_Field_Model
66
	 */
67
	public function getFieldInstanceByName(string $name)
68
	{
69
		$moduleName = $this->getName(true);
70
		$params = [];
71
		switch ($name) {
72
			case 'mail_account':
73
				$params = ['column' => $name, 'name' => $name,  'displaytype' => 1, 'typeofdata' => 'I~M', 'presence' => 0, 'isEditableReadOnly' => false, 'defaultvalue' => '0'];
74
				$params['uitype'] = 10;
75
				$params['label'] = 'FL_MAIL_ACCOUNT';
76
				$params['referenceList'] = ['MailAccount'];
77
				$params['blockLabel'] = 'BL_BASE';
78
				$params['fieldparams'] = [
79
					'searchParams' => '[[["mailaccount_status","e","PLL_ACTIVE"]]]',
80
				];
81
				break;
82
			case 'name':
83
				$params = [
84
					'name' => $name,
85
					'label' => 'LBL_NAME',
86
					'uitype' => 1,
87
					'typeofdata' => 'V~M',
88
					'maximumlength' => '255',
89
					'defaultvalue' => '',
90
					'purifyType' => \App\Purifier::TEXT,
91
					'blockLabel' => 'BL_BASE'
92
				];
93
				break;
94
			case 'mailer_type':
95
				$params = [
96
					'name' => $name,
97
					'label' => 'LBL_MAILER_TYPE',
98
					'uitype' => 16,
99
					'typeofdata' => 'V~M',
100
					'maximumlength' => '10',
101
					'purifyType' => \App\Purifier::ALNUM,
102
					'blockLabel' => 'BL_BASE',
103
					'defaultvalue' => 'yfsmtp',
104
					'picklistValues' => [
105
						'yfsmtp' => \App\Language::translate('LBL_SMTP_MAIL_ACCOUNT', $moduleName),
106
						'smtp' => \App\Language::translate('LBL_SMTP', $moduleName),
107
						'sendmail' => \App\Language::translate('LBL_SENDMAIL', $moduleName),
108
						'mail' => \App\Language::translate('LBL_MAIL', $moduleName),
109
						'qmail' => \App\Language::translate('LBL_QMAIL', $moduleName),
110
					]
111
				];
112
				break;
113
			case 'default':
114
				$params = [
115
					'name' => $name,
116
					'label' => 'LBL_DEFAULT',
117
					'uitype' => 56,
118
					'typeofdata' => 'C~O',
119
					'maximumlength' => '1',
120
					'defaultvalue' => 0,
121
					'purifyType' => \App\Purifier::BOOL,
122
					'blockLabel' => 'BL_BASE'
123
				];
124
				break;
125
			case 'host':
126
				$params = [
127
					'name' => $name,
128
					'label' => 'LBL_HOST',
129
					'uitype' => 17,
130
					'typeofdata' => 'V~O',
131
					'maximumlength' => '240',
132
					'purifyType' => \App\Purifier::URL,
133
					'blockLabel' => 'BL_BASE'
134
				];
135
				break;
136
			case 'port':
137
				$params = [
138
					'name' => $name,
139
					'label' => 'LBL_PORT',
140
					'uitype' => 7,
141
					'typeofdata' => 'I~O',
142
					'maximumlength' => '0,65535',
143
					'purifyType' => \App\Purifier::INTEGER,
144
					'blockLabel' => 'BL_BASE'
145
				];
146
				break;
147
			case 'authentication':
148
				$params = [
149
					'name' => $name,
150
					'label' => 'LBL_AUTHENTICATION',
151
					'uitype' => 56,
152
					'typeofdata' => 'C~O',
153
					'maximumlength' => '1',
154
					'purifyType' => \App\Purifier::BOOL,
155
					'blockLabel' => 'BL_BASE'
156
				];
157
				break;
158
			case 'username':
159
				$params = [
160
					'name' => $name,
161
					'label' => 'LBL_USERNAME',
162
					'uitype' => 1,
163
					'typeofdata' => 'V~O',
164
					'maximumlength' => '255',
165
					'purifyType' => \App\Purifier::TEXT,
166
					'blockLabel' => 'BL_BASE'
167
				];
168
				break;
169
			case 'password':
170
				$params = [
171
					'name' => $name,
172
					'label' => 'LBL_PASSWORD',
173
					'uitype' => 99,
174
					'typeofdata' => 'V~O',
175
					'maximumlength' => '255',
176
					'purifyType' => 'raw',
177
					'blockLabel' => 'BL_BASE',
178
					'fromOutsideList' => true
179
				];
180
				break;
181
			case 'individual_delivery':
182
				$params = [
183
					'name' => $name,
184
					'label' => 'LBL_INDIVIDUAL_DELIVERY',
185
					'uitype' => 56,
186
					'typeofdata' => 'C~O',
187
					'maximumlength' => '1',
188
					'purifyType' => \App\Purifier::BOOL,
189
					'blockLabel' => 'BL_BASE',
190
					'tooltip' => 'LBL_INDIVIDUAL_DELIVERY_INFO'
191
				];
192
				break;
193
			case 'secure':
194
				$params = [
195
					'name' => $name,
196
					'label' => 'LBL_SECURE',
197
					'uitype' => 16,
198
					'typeofdata' => 'V~O',
199
					'maximumlength' => '5',
200
					'purifyType' => \App\Purifier::STANDARD,
201
					'blockLabel' => 'BL_BASE',
202
					'defaultvalue' => ''
203
				];
204
				$params['picklistValues'] = [
205
					'ssl' => \App\Language::translate('LBL_SSL', $moduleName),
206
					'tls' => \App\Language::translate('LBL_TLS', $moduleName)
207
				];
208
				break;
209
			case 'from_name':
210
				$params = [
211
					'name' => $name,
212
					'label' => 'LBL_FROM_NAME',
213
					'uitype' => 1,
214
					'typeofdata' => 'V~O',
215
					'maximumlength' => '255',
216
					'purifyType' => \App\Purifier::TEXT,
217
					'blockLabel' => 'BL_BASE'
218
				];
219
				break;
220
			case 'from_email':
221
				$params = [
222
					'name' => $name,
223
					'label' => 'LBL_FROM_EMAIL',
224
					'uitype' => 13,
225
					'typeofdata' => 'V~O',
226
					'maximumlength' => '255',
227
					'purifyType' => 'Email',
228
					'blockLabel' => 'BL_BASE'
229
				];
230
				break;
231
			case 'reply_to':
232
				$params = [
233
					'name' => $name,
234
					'label' => 'LBL_REPLY_TO',
235
					'uitype' => 13,
236
					'typeofdata' => 'V~O',
237
					'maximumlength' => '255',
238
					'purifyType' => 'Email',
239
					'blockLabel' => 'BL_BASE'
240
				];
241
				break;
242
			case 'priority':
243
				$params = [
244
					'name' => $name,
245
					'label' => 'LBL_MAIL_PRIORITY',
246
					'uitype' => 16,
247
					'typeofdata' => 'V~O',
248
					'maximumlength' => '255',
249
					'purifyType' => \App\Purifier::ALNUM_EXTENDED,
250
					'blockLabel' => 'LBL_ADDITIONAL_HEADERS',
251
					'defaultvalue' => '',
252
					'picklistValues' => [
253
						'normal' => \App\Language::translate('LBL_NORMAL', $moduleName),
254
						'non-urgent' => \App\Language::translate('LBL_NO_URGENT', $moduleName),
255
						'urgent' => \App\Language::translate('LBL_URGENT', $moduleName)
256
					]
257
				];
258
				break;
259
			case 'confirm_reading_to':
260
				$params = [
261
					'name' => $name,
262
					'label' => 'LBL_CONFIRM_READING_TO',
263
					'uitype' => 13,
264
					'typeofdata' => 'V~O',
265
					'maximumlength' => '255',
266
					'purifyType' => 'Email',
267
					'blockLabel' => 'LBL_ADDITIONAL_HEADERS'
268
				];
269
				break;
270
			case 'organization':
271
				$params = [
272
					'name' => $name,
273
					'label' => 'LBL_ORGANIZATION',
274
					'uitype' => 1,
275
					'typeofdata' => 'V~O',
276
					'maximumlength' => '255',
277
					'purifyType' => \App\Purifier::TEXT,
278
					'blockLabel' => 'LBL_ADDITIONAL_HEADERS'
279
				];
280
				break;
281
			case 'unsubscribe':
282
				$params = [
283
					'label' => 'LBL_UNSUBSCIBE',
284
					'uitype' => 33,
285
					'maximumlength' => '255',
286
					'typeofdata' => 'V~O',
287
					'purifyType' => \App\Purifier::TEXT,
288
					'createTags' => true,
289
					'tooltip' => 'LBL_UNSUBSCRIBE_INFO',
290
					'picklistValues' => [],
291
					'blockLabel' => 'LBL_ADDITIONAL_HEADERS'
292
				];
293
				break;
294
			case 'options':
295
				$params = [
296
					'label' => 'LBL_OPTIONS',
297
					'uitype' => 21,
298
					'maximumlength' => '6500',
299
					'typeofdata' => 'V~O',
300
					'purifyType' => \App\Purifier::TEXT,
301
					'tooltip' => 'LBL_OPTIONS_INFO',
302
					'blockLabel' => 'LBL_ADDITIONAL_HEADERS'
303
				];
304
				break;
305
			case 'save_send_mail':
306
				$params = [
307
					'name' => $name,
308
					'label' => 'LBL_SAVE_SEND_MAIL',
309
					'uitype' => 56,
310
					'typeofdata' => 'C~O',
311
					'maximumlength' => '1',
312
					'defaultvalue' => 0,
313
					'purifyType' => \App\Purifier::BOOL,
314
					'tooltip' => 'LBL_SAVE_SEND_MAIL_INFO',
315
					'blockLabel' => 'LBL_SAVE_SENT_MESSAGE'
316
				];
317
				break;
318
			case 'imap_host':
319
				$params = [
320
					'name' => $name,
321
					'label' => 'LBL_HOST',
322
					'uitype' => 17,
323
					'typeofdata' => 'V~O',
324
					'maximumlength' => '240',
325
					'purifyType' => \App\Purifier::URL,
326
					'blockLabel' => 'LBL_SAVE_SENT_MESSAGE'
327
				];
328
				break;
329
			case 'imap_port':
330
				$params = [
331
					'name' => $name,
332
					'label' => 'LBL_PORT',
333
					'uitype' => 7,
334
					'typeofdata' => 'I~O',
335
					'maximumlength' => '0,65535',
336
					'purifyType' => \App\Purifier::INTEGER,
337
					'blockLabel' => 'LBL_SAVE_SENT_MESSAGE'
338
				];
339
				break;
340
			case 'imap_validate_cert':
341
				$params = [
342
					'name' => $name,
343
					'label' => 'LBL_VALIDATE_CERT',
344
					'uitype' => 56,
345
					'typeofdata' => 'C~O',
346
					'maximumlength' => '1',
347
					'purifyType' => \App\Purifier::BOOL,
348
					'blockLabel' => 'LBL_SAVE_SENT_MESSAGE'
349
				];
350
				break;
351
			case 'imap_username':
352
				$params = [
353
					'name' => $name,
354
					'label' => 'LBL_USERNAME',
355
					'uitype' => 1,
356
					'typeofdata' => 'V~O',
357
					'maximumlength' => '255',
358
					'purifyType' => \App\Purifier::TEXT,
359
					'blockLabel' => 'LBL_SAVE_SENT_MESSAGE'
360
				];
361
				break;
362
			case 'imap_password':
363
				$params = [
364
					'name' => $name,
365
					'label' => 'LBL_PASSWORD',
366
					'uitype' => 99,
367
					'typeofdata' => 'V~O',
368
					'maximumlength' => '255',
369
					'purifyType' => 'raw',
370
					'blockLabel' => 'LBL_SAVE_SENT_MESSAGE',
371
					'fromOutsideList' => true
372
				];
373
				break;
374
			case 'imap_folder':
375
				$params = [
376
					'name' => $name,
377
					'label' => 'LBL_SEND_FOLDER',
378
					'uitype' => 1,
379
					'typeofdata' => 'V~M',
380
					'maximumlength' => '255',
381
					'purifyType' => \App\Purifier::TEXT,
382
					'blockLabel' => 'LBL_SAVE_SENT_MESSAGE'
383
				];
384
				break;
385
			default:
386
				break;
387
		}
388
389
		return $params ? \Vtiger_Field_Model::init($this->getName(true), $params, $name) : null;
390
	}
391
392
	public function dependency(string $field = '')
393
	{
394
		$dependency = [
395
			//hide if only one condition is valid
396
			'mail_account' => ['condition' => ['mailer_type' => ['value' => 'yfsmtp', 'operator' => 'n']], 'default' => 0],
397
			'host' => ['condition' => ['mailer_type' => ['value' => 'yfsmtp', 'operator' => 'e']], 'default' => ''],
398
			'port' => ['condition' => ['mailer_type' => ['value' => 'yfsmtp', 'operator' => 'e']], 'default' => 0],
399
			'authentication' => ['condition' => ['mailer_type' => ['value' => 'yfsmtp', 'operator' => 'e']], 'default' => 1],
400
			'username' => ['condition' => ['mailer_type' => ['value' => 'yfsmtp', 'operator' => 'e']], 'default' => ''],
401
			'password' => ['condition' => ['mailer_type' => ['value' => 'yfsmtp', 'operator' => 'e']], 'default' => ''],
402
			'secure' => ['condition' => ['mailer_type' => ['value' => 'yfsmtp', 'operator' => 'e']], 'default' => ''],
403
			'imap_host' => ['condition' => ['mailer_type' => ['value' => 'yfsmtp', 'operator' => 'e'], 'save_send_mail' => ['value' => 1, 'operator' => 'n']], 'default' => 0],
404
			'imap_port' => ['condition' => ['mailer_type' => ['value' => 'yfsmtp', 'operator' => 'e'], 'save_send_mail' => ['value' => 1, 'operator' => 'n']], 'default' => ''],
405
			'imap_username' => ['condition' => ['mailer_type' => ['value' => 'yfsmtp', 'operator' => 'e'], 'save_send_mail' => ['value' => 1, 'operator' => 'n']], 'default' => ''],
406
			'imap_password' => ['condition' => ['mailer_type' => ['value' => 'yfsmtp', 'operator' => 'e'], 'save_send_mail' => ['value' => 1, 'operator' => 'n']], 'default' => ''],
407
			'imap_validate_cert' => ['condition' => ['mailer_type' => ['value' => 'yfsmtp', 'operator' => 'e'], 'save_send_mail' => ['value' => 1, 'operator' => 'n']], 'default' => 0],
408
			'imap_folder' => ['condition' => ['save_send_mail' => ['value' => 1, 'operator' => 'n']], 'default' => '']
409
		];
410
411
		return $field ? ($dependency[$field] ?? '') : $dependency;
412
	}
413
414
	/**
415
	 * Get structure fields.
416
	 *
417
	 * @param Settings_MailSmtp_Record_Model|null $recordModel
418
	 *
419
	 * @return array
420
	 */
421
	public function getEditViewStructure($recordModel = null): array
422
	{
423
		$structure = [];
424
		foreach ($this->editFields as $fieldName) {
425
			$fieldModel = $this->getFieldInstanceByName($fieldName);
426
			if ($recordModel && $recordModel->has($fieldName)) {
427
				$fieldModel->set('fieldvalue', $recordModel->get($fieldName));
428
			} else {
429
				$defaultValue = $fieldModel->get('defaultvalue') ?? '';
430
				$fieldModel->set('fieldvalue', $defaultValue);
431
				if ($recordModel) {
432
					$recordModel->set($fieldName, $defaultValue);
433
				}
434
			}
435
			$block = $fieldModel->get('blockLabel') ?: '';
436
			$structure[$block][$fieldName] = $fieldModel;
437
		}
438
439
		return $structure;
440
	}
441
}
442