Passed
Push — developer ( 6b5868...bed0f9 )
by Radosław
22:42 queued 03:39
created

Contacts_DuplicateEmail_Handler::editViewPreSave()   B

Complexity

Conditions 9
Paths 15

Size

Total Lines 29
Code Lines 20

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 20
c 1
b 0
f 0
dl 0
loc 29
rs 8.0555
cc 9
nc 15
nop 1
1
<?php
2
/**
3
 * Duplicate e-mail handler.
4
 *
5
 * @package Handler
6
 *
7
 * @copyright YetiForce S.A.
8
 * @license YetiForce Public License 6.5 (licenses/LicenseEN.txt or yetiforce.com)
9
 * @author Radosław Skrzypczak <[email protected]>
10
 */
11
/**
12
 * Contacts_DuplicateEmail_Handler class.
13
 */
14
class Contacts_DuplicateEmail_Handler
15
{
16
	/**
17
	 * EditViewPreSave handler function.
18
	 *
19
	 * @param App\EventHandler $eventHandler
20
	 *
21
	 * @return array Example: ['result' => false, 'message' => 'LBL_MESSAGE']
22
	 */
23
	public function editViewPreSave(App\EventHandler $eventHandler)
24
	{
25
		$recordModel = $eventHandler->getRecordModel();
26
		$response = ['result' => true];
27
		$values = [];
28
		$fields = $recordModel->getModule()->getFieldsByType('email', true);
29
		foreach ($fields as $fieldModel) {
30
			if (($value = $recordModel->get($fieldModel->getName())) && $fieldModel->isViewable()) {
31
				$values[] = $value;
32
			}
33
		}
34
		if ($fields && $values) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $fields 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...
35
			$queryGenerator = new \App\QueryGenerator($recordModel->getModuleName());
36
			$queryGenerator->setStateCondition('All');
37
			$queryGenerator->setFields(['id'])->permissions = false;
38
			foreach ($fields as $fieldModel) {
39
				$queryGenerator->addCondition($fieldModel->getName(), $values, 'e', false);
40
			}
41
			if ($recordModel->getId()) {
42
				$queryGenerator->addCondition('id', $recordModel->getId(), 'n', true);
43
			}
44
			if ($queryGenerator->createQuery()->exists()) {
45
				$response = [
46
					'result' => false,
47
					'message' => App\Language::translate('LBL_DUPLICATE_EMAIL_ADDRESS', $recordModel->getModuleName())
48
				];
49
			}
50
		}
51
		return $response;
52
	}
53
}
54