Passed
Pull Request — developer (#16705)
by Arkadiusz
28:35 queued 12:08
created

entityAfterSave()   B

Complexity

Conditions 11
Paths 10

Size

Total Lines 21
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 17
c 1
b 0
f 0
dl 0
loc 21
rs 7.3166
cc 11
nc 10
nop 1

How to fix   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
 *  Update map coordinates handler file.
5
 *
6
 * @package Handler
7
 *
8
 * @copyright YetiForce S.A.
9
 * @license YetiForce Public License 5.0 (licenses/LicenseEN.txt or yetiforce.com)
10
 * @author Arkadiusz Sołek <[email protected]>
11
 * @author Mariusz Krzaczkowski <[email protected]>
12
 */
13
14
use App\Fields\MapCoordinates;
15
16
/**
17
 * Update map coordinates handler class.
18
 */
19
class Vtiger_UpdateMapCoordinates_Handler
20
{
21
	/**
22
	 * EntityAfterSave function.
23
	 *
24
	 * @param App\EventHandler $eventHandler
25
	 *
26
	 * @return void
27
	 */
28
	public function entityAfterSave(App\EventHandler $eventHandler): void
29
	{
30
		$recordModel = $eventHandler->getRecordModel();
31
		$recordId = $recordModel->getId();
32
		$isNew = $recordModel->isNew();
33
		foreach ($recordModel->getModule()->getFieldsByType(['mapCoordinates'], true) as $fieldName => $fieldModel) {
34
			if ($isNew || false !== $recordModel->getPreviousValue($fieldName)) {
35
				$value = $recordModel->get($fieldName);
36
				if (!\App\Json::isEmpty($value)) {
37
					$value = \App\Json::decode($value);
38
					$coordinate = MapCoordinates::convert($value['type'], MapCoordinates::DECIMAL, $value['value']);
39
				}
40
				if (!empty($coordinate['lat']) && !empty($coordinate['lon'])) {
41
					if ((new \App\Db\Query())->from(\OpenStreetMap_Module_Model::COORDINATES_TABLE_NAME)
42
						->where(['crmid' => $recordId, 'type' => $fieldName])->exists()) {
43
						MapCoordinates::updateMapCoordinates($recordId, $fieldName, 'update', $coordinate);
44
					} else {
45
						MapCoordinates::updateMapCoordinates($recordId, $fieldName, 'insert', $coordinate);
46
					}
47
				} elseif (!$isNew && empty($coordinate['lat']) && empty($coordinate['lon'])) {
48
					MapCoordinates::updateMapCoordinates($recordId, $fieldName, 'delete');
49
				}
50
			}
51
		}
52
	}
53
54
	/**
55
	 * EditViewChangeValue function.
56
	 *
57
	 * @param App\EventHandler $eventHandler
58
	 *
59
	 * @return array
60
	 */
61
	public function editViewChangeValue(App\EventHandler $eventHandler): array
62
	{
63
		$return = [];
64
		$recordModel = $eventHandler->getRecordModel();
65
		$coordinates = $recordModel->get('coordinates');
66
		if (!\App\Json::isEmpty($coordinates)) {
67
			$coordinates = \App\Json::decode($coordinates);
0 ignored issues
show
Unused Code introduced by
The assignment to $coordinates is dead and can be removed.
Loading history...
68
		}
69
		$coordinatesConnector = \App\Map\Coordinates::getInstance();
70
		$values = [];
71
		if (!empty($coordinatesData = $coordinatesConnector->getCoordinates(\App\Map\Coordinates::getAddressParams($recordModel, 'a')))) {
72
			$coordinatesData = reset($coordinatesData);
73
			foreach ([MapCoordinates::DECIMAL, MapCoordinates::DEGREES, MapCoordinates::CODE_PLUS] as $type) {
74
				$values[$type][] = \App\Fields\MapCoordinates::convert(MapCoordinates::DECIMAL, $type, ['lat' => $coordinatesData['lat'], 'lon' => $coordinatesData['lon']]);
75
			}
76
			if (isset($values['decimal'][0]['lat'], $values['decimal'][0]['lon'])) {
77
				$return['changeValues'][] = ['fieldName' => 'coordinates[decimal][lat]', 'value' => $values['decimal'][0]['lat']];
78
				$return['changeValues'][] = ['fieldName' => 'coordinates[decimal][lon]', 'value' => $values['decimal'][0]['lon']];
79
				$recordModel->set('coordinates[decimal][lat]', $values['decimal'][0]['lat']);
80
				$recordModel->set('coordinates[decimal][lon]', $values['decimal'][0]['lon']);
81
			}
82
			if (isset($values['degrees'][0]['lat'], $values['degrees'][0]['lon'])) {
83
				$return['changeValues'][] = ['fieldName' => 'coordinates[degrees][lat]', 'value' => $values['degrees'][0]['lat']];
84
				$return['changeValues'][] = ['fieldName' => 'coordinates[degrees][lon]', 'value' => $values['degrees'][0]['lon']];
85
				$recordModel->set('coordinates[degrees][lat]', $values['degrees'][0]['lat']);
86
				$recordModel->set('coordinates[degrees][lon]', $values['degrees'][0]['lon']);
87
			}
88
			if (isset($values['codeplus'][0])) {
89
				$return['changeValues'][] = ['fieldName' => 'coordinates[codeplus]', 'value' => $values['codeplus'][0]];
90
				$recordModel->set('coordinates[codeplus]', $values['codeplus'][0]['lat']);
91
			}
92
		}
93
		return $return;
94
	}
95
96
	/**
97
	 * Get variables for the current event.
98
	 *
99
	 * @param string $name
100
	 * @param array  $params
101
	 * @param string $moduleName
102
	 *
103
	 * @return array|null
104
	 */
105
	public function vars(string $name, array $params, string $moduleName): ?array
0 ignored issues
show
Unused Code introduced by
The parameter $params is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

105
	public function vars(string $name, /** @scrutinizer ignore-unused */ array $params, string $moduleName): ?array

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $moduleName is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

105
	public function vars(string $name, array $params, /** @scrutinizer ignore-unused */ string $moduleName): ?array

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
106
	{
107
		if (\App\EventHandler::EDIT_VIEW_CHANGE_VALUE === $name) {
108
			return ['addresslevel1a', 'addresslevel2a', 'addresslevel3a',  'addresslevel5a', 'addresslevel8a', 'buildingnumbera', 'addresslevel8a'];
109
		}
110
		return null;
111
	}
112
}
113