Gus::search()   F
last analyzed

Complexity

Conditions 21
Paths 149

Size

Total Lines 81
Code Lines 62

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 62
dl 0
loc 81
rs 3.7583
c 0
b 0
f 0
cc 21
nc 149
nop 0

How to fix   Long Method    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
 * Gus class for downloading REGON registry open public data. For BIR 1.1 version.
4
 *
5
 * The file is part of the paid functionality. Using the file is allowed only after purchasing a subscription. File modification allowed only with the consent of the system producer.
6
 *
7
 * @see https://api.stat.gov.pl/Home/RegonApi
8
 *
9
 * @package App
10
 *
11
 * @copyright YetiForce S.A.
12
 * @license   YetiForce Public License 6.5 (licenses/LicenseEN.txt or yetiforce.com)
13
 * @author    Adrian Kon <[email protected]>
14
 * @author    Mariusz Krzaczkowski <[email protected]>
15
 */
16
17
namespace App\RecordCollectors;
18
19
/**
20
 * Gus record collector class.
21
 */
22
class Gus extends Base
23
{
24
	/** {@inheritdoc} */
25
	public $allowedModules = ['Accounts', 'Leads', 'Vendors', 'Competition', 'Partners'];
26
27
	/** {@inheritdoc} */
28
	public $icon = 'yfi-gus-regon-pl';
29
30
	/** {@inheritdoc} */
31
	public $label = 'LBL_PL_GUS';
32
33
	/** {@inheritdoc} */
34
	public $description = 'LBL_PL_GUS_DESC';
35
36
	/** {@inheritdoc} */
37
	public $displayType = 'FillFields';
38
39
	/** {@inheritdoc} */
40
	public $docUrl = 'https://api.stat.gov.pl/Home/RegonApi';
41
42
	/** {@inheritdoc} */
43
	public $formFieldsToRecordMap = [
44
		'Accounts' => [
45
			'Nazwa' => 'accountname',
46
			'Regon' => 'registration_number_2',
47
			'Krs' => 'registration_number_1',
48
			'Nip' => 'vat_id',
49
			'NumerBudynku' => 'buildingnumbera',
50
			'NumerLokalu' => 'localnumbera',
51
			'Ulica' => 'addresslevel8a',
52
			'KodPocztowy' => 'addresslevel7a',
53
			'Miejscowosc' => 'addresslevel5a',
54
			'Gmina' => 'addresslevel4a',
55
			'Powiat' => 'addresslevel3a',
56
			'Wojewodztwo' => 'addresslevel2a',
57
			'Kraj' => 'addresslevel1a',
58
			'NumerTelefonu' => 'phone',
59
			'NumerFaksu' => 'fax',
60
			'AdresEmail' => 'email1',
61
			'AdresStronyInternetowej' => 'website',
62
		],
63
		'Leads' => [
64
			'Nazwa' => 'company',
65
			'Nip' => 'vat_id',
66
			'Regon' => 'registration_number_2',
67
			'NumerBudynku' => 'buildingnumbera',
68
			'NumerLokalu' => 'localnumbera',
69
			'Ulica' => 'addresslevel8a',
70
			'KodPocztowy' => 'addresslevel7a',
71
			'Miejscowosc' => 'addresslevel5a',
72
			'Gmina' => 'addresslevel4a',
73
			'Powiat' => 'addresslevel3a',
74
			'Wojewodztwo' => 'addresslevel2a',
75
			'Kraj' => 'addresslevel1a',
76
			'NumerBudynku' => 'buildingnumbera',
77
			'NumerTelefonu' => 'phone',
78
			'NumerFaksu' => 'fax',
79
			'AdresEmail' => 'email',
80
			'AdresStronyInternetowej' => 'website',
81
		],
82
		'Partners' => [
83
			'Nazwa' => 'subject',
84
			'Nip' => 'vat_id',
85
			'NumerBudynku' => 'buildingnumbera',
86
			'NumerLokalu' => 'localnumbera',
87
			'Ulica' => 'addresslevel8a',
88
			'KodPocztowy' => 'addresslevel7a',
89
			'Miejscowosc' => 'addresslevel5a',
90
			'Gmina' => 'addresslevel4a',
91
			'Powiat' => 'addresslevel3a',
92
			'Wojewodztwo' => 'addresslevel2a',
93
			'Kraj' => 'addresslevel1a',
94
		],
95
		'Vendors' => [
96
			'Nazwa' => 'vendorname',
97
			'Nip' => 'vat_id',
98
			'Regon' => 'registration_number_2',
99
			'NumerBudynku' => 'buildingnumbera',
100
			'NumerLokalu' => 'localnumbera',
101
			'Ulica' => 'addresslevel8a',
102
			'KodPocztowy' => 'addresslevel7a',
103
			'Miejscowosc' => 'addresslevel5a',
104
			'Gmina' => 'addresslevel4a',
105
			'Powiat' => 'addresslevel3a',
106
			'Wojewodztwo' => 'addresslevel2a',
107
			'Kraj' => 'addresslevel1a',
108
		],
109
		'Competition' => [
110
			'Nazwa' => 'subject',
111
			'Nip' => 'vat_id',
112
			'NumerBudynku' => 'buildingnumbera',
113
			'NumerLokalu' => 'localnumbera',
114
			'Ulica' => 'addresslevel8a',
115
			'KodPocztowy' => 'addresslevel7a',
116
			'Miejscowosc' => 'addresslevel5a',
117
			'Gmina' => 'addresslevel4a',
118
			'Powiat' => 'addresslevel3a',
119
			'Wojewodztwo' => 'addresslevel2a',
120
			'Kraj' => 'addresslevel1a',
121
		],
122
	];
123
124
	/** {@inheritdoc} */
125
	protected $fields = [
126
		'vatId' => [
127
			'labelModule' => '_Base',
128
			'label' => 'Vat ID',
129
		],
130
		'ncr' => [
131
			'labelModule' => '_Base',
132
			'label' => 'Registration number 1',
133
		],
134
		'taxNumber' => [
135
			'labelModule' => '_Base',
136
			'label' => 'Registration number 2',
137
		],
138
	];
139
140
	/** {@inheritdoc} */
141
	protected string $addOnName = 'YetiForcePlGus';
142
143
	/** {@inheritdoc} */
144
	protected $modulesFieldsMap = [
145
		'Accounts' => [
146
			'vatId' => 'vat_id',
147
			'taxNumber' => 'registration_number_2',
148
			'ncr' => 'registration_number_1',
149
		],
150
		'Leads' => [
151
			'vatId' => 'vat_id',
152
			'taxNumber' => 'registration_number_2',
153
			'ncr' => 'registration_number_1',
154
		],
155
		'Vendors' => [
156
			'vatId' => 'vat_id',
157
			'taxNumber' => 'registration_number_2',
158
			'ncr' => 'registration_number_1',
159
		],
160
		'Competition' => [
161
			'vatId' => 'vat_id',
162
			'taxNumber' => 'registration_number_2',
163
			'ncr' => 'registration_number_1',
164
		],
165
		'Partners' => [
166
			'vatId' => 'vat_id',
167
		],
168
	];
169
170
	/** {@inheritdoc} */
171
	public function search(): array
172
	{
173
		if (!$this->isActive()) {
174
			return [];
175
		}
176
		$vatId = preg_replace('/[^0-9]/', '', $this->request->getByType('vatId', 'Text'));
177
		$taxNumber = str_replace([' ', ',', '.', '-'], '', $this->request->getByType('taxNumber', 'Text'));
178
		$ncr = str_replace([' ', ',', '.', '-'], '', $this->request->getByType('ncr', 'Text'));
179
		$response = [];
180
		$moduleName = $this->request->getModule();
181
		$client = \App\RecordCollectors\Helper\GusClient::getInstance($this->getClientParams($moduleName));
182
		try {
183
			$infoFromGus = $client->search($vatId, $ncr, $taxNumber);
184
			if ($recordId = $this->request->getInteger('record')) {
185
				$recordModel = \Vtiger_Record_Model::getInstanceById($recordId, $moduleName);
186
				$response['recordModel'] = $recordModel;
187
				$fieldsModel = $recordModel->getModule()->getFields();
188
			} else {
189
				$fieldsModel = \Vtiger_Module_Model::getInstance($moduleName)->getFields();
190
			}
191
			if ($infoFromGus && isset($this->formFieldsToRecordMap[$moduleName])) {
192
				$additional = $fieldsData = $skip = $dataCounter = [];
193
				foreach ($infoFromGus as $key => &$row) {
194
					$dataCounter[$key] = 0;
195
					if (empty($row)) {
196
						continue;
197
					}
198
					foreach ($this->formFieldsToRecordMap[$moduleName] as $apiKey => $fieldName) {
199
						if (empty($fieldsModel[$fieldName]) || !$fieldsModel[$fieldName]->isActiveField()) {
200
							if (isset($row[$apiKey]) && '' !== $row[$apiKey]) {
201
								$skip[$fieldName]['data'][$key] = $row[$apiKey];
202
								if (isset($fieldsModel[$fieldName]) && empty($skip[$fieldName]['label'])) {
203
									$skip[$fieldName]['label'] = \App\Language::translate($fieldsModel[$fieldName]->getFieldLabel(), $moduleName);
204
								} else {
205
									$skip[$fieldName]['label'] = $fieldName;
206
								}
207
							}
208
							unset($row[$apiKey]);
209
							continue;
210
						}
211
						$value = '';
212
						if (isset($row[$apiKey])) {
213
							$value = trim($row[$apiKey]);
214
							unset($row[$apiKey]);
215
						}
216
						$fieldModel = $fieldsModel[$fieldName];
217
						if ($value) {
218
							++$dataCounter[$key];
219
							if ('phone' === $fieldModel->getFieldDataType()) {
220
								$details = $fieldModel->getUITypeModel()->getPhoneDetails($value, 'PL');
0 ignored issues
show
Bug introduced by
The method getPhoneDetails() does not exist on Vtiger_Base_UIType. It seems like you code against a sub-type of Vtiger_Base_UIType such as Vtiger_Phone_UIType. ( Ignorable by Annotation )

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

220
								$details = $fieldModel->getUITypeModel()->/** @scrutinizer ignore-call */ getPhoneDetails($value, 'PL');
Loading history...
221
								$value = $details['number'];
222
								if ($fieldName !== $details['fieldName']) {
223
									$fieldName = $details['fieldName'];
224
									$fieldModel = $fieldsModel[$fieldName];
225
								}
226
							}
227
						}
228
						$fieldsData[$fieldName]['label'] = \App\Language::translate($fieldModel->getFieldLabel(), $moduleName);
0 ignored issues
show
Deprecated Code introduced by
The function Vtiger_Field_Model::getFieldLabel() has been deprecated: 7.0 Use $this->getLabel() ( Ignorable by Annotation )

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

228
						$fieldsData[$fieldName]['label'] = \App\Language::translate(/** @scrutinizer ignore-deprecated */ $fieldModel->getFieldLabel(), $moduleName);

This function has been deprecated. The supplier of the function has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.

Loading history...
229
						$fieldsData[$fieldName]['data'][$key] = [
230
							'raw' => $value,
231
							'edit' => $fieldModel->getEditViewDisplayValue($value),
232
							'display' => $fieldModel->getDisplayValue($value),
233
						];
234
					}
235
					foreach ($row as $name => $value) {
236
						if ('' !== $value) {
237
							$additional[$name][$key] = $value;
238
						}
239
					}
240
				}
241
				$response['fields'] = $fieldsData;
242
				$response['additional'] = $additional;
243
				$response['keys'] = array_keys($infoFromGus);
244
				$response['skip'] = $skip;
245
				$response['dataCounter'] = $dataCounter;
246
			}
247
		} catch (\SoapFault $e) {
248
			\App\Log::warning($e->faultstring, 'RecordCollectors');
249
			$response['error'] = $e->faultstring;
250
		}
251
		return $response;
252
	}
253
254
	/**
255
	 * Get params.
256
	 *
257
	 * @param string $moduleName
258
	 *
259
	 * @return string[]
260
	 */
261
	public function getClientParams(string $moduleName): array
262
	{
263
		$params = [];
264
		if (isset($this->formFieldsToRecordMap[$moduleName]['PKDPodstawowyKod']) || isset($this->formFieldsToRecordMap[$moduleName]['PKDPozostaleNazwy']) || isset($this->formFieldsToRecordMap[$moduleName]['PKDPozostaleKodyNazwy'])) {
265
			$params[] = 'pkd';
266
		}
267
		return $params;
268
	}
269
}
270