Passed
Push — developer ( 4a3269...9055ae )
by Radosław
15:12
created

Vtiger_MassState_Action::process()   C

Complexity

Conditions 12
Paths 32

Size

Total Lines 54
Code Lines 41

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 156

Importance

Changes 0
Metric Value
eloc 41
dl 0
loc 54
ccs 0
cts 34
cp 0
rs 6.9666
c 0
b 0
f 0
cc 12
nc 32
nop 1
crap 156

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
 * Mass records state action class.
5
 *
6
 * @package Action
7
 *
8
 * @copyright YetiForce S.A.
9
 * @license   YetiForce Public License 5.0 (licenses/LicenseEN.txt or yetiforce.com)
10
 * @author    Mariusz Krzaczkowski <[email protected]>
11
 * @author    Radosław Skrzypczak <[email protected]>
12
 */
13
class Vtiger_MassState_Action extends Vtiger_Mass_Action
14
{
15
	/**
16
	 * Function to check permission.
17
	 *
18
	 * @param \App\Request $request
19
	 *
20
	 * @throws \App\Exceptions\NoPermitted
21
	 */
22
	public function checkPermission(App\Request $request)
23
	{
24
		$userPriviligesModel = Users_Privileges_Model::getCurrentUserPrivilegesModel();
25
		switch ($request->getByType('state')) {
26
			case 'Archived':
27
				if ($userPriviligesModel->hasModuleActionPermission($request->getModule(), 'MassArchived')) {
28
					return true;
29
				}
30
				break;
31
			case 'Trash':
32
				if ($userPriviligesModel->hasModuleActionPermission($request->getModule(), 'MassTrash')) {
33
					return true;
34
				}
35
				break;
36
			case 'Active':
37
				if ($userPriviligesModel->hasModuleActionPermission($request->getModule(), 'MassActive')) {
38
					return true;
39
				}
40
				break;
41
			default:
42
				break;
43
		}
44
		throw new \App\Exceptions\NoPermitted('LBL_PERMISSION_DENIED', 406);
45
	}
46
47
	/**
48
	 * Process.
49
	 *
50
	 * @param \App\Request $request
51
	 *
52
	 * @throws \App\Exceptions\AppException
53
	 */
54
	public function process(App\Request $request)
55
	{
56
		$moduleName = $request->getModule();
57
		$recordIds = self::getRecordsListFromRequest($request);
58
		$skipped = [];
59
		foreach ($recordIds as $recordId) {
60
			$recordModel = Vtiger_Record_Model::getInstanceById($recordId, $moduleName);
61
			switch ($request->getByType('state')) {
62
				case 'Archived':
63
					if (!$recordModel->privilegeToArchive()) {
64
						$skipped[] = $recordModel->getName();
65
						continue 2;
66
					}
67
					break;
68
				case 'Trash':
69
					if (!$recordModel->privilegeToMoveToTrash()) {
70
						$skipped[] = $recordModel->getName();
71
						continue 2;
72
					}
73
					break;
74
				case 'Active':
75
					if (!$recordModel->privilegeToActivate()) {
76
						$skipped[] = $recordModel->getName();
77
						continue 2;
78
					}
79
					break;
80
				default:
81
					break;
82
			}
83
84
			$eventHandler = $recordModel->getEventHandler();
85
			foreach ($eventHandler->getHandlers(\App\EventHandler::PRE_STATE_CHANGE) as $handler) {
86
				if (!($eventHandler->triggerHandler($handler)['result'] ?? null)) {
87
					$skipped[] = $recordModel->getName();
88
					continue 2;
89
				}
90
			}
91
92
			$recordModel->changeState($request->getByType('state'));
93
			unset($recordModel);
94
		}
95
		$text = \App\Language::translate('LBL_CHANGES_SAVED');
96
		$type = 'success';
97
		if ($skipped) {
98
			$type = 'info';
99
			$break = '<br>';
100
			$text .= $break . \App\Language::translate('LBL_OMITTED_RECORDS');
101
			foreach ($skipped as $name) {
102
				$text .= $break . $name;
103
			}
104
		}
105
		$response = new Vtiger_Response();
106
		$response->setResult(['notify' => ['text' => $text, 'type' => $type]]);
107
		$response->emit();
108
	}
109
}
110