Passed
Push — developer ( e0199d...a3edbd )
by Mariusz
63:10 queued 29:14
created

RelatedListView   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 57
Duplicated Lines 100 %

Coupling/Cohesion

Components 1
Dependencies 4

Importance

Changes 0
Metric Value
wmc 12
lcom 1
cbo 4
dl 57
loc 57
rs 10
c 0
b 0
f 0

1 Method

Rating   Name   Duplication   Size   Complexity  
C process() 53 53 12

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
/**
3
 * Related list view action file.
4
 *
5
 * @package Action
6
 *
7
 * @copyright YetiForce Sp. z o.o.
8
 * @license   YetiForce Public License 3.0 (licenses/LicenseEN.txt or yetiforce.com)
9
 * @author Mariusz Krzaczkowski <[email protected]>
10
 */
11
12
namespace YF\Modules\Base\Action;
13
14
/**
15
 * Related list view action class.
16
 */
17 View Code Duplication
class RelatedListView extends \App\Controller\Action
0 ignored issues
show
Duplication introduced by
This class seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
18
{
19
	/** {@inheritdoc} */
20
	public function process(): void
21
	{
22
		$relatedListModel = \YF\Modules\Base\Model\RelatedList::getInstance($this->moduleName, 'RelatedList');
23
		$relatedListModel->setRequest($this->request);
24
		$rows = $columns = $fields = [];
25
		foreach ($this->request->getArray('columns') as $key => $value) {
26
			$columns[$key] = $value['name'];
27
			if ($value['name']) {
28
				$fields[] = $value['name'];
29
			}
30
		}
31
		$order = current($this->request->getArray('order', \App\Purifier::ALNUM));
32
		if ($order && isset($columns[$order['column']], $columns[$order['column']])) {
33
			$relatedListModel->setOrder($columns[$order['column']], strtoupper($order['dir']));
34
		}
35
		if ($this->request->has('filters') && !$this->request->isEmpty('filters')) {
36
			$conditions = [];
37
			foreach ($this->request->getArray('filters') as $fieldName => $value) {
38
				if ('' !== $value) {
39
					$conditions[] = [
40
						'fieldName' => $fieldName,
41
						'value' => $value,
42
						'operator' => 'a',
43
					];
44
				}
45
			}
46
			$relatedListModel->setConditions($conditions);
47
		}
48
		$relatedListModel->setFields($fields);
49
		$relatedListModel->setLimit($this->request->getInteger('length'));
50
		$relatedListModel->setOffset($this->request->getInteger('start'));
51
		$relatedListModel->loadRecordsList();
52
		foreach ($relatedListModel->getRecordsListModel() as $id => $recordModel) {
53
			$row = [];
54
			foreach ($columns as $column) {
55
				if ($column) {
56
					$value = $recordModel->getListDisplayValue($column);
57
				} else {
58
					$value = $recordModel->getRelatedListActions();
59
				}
60
				$row[] = $value;
61
			}
62
			$rows[] = $row;
63
		}
64
		$response = [
65
			'draw' => $this->request->getInteger('draw'),
66
			'iTotalDisplayRecords' => \count($rows),
67
			'iTotalRecords' => $relatedListModel->getCount(),
68
			'aaData' => $rows,
69
		];
70
		header('content-type: text/json; charset=UTF-8');
71
		echo \App\Json::encode($response);
72
	}
73
}
74