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

Settings_MailSmtp_SaveAjax_Action::updateSmtp()   D

Complexity

Conditions 12
Paths 320

Size

Total Lines 53
Code Lines 46

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 156

Importance

Changes 0
Metric Value
eloc 46
dl 0
loc 53
ccs 0
cts 44
cp 0
rs 4.6333
c 0
b 0
f 0
cc 12
nc 320
nop 1
crap 156

3 Methods

Rating   Name   Duplication   Size   Complexity  
A Settings_MailSmtp_SaveAjax_Action::save() 0 14 3
A Settings_MailSmtp_SaveAjax_Action::preSaveValidation() 0 6 1
A Settings_MailSmtp_SaveAjax_Action::getRecordModelFromRequest() 0 9 2

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 SaveAjax action 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_SaveAjax_Action extends Settings_Vtiger_Save_Action
12
{
13
	/** {@inheritdoc} */
14
	public function __construct()
15
	{
16
		parent::__construct();
17
		$this->exposeMethod('save');
18
		$this->exposeMethod('preSaveValidation');
19
	}
20
21
	/**
22
	 * Function to get the record model based on the request parameters.
23
	 *
24
	 * @param \App\Request $request
25
	 *
26
	 * @return Settings_MailSmtp_Record_Model
27
	 */
28
	protected function getRecordModelFromRequest(App\Request $request)
29
	{
30
		if ($request->isEmpty('record')) {
31
			$recordModel = Settings_MailSmtp_Record_Model::getCleanInstance();
32
		} else {
33
			$recordModel = Settings_MailSmtp_Record_Model::getInstanceById($request->getInteger('record'));
34
		}
35
		$recordModel->setDataFromRequest($request);
36
		return $recordModel;
37
	}
38
39
	/**
40
	 * PreSave validation function.
41
	 *
42
	 * @param App\Request $request
43
	 *
44
	 * @return void
45
	 */
46
	public function preSaveValidation(App\Request $request)
47
	{
48
		$recordModel = $this->getRecordModelFromRequest($request);
49
		$response = new Vtiger_Response();
50
		$response->setResult($recordModel->validate());
51
		$response->emit();
52
	}
53
54
	/**
55
	 * Save function.
56
	 *
57
	 * @param App\Request $request
58
	 *
59
	 * @return void
60
	 */
61
	public function save(App\Request $request)
62
	{
63
		try {
64
			$recordModel = $this->getRecordModelFromRequest($request);
65
			$recordId = $recordModel->getId();
66
			$recordModel->save();
67
			\Settings_Vtiger_Tracker_Model::addDetail($recordModel->getPreviousValue(), $recordId ? array_intersect_key($recordModel->getData(), $recordModel->getPreviousValue()) : $recordModel->getData());
68
			$result = ['success' => true, 'url' => $recordModel->getModule()->getDefaultUrl()];
69
		} catch (\App\Exceptions\AppException $e) {
70
			$result = ['success' => false, 'message' => $e->getDisplayMessage()];
71
		}
72
		$response = new Vtiger_Response();
73
		$response->setResult($result);
74
		$response->emit();
75
	}
76
}
77