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

RelatedListView::process()   C

Complexity

Conditions 12
Paths 48

Size

Total Lines 53

Duplication

Lines 53
Ratio 100 %

Importance

Changes 0
Metric Value
dl 53
loc 53
rs 6.9666
c 0
b 0
f 0
cc 12
nc 48
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
 * 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