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

Vtiger_State_Action   A

Complexity

Total Complexity 17

Size/Duplication

Total Lines 54
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 17
eloc 29
dl 0
loc 54
ccs 0
cts 17
cp 0
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
B checkPermission() 0 11 8
B process() 0 29 9
1
<?php
2
3
/**
4
 * Record 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
14
/**
15
 * Class Vtiger_State_Action.
16
 */
17
class Vtiger_State_Action extends \App\Controller\Action
18
{
19
	/**
20
	 * Record model instance.
21
	 *
22
	 * @var Vtiger_Record_Model
23
	 */
24
	protected $record;
25
26
	/** {@inheritdoc} */
27
	public function checkPermission(App\Request $request)
28
	{
29
		if ($request->isEmpty('record', true)) {
30
			throw new \App\Exceptions\NoPermittedToRecord('ERR_NO_PERMISSIONS_FOR_THE_RECORD', 406);
31
		}
32
		$this->record = Vtiger_Record_Model::getInstanceById($request->getInteger('record'), $request->getModule());
33
		if (!(('Archived' === $request->getByType('state') && $this->record->privilegeToArchive())
34
			|| ('Trash' === $request->getByType('state') && $this->record->privilegeToMoveToTrash())
35
			|| ('Active' === $request->getByType('state') && $this->record->privilegeToActivate()))
36
		) {
37
			throw new \App\Exceptions\NoPermittedToRecord('ERR_NO_PERMISSIONS_FOR_THE_RECORD', 406);
38
		}
39
	}
40
41
	/** {@inheritdoc} */
42
	public function process(App\Request $request)
43
	{
44
		$result = [];
45
		$eventHandler = $this->record->getEventHandler();
46
		$skipHandlers = $request->getArray('skipHandlers', \App\Purifier::ALNUM, [], \App\Purifier::INTEGER);
47
		foreach ($eventHandler->getHandlers(\App\EventHandler::PRE_STATE_CHANGE) as $handler) {
48
			$handlerId = $handler['eventhandler_id'];
49
			$response = $eventHandler->triggerHandler($handler);
50
			if (!($response['result'] ?? null) && (!isset($response['hash'], $skipHandlers[$handlerId]) || $skipHandlers[$handlerId] !== $response['hash'])) {
51
				if ($result && 'confirm' === ($response['type'] ?? '')) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $result of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using ! empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
52
					break;
53
				}
54
				$result[$handlerId] = $response;
55
			}
56
		}
57
58
		if (!$result) {
0 ignored issues
show
introduced by
$result is of type array, thus it always evaluated to false.
Loading history...
59
			$this->record->changeState($request->getByType('state'));
60
			if ('List' === $request->getByType('sourceView')) {
61
				$result = ['notify' => ['type' => 'success', 'text' => \App\Language::translate('LBL_CHANGES_SAVED')]];
62
			} else {
63
				$result = ['url' => $this->record->getDetailViewUrl()];
64
			}
65
		}
66
67
		$response = new Vtiger_Response();
68
		$response->setEmitType(Vtiger_Response::$EMIT_JSON);
69
		$response->setResult($result);
70
		$response->emit();
71
	}
72
}
73