Conditions | 17 |
Paths | 7 |
Total Lines | 58 |
Code Lines | 49 |
Lines | 0 |
Ratio | 0 % |
Changes | 0 |
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:
If many parameters/temporary variables are present:
1 | <?php |
||
19 | public function process(App\Request $request) |
||
20 | { |
||
21 | $type = $request->getByType('type'); |
||
22 | if (!isset(\App\Log::LOGS_VIEWER_COLUMN_MAP[$type])) { |
||
23 | throw new \App\Exceptions\NoPermittedForAdmin('ERR_ILLEGAL_VALUE'); |
||
24 | } |
||
25 | $rows = $columns = []; |
||
26 | foreach ($request->getArray('columns') as $key => $value) { |
||
27 | $columns[$key] = $value['name']; |
||
28 | } |
||
29 | $mapping = \App\Log::LOGS_VIEWER_COLUMN_MAP[$type]; |
||
30 | if (\App\Db::getInstance()->isTableExists($mapping['table'])) { |
||
31 | $query = (new \App\Db\Query())->from($mapping['table']); |
||
32 | $logsCountAll = (int) $query->count('*'); |
||
33 | $this->loadFilter($request, $mapping['filter'], $query); |
||
34 | $count = (int) $query->count('*'); |
||
35 | $query->limit($request->getInteger('length'))->offset($request->getInteger('start')); |
||
36 | $order = current($request->getArray('order', App\Purifier::ALNUM)); |
||
37 | if ($order && isset($columns[$order['column']], $mapping['columns'][$columns[$order['column']]])) { |
||
38 | $query->orderBy([$columns[$order['column']] => \App\Db::ASC === strtoupper($order['dir']) ? \SORT_ASC : \SORT_DESC]); |
||
39 | } |
||
40 | $dataReader = $query->createCommand()->query(); |
||
41 | while ($row = $dataReader->read()) { |
||
42 | $r = []; |
||
43 | foreach ($mapping['columns'] as $key => $value) { |
||
44 | switch ($value['type']) { |
||
45 | case 'DateTime': |
||
46 | $r[] = \App\Fields\DateTime::formatToDisplay($row[$key]); |
||
47 | break; |
||
48 | case 'Date': |
||
49 | $r[] = \App\Fields\Date::formatToDisplay($row[$key]); |
||
50 | break; |
||
51 | case 'Text': |
||
52 | $r[] = $row[$key] ? \App\Layout::truncateText($row[$key], 50, true) : ''; |
||
53 | break; |
||
54 | case 'Owner': |
||
55 | $r[] = \App\Fields\Owner::getUserLabel($row[$key]); |
||
56 | break; |
||
57 | case 'Reference': |
||
58 | $r[] = \App\Record::getLabel($row[$key]); |
||
59 | break; |
||
60 | case 'Boolean': |
||
61 | $r[] = \App\Language::translate(empty($row[$key]) ? 'LBL_NO' : 'LBL_YES'); |
||
62 | break; |
||
63 | default: |
||
64 | break; |
||
65 | } |
||
66 | } |
||
67 | $rows[] = $r; |
||
68 | } |
||
69 | $dataReader->close(); |
||
70 | } |
||
71 | header('content-type: text/json; charset=UTF-8'); |
||
72 | echo \App\Json::encode([ |
||
73 | 'draw' => $request->getInteger('draw'), |
||
74 | 'iTotalRecords' => $logsCountAll ?? 0, |
||
75 | 'iTotalDisplayRecords' => $count ?? 0, |
||
76 | 'aaData' => $rows |
||
77 | ]); |
||
109 |