Passed
Push — developer ( 5255d7...186ac1 )
by Radosław
29:50 queued 13:55
created

Vtiger_Delete_Action::checkPermission()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 5
c 1
b 0
f 0
dl 0
loc 8
ccs 0
cts 6
cp 0
rs 10
cc 3
nc 3
nop 1
crap 12
1
<?php
2
/**
3
 * Delete record action file.
4
 *
5
 * @package Action
6
 *
7
 * @copyright YetiForce S.A.
8
 * @license   YetiForce Public License 5.0 (licenses/LicenseEN.txt or yetiforce.com)
9
 */
10
11
/**
12
 * Delete record action class.
13
 */
14
class Vtiger_Delete_Action extends \App\Controller\Action
15
{
16
	/**
17
	 * Record model instance.
18
	 *
19
	 * @var Vtiger_Record_Model
20
	 */
21
	protected $record;
22
23
	/** {@inheritdoc} */
24
	public function checkPermission(App\Request $request)
25
	{
26
		if ($request->isEmpty('record', true)) {
27
			throw new \App\Exceptions\NoPermittedToRecord('ERR_NO_PERMISSIONS_FOR_THE_RECORD', 406);
28
		}
29
		$this->record = Vtiger_Record_Model::getInstanceById($request->getInteger('record'), $request->getModule());
30
		if (!$this->record->privilegeToDelete()) {
31
			throw new \App\Exceptions\NoPermittedToRecord('ERR_NO_PERMISSIONS_FOR_THE_RECORD', 406);
32
		}
33
	}
34
35
	/** {@inheritdoc} */
36
	public function process(App\Request $request)
37
	{
38
		$result = [];
39
		$eventHandler = $this->record->getEventHandler();
40
		$skipHandlers = $request->getArray('skipHandlers', \App\Purifier::ALNUM, [], \App\Purifier::INTEGER);
41
		foreach ($eventHandler->getHandlers(\App\EventHandler::PRE_DELETE) as $handler) {
42
			$handlerId = $handler['eventhandler_id'];
43
			$response = $eventHandler->triggerHandler($handler);
44
			if (!($response['result'] ?? null) && (!isset($response['hash'], $skipHandlers[$handlerId]) || $skipHandlers[$handlerId] !== $response['hash'])) {
45
				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...
46
					break;
47
				}
48
				$result[$handlerId] = $response;
49
			}
50
		}
51
52
		if (!$result) {
0 ignored issues
show
introduced by
$result is of type array, thus it always evaluated to false.
Loading history...
53
			$this->record->delete();
54
			if ('List' === $request->getByType('sourceView')) {
55
				$result = ['notify' => ['type' => 'success', 'text' => \App\Language::translate('LBL_RECORD_HAS_BEEN_DELETED')]];
56
			} else {
57
				$result = ['url' => $this->record->getModule()->getListViewUrl()];
58
			}
59
		}
60
61
		$response = new Vtiger_Response();
62
		$response->setEmitType(Vtiger_Response::$EMIT_JSON);
63
		$response->setResult($result);
64
		$response->emit();
65
	}
66
}
67