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

Base   B

Complexity

Total Complexity 48

Size/Duplication

Total Lines 265
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 48
eloc 107
c 1
b 0
f 0
dl 0
loc 265
rs 8.5599

11 Methods

Rating   Name   Duplication   Size   Complexity  
F loadData() 0 62 21
A getFieldsLabelsByModule() 0 10 4
A isAvailable() 0 3 2
B getFields() 0 26 9
A search() 0 3 1
A __construct() 0 16 4
A getName() 0 3 1
A setRequest() 0 3 1
A getFieldsModule() 0 3 1
A getParams() 0 6 2
A isActive() 0 3 2

How to fix   Complexity   

Complex Class

Complex classes like Base often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.

Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.

While breaking up the class, it is a good idea to analyze how other classes use Base, and based on these observations, apply Extract Interface, too.

1
<?php
2
/**
3
 * Base record collector file.
4
 *
5
 * @package App
6
 *
7
 * @copyright YetiForce S.A.
8
 * @license   YetiForce Public License 6.5 (licenses/LicenseEN.txt or yetiforce.com)
9
 * @author    Mariusz Krzaczkowski <[email protected]>
10
 * @author    Radosław Skrzypczak <[email protected]>
11
 */
12
13
namespace App\RecordCollectors;
14
15
/**
16
 * Base record collector class.
17
 */
18
class Base
19
{
20
	/** @var string Module name. */
21
	public $moduleName;
22
23
	/** @var string[] Allowed modules. */
24
	public $allowedModules;
25
26
	/** @var string Icon. */
27
	public $icon;
28
29
	/** @var string Label. */
30
	public $label;
31
32
	/** @var string Additional description, visible in the modal window. */
33
	public $description;
34
35
	/** @var string Search results display type. */
36
	public $displayType;
37
38
	/** @var array Configuration field list. */
39
	public $settingsFields = [];
40
41
	/** @var string Url to Documentation API */
42
	public $docUrl;
43
44
	/** @var string Record collector name. */
45
	protected $name;
46
47
	/** var array List of fields for the modal search window. */
48
	protected $fields = [];
49
50
	/** @var array Data from record collector source. */
51
	protected $data = [];
52
53
	/** @var array Response data. */
54
	protected $response = [];
55
56
	/** @var \App\Request Request instance. */
57
	protected $request;
58
59
	/** @var array Fields mapping for loading record data. */
60
	protected $modulesFieldsMap = [];
61
62
	/** @var bool Requires subscription. */
63
	protected bool $paid = true;
64
65
	/**
66
	 * Constructor.
67
	 */
68
	public function __construct()
69
	{
70
		$name = basename(str_replace('\\', '/', static::class));
71
		$this->name = $name;
72
73
		$class = '\\Config\\Components\\RecordCollectors\\' . $name;
74
		if (!class_exists($class)) {
75
			return;
76
		}
77
		$config = (new \ReflectionClass($class))->getStaticProperties();
78
		if (isset($config['allowedModules'])) {
79
			$this->allowedModules = $config['allowedModules'];
80
			unset($config['allowedModules']);
81
		}
82
		foreach ($config as $key => $value) {
83
			$this->{$key} = $value;
84
		}
85
	}
86
87
	/**
88
	 * Get record collector name.
89
	 *
90
	 * @return string
91
	 */
92
	public function getName(): string
93
	{
94
		return $this->name;
95
	}
96
97
	/**
98
	 * Set request.
99
	 *
100
	 * @param \App\Request $request
101
	 *
102
	 * @return void
103
	 */
104
	public function setRequest(\App\Request $request): void
105
	{
106
		$this->request = $request;
107
	}
108
109
	/**
110
	 * Get fields for the modal search window.
111
	 *
112
	 * @return \Vtiger_Field_Model[]
113
	 */
114
	public function getFields(): array
115
	{
116
		$fieldsModel = [];
117
		foreach ($this->fields as $fieldName => $data) {
118
			if (isset($data['picklistValuesFunction'])) {
119
				$data['picklistValues'] = $this->{$data['picklistValuesFunction']}($data);
120
			} elseif (isset($data['picklistValues']) && false !== $data['picklistModule']) {
121
				$picklistModule = $data['picklistModule'] ?? $this->moduleName;
122
				foreach ($data['picklistValues'] as $picklistKey => $value) {
123
					$data['picklistValues'][$picklistKey] = \App\Language::translate($value, $picklistModule);
124
				}
125
			}
126
			$fieldModel = \Vtiger_Field_Model::init($this->moduleName, $data, $fieldName);
127
			if (isset($this->modulesFieldsMap[$this->moduleName][$fieldName]) && $this->request->has($this->modulesFieldsMap[$this->moduleName][$fieldName])) {
128
				try {
129
					$uitypeModel = $fieldModel->getUITypeModel();
130
					$value = $this->request->getByType($this->modulesFieldsMap[$this->moduleName][$fieldName], 'Text');
131
					$uitypeModel->validate($value, true);
132
					$fieldModel->set('fieldvalue', $uitypeModel->getDBValue($value));
133
				} catch (\Throwable $th) {
134
					\App\Log::error($th->__toString(), 'RecordCollectors');
135
				}
136
			}
137
			$fieldsModel[$fieldName] = $fieldModel;
138
		}
139
		return $fieldsModel;
140
	}
141
142
	/**
143
	 * Get fields for the module name.
144
	 *
145
	 * @param string $moduleName
146
	 *
147
	 * @return string[]
148
	 */
149
	public function getFieldsModule(string $moduleName): array
150
	{
151
		return $this->modulesFieldsMap[$moduleName];
152
	}
153
154
	/**
155
	 * Check whether it is active.
156
	 *
157
	 * @return bool
158
	 */
159
	public function isActive(): bool
160
	{
161
		return \in_array($this->moduleName, $this->allowedModules) && $this->isAvailable();
162
	}
163
164
	/**
165
	 * Check if product is available.
166
	 *
167
	 * @return bool
168
	 */
169
	public function isAvailable(): bool
170
	{
171
		return !$this->paid || \App\YetiForce\Shop::check($this->getName());
172
	}
173
174
	/**
175
	 * Search data function.
176
	 *
177
	 * @return array
178
	 */
179
	public function search(): array
180
	{
181
		throw new \Api\Core\Exception('no search function');
182
	}
183
184
	/**
185
	 * Load data.
186
	 *
187
	 * @return void
188
	 */
189
	public function loadData(): void
190
	{
191
		if (empty($this->data)) {
192
			return;
193
		}
194
		if ($recordId = $this->request->getInteger('record')) {
195
			$recordModel = \Vtiger_Record_Model::getInstanceById($recordId, $this->moduleName);
196
			$this->response['recordModel'] = $recordModel;
197
			$fieldsModel = $recordModel->getModule()->getFields();
198
		} else {
199
			$fieldsModel = \Vtiger_Module_Model::getInstance($this->moduleName)->getFields();
200
		}
201
		$additional = $fieldsData = $skip = [];
202
		$rows = isset($this->data[0]) ? $this->data : [$this->data];
203
		foreach ($rows as $key => &$row) {
204
			$dataCounter[$key] = 0;
205
			if (empty($row)) {
206
				continue;
207
			}
208
			foreach ($this->formFieldsToRecordMap[$this->moduleName] as $apiKey => $fieldName) {
209
				if (empty($fieldsModel[$fieldName]) || !$fieldsModel[$fieldName]->isActiveField()) {
210
					if (isset($row[$apiKey]) && '' !== $row[$apiKey] && null !== $row[$apiKey]) {
211
						$skip[$fieldName]['data'][$key] = $row[$apiKey];
212
						if (isset($fieldsModel[$fieldName]) && empty($skip[$fieldName]['label'])) {
213
							$skip[$fieldName]['label'] = \App\Language::translate($fieldsModel[$fieldName]->getFieldLabel(), $this->moduleName);
214
						} else {
215
							$skip[$fieldName]['label'] = $fieldName;
216
						}
217
					}
218
					unset($row[$apiKey]);
219
					continue;
220
				}
221
				$value = '';
222
				if (isset($row[$apiKey])) {
223
					$value = trim($row[$apiKey]);
224
					unset($row[$apiKey]);
225
				}
226
				if ('' === $value && isset($fieldsData[$fieldName]['data'][$key])) {
227
					continue;
228
				}
229
				if ($value) {
230
					++$dataCounter[$key];
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $dataCounter seems to be defined later in this foreach loop on line 204. Are you sure it is defined here?
Loading history...
231
				}
232
				$fieldModel = $fieldsModel[$fieldName];
233
				$fieldsData[$fieldName]['label'] = \App\Language::translate($fieldModel->getFieldLabel(), $this->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

233
				$fieldsData[$fieldName]['label'] = \App\Language::translate(/** @scrutinizer ignore-deprecated */ $fieldModel->getFieldLabel(), $this->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...
234
				$fieldsData[$fieldName]['data'][$key] = [
235
					'raw' => $value,
236
					'edit' => $fieldModel->getEditViewDisplayValue($value),
237
					'display' => $fieldModel->getDisplayValue($value, false, false, false, 40),
238
				];
239
			}
240
			foreach ($row as $name => $value) {
241
				if ('' !== $value && null !== $value) {
242
					$additional[$name][$key] = $value;
243
				}
244
			}
245
		}
246
		$this->response['fields'] = $fieldsData;
247
		$this->response['skip'] = $skip;
248
		$this->response['keys'] = array_keys($rows);
249
		$this->response['additional'] = $additional;
250
		$this->response['dataCounter'] = $dataCounter;
251
	}
252
253
	/**
254
	 * Get fields labels for the module name.
255
	 *
256
	 * @param string $moduleName
257
	 *
258
	 * @return string[]
259
	 */
260
	public function getFieldsLabelsByModule(string $moduleName): array
261
	{
262
		$fieldsModels = \Vtiger_Module_Model::getInstance($moduleName)->getFields();
263
		$labels = [];
264
		foreach ($this->formFieldsToRecordMap[$moduleName] as $fieldName) {
265
			if (isset($fieldsModels[$fieldName]) && $fieldsModels[$fieldName]->isActiveField()) {
266
				$labels[$fieldName] = $fieldsModels[$fieldName]->getFullLabelTranslation();
267
			}
268
		}
269
		return $labels;
270
	}
271
272
	/**
273
	 * Get params of collector.
274
	 *
275
	 * @return array
276
	 */
277
	protected function getParams(): array
278
	{
279
		if ($params = (new \App\Db\Query())->select(['params'])->from('vtiger_links')->where(['linktype' => 'EDIT_VIEW_RECORD_COLLECTOR', 'linkurl' => static::class])->scalar()) {
280
			return \App\Json::decode($params, true);
0 ignored issues
show
Bug introduced by
true of type true is incompatible with the type integer expected by parameter $objectDecodeType of App\Json::decode(). ( Ignorable by Annotation )

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

280
			return \App\Json::decode($params, /** @scrutinizer ignore-type */ true);
Loading history...
281
		}
282
		return [];
283
	}
284
}
285