Passed
Push — developer ( b17be7...a57010 )
by Mariusz
19:04
created

Gus::search()   D

Complexity

Conditions 20
Paths 75

Size

Total Lines 76
Code Lines 58

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 58
dl 0
loc 76
rs 4.1666
c 0
b 0
f 0
cc 20
nc 75
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 5.0 (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
	protected $fields = [
44
		'vatId' => [
45
			'labelModule' => '_Base',
46
			'label' => 'Vat ID',
47
		],
48
		'ncr' => [
49
			'labelModule' => '_Base',
50
			'label' => 'Registration number 1',
51
		],
52
		'taxNumber' => [
53
			'labelModule' => '_Base',
54
			'label' => 'Registration number 2',
55
		],
56
	];
57
58
	/** {@inheritdoc} */
59
	protected $modulesFieldsMap = [
60
		'Accounts' => [
61
			'vatId' => 'vat_id',
62
			'taxNumber' => 'registration_number_2',
63
			'ncr' => 'registration_number_1',
64
		],
65
		'Leads' => [
66
			'vatId' => 'vat_id',
67
			'taxNumber' => 'registration_number_2',
68
			'ncr' => 'registration_number_1',
69
		],
70
		'Vendors' => [
71
			'vatId' => 'vat_id',
72
			'taxNumber' => 'registration_number_2',
73
			'ncr' => 'registration_number_1',
74
		],
75
		'Competition' => [
76
			'vatId' => 'vat_id',
77
			'taxNumber' => 'registration_number_2',
78
			'ncr' => 'registration_number_1',
79
		],
80
		'Partners' => [
81
			'vatId' => 'vat_id',
82
		],
83
	];
84
85
	/** {@inheritdoc} */
86
	public $formFieldsToRecordMap = [
87
		'Accounts' => [
88
			'Nazwa' => 'accountname',
89
			'Regon' => 'registration_number_2',
90
			'Krs' => 'registration_number_1',
91
			'Nip' => 'vat_id',
92
			'NumerBudynku' => 'buildingnumbera',
93
			'NumerLokalu' => 'localnumbera',
94
			'Ulica' => 'addresslevel8a',
95
			'KodPocztowy' => 'addresslevel7a',
96
			'Miejscowosc' => 'addresslevel5a',
97
			'Gmina' => 'addresslevel4a',
98
			'Powiat' => 'addresslevel3a',
99
			'Wojewodztwo' => 'addresslevel2a',
100
			'Kraj' => 'addresslevel1a',
101
			'NumerTelefonu' => 'phone',
102
			'NumerFaksu' => 'fax',
103
			'AdresEmail' => 'email1',
104
			'AdresStronyInternetowej' => 'website',
105
		],
106
		'Leads' => [
107
			'Nazwa' => 'company',
108
			'Nip' => 'vat_id',
109
			'Regon' => 'registration_number_2',
110
			'NumerBudynku' => 'buildingnumbera',
111
			'NumerLokalu' => 'localnumbera',
112
			'Ulica' => 'addresslevel8a',
113
			'KodPocztowy' => 'addresslevel7a',
114
			'Miejscowosc' => 'addresslevel5a',
115
			'Gmina' => 'addresslevel4a',
116
			'Powiat' => 'addresslevel3a',
117
			'Wojewodztwo' => 'addresslevel2a',
118
			'Kraj' => 'addresslevel1a',
119
			'NumerBudynku' => 'buildingnumbera',
120
			'NumerTelefonu' => 'phone',
121
			'NumerFaksu' => 'fax',
122
			'AdresEmail' => 'email',
123
			'AdresStronyInternetowej' => 'website',
124
		],
125
		'Partners' => [
126
			'Nazwa' => 'subject',
127
			'Nip' => 'vat_id',
128
			'NumerBudynku' => 'buildingnumbera',
129
			'NumerLokalu' => 'localnumbera',
130
			'Ulica' => 'addresslevel8a',
131
			'KodPocztowy' => 'addresslevel7a',
132
			'Miejscowosc' => 'addresslevel5a',
133
			'Gmina' => 'addresslevel4a',
134
			'Powiat' => 'addresslevel3a',
135
			'Wojewodztwo' => 'addresslevel2a',
136
			'Kraj' => 'addresslevel1a',
137
		],
138
		'Vendors' => [
139
			'Nazwa' => 'vendorname',
140
			'Nip' => 'vat_id',
141
			'Regon' => 'registration_number_2',
142
			'NumerBudynku' => 'buildingnumbera',
143
			'NumerLokalu' => 'localnumbera',
144
			'Ulica' => 'addresslevel8a',
145
			'KodPocztowy' => 'addresslevel7a',
146
			'Miejscowosc' => 'addresslevel5a',
147
			'Gmina' => 'addresslevel4a',
148
			'Powiat' => 'addresslevel3a',
149
			'Wojewodztwo' => 'addresslevel2a',
150
			'Kraj' => 'addresslevel1a',
151
		],
152
		'Competition' => [
153
			'Nazwa' => 'subject',
154
			'Nip' => 'vat_id',
155
			'NumerBudynku' => 'buildingnumbera',
156
			'NumerLokalu' => 'localnumbera',
157
			'Ulica' => 'addresslevel8a',
158
			'KodPocztowy' => 'addresslevel7a',
159
			'Miejscowosc' => 'addresslevel5a',
160
			'Gmina' => 'addresslevel4a',
161
			'Powiat' => 'addresslevel3a',
162
			'Wojewodztwo' => 'addresslevel2a',
163
			'Kraj' => 'addresslevel1a',
164
		],
165
	];
166
167
	/** {@inheritdoc} */
168
	public function search(): array
169
	{
170
		if (!$this->isActive()) {
171
			return [];
172
		}
173
		$vatId = preg_replace('/[^0-9]/', '', $this->request->getByType('vatId', 'Text'));
174
		$taxNumber = str_replace([' ', ',', '.', '-'], '', $this->request->getByType('taxNumber', 'Text'));
175
		$ncr = str_replace([' ', ',', '.', '-'], '', $this->request->getByType('ncr', 'Text'));
176
		$response = [];
177
		$moduleName = $this->request->getModule();
178
		$client = \App\RecordCollectors\Helper\GusClient::getInstance($this->getClientParams($moduleName));
179
		try {
180
			$infoFromGus = $client->search($vatId, $ncr, $taxNumber);
181
			$response['recordModel'] = $this->getRecordModel();
182
			$fieldsModel = $response['recordModel']->getModule()->getFields();
183
			if ($infoFromGus && isset($this->formFieldsToRecordMap[$moduleName])) {
184
				$additional = $fieldsData = $skip = $dataCounter = [];
185
				foreach ($infoFromGus as $key => &$row) {
186
					$dataCounter[$key] = 0;
187
					if (empty($row)) {
188
						continue;
189
					}
190
					foreach ($this->formFieldsToRecordMap[$moduleName] as $apiKey => $fieldName) {
191
						if (empty($fieldsModel[$fieldName]) || !$fieldsModel[$fieldName]->isActiveField()) {
192
							if (isset($row[$apiKey]) && '' !== $row[$apiKey]) {
193
								$skip[$fieldName]['data'][$key] = $row[$apiKey];
194
								if (isset($fieldsModel[$fieldName]) && empty($skip[$fieldName]['label'])) {
195
									$skip[$fieldName]['label'] = \App\Language::translate($fieldsModel[$fieldName]->getFieldLabel(), $moduleName);
196
								} else {
197
									$skip[$fieldName]['label'] = $fieldName;
198
								}
199
							}
200
							unset($row[$apiKey]);
201
							continue;
202
						}
203
						$value = '';
204
						if (isset($row[$apiKey])) {
205
							$value = trim($row[$apiKey]);
206
							unset($row[$apiKey]);
207
						}
208
						$fieldModel = $fieldsModel[$fieldName];
209
						if ($value) {
210
							++$dataCounter[$key];
211
							if ('phone' === $fieldModel->getFieldDataType()) {
212
								$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

212
								$details = $fieldModel->getUITypeModel()->/** @scrutinizer ignore-call */ getPhoneDetails($value, 'PL');
Loading history...
213
								$value = $details['number'];
214
								if ($fieldName !== $details['fieldName']) {
215
									$fieldName = $details['fieldName'];
216
									$fieldModel = $fieldsModel[$fieldName];
217
								}
218
							}
219
						}
220
						$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

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