Passed
Push — developer ( e5c82c...bcac4b )
by Mariusz
32:34
created

ListView::process()   C

Complexity

Conditions 12
Paths 48

Size

Total Lines 51

Duplication

Lines 0
Ratio 0 %

Importance

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