Passed
Push — developer ( d5cf61...0a1e92 )
by Radosław
18:21
created

Vtiger_Delete_Action::performDelete()   B

Complexity

Conditions 9
Paths 12

Size

Total Lines 24
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 90

Importance

Changes 0
Metric Value
eloc 17
dl 0
loc 24
ccs 0
cts 0
cp 0
rs 8.0555
c 0
b 0
f 0
cc 9
nc 12
nop 1
crap 90
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 = $this->performDelete($request);
39
40
		$response = new Vtiger_Response();
41
		$response->setEmitType(Vtiger_Response::$EMIT_JSON);
42
		$response->setResult($result);
43
		$response->emit();
44
	}
45
46
	/**
47
	 * Perform delete action.
48
	 *
49
	 * @param App\Request $request
50
	 *
51
	 * @return array
52
	 */
53
	protected function performDelete(App\Request $request): array
54
	{
55
		$result = [];
56
		$skipHandlers = $request->getArray('skipHandlers', \App\Purifier::ALNUM, [], \App\Purifier::INTEGER);
57
		$eventHandler = $this->record->getEventHandler();
58
		foreach ($eventHandler->getHandlers(\App\EventHandler::PRE_DELETE) as $handler) {
59
			$handlerId = $handler['eventhandler_id'];
60
			$response = $eventHandler->triggerHandler($handler);
61
			if (!($response['result'] ?? null) && (!isset($response['hash'], $skipHandlers[$handlerId]) || $skipHandlers[$handlerId] !== $response['hash'])) {
62
				$result[$handlerId] = $response;
63
				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...
64
					break;
65
				}
66
			}
67
		}
68
		if (!$result) {
69
			$this->record->delete();
70
			if ('List' === $request->getByType('sourceView')) {
71
				$result = ['notify' => ['type' => 'success', 'text' => \App\Language::translate('LBL_RECORD_HAS_BEEN_DELETED')]];
72
			} else {
73
				$result = ['url' => $this->record->getModule()->getListViewUrl()];
74
			}
75
		}
76
		return $result;
77
	}
78
}
79