Passed
Push — master ( 21d85a...18548d )
by Iman
04:31
created

HandleListAction::handleListAction()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 6
nc 2
nop 4
dl 0
loc 9
rs 9.6666
c 0
b 0
f 0
1
<?php
2
3
namespace crocodicstudio\crudbooster\controllers\ApiController;
4
5
class HandleListAction
6
{
7
    /**
8
     * @param $table
9
     * @param $data
10
     * @param $responsesFields
11
     * @return array
12
     */
13
    public static function handleListAction($table, $data, $responsesFields, $ctrl)
14
    {
15
        $rows = self::sortRows($table, $data);
16
        if ($rows) {
17
            return self::handleRows($responsesFields, $rows);
18
        }
19
        $result = ApiResponder::makeResult(0, 'No data found !');
20
        $result['data'] = [];
21
        ApiResponder::send($result, request()->all(), $ctrl);
22
    }
23
24
    /**
25
     * @param $responsesFields
26
     * @param $rows
27
     * @return array
28
     */
29
    private static function handleRows($responsesFields, $rows)
30
    {
31
        foreach ($rows as &$row) {
32
            HandleDetailsAction::handleFile($row, $responsesFields);
0 ignored issues
show
Bug Best Practice introduced by
The method crocodicstudio\crudboost...ilsAction::handleFile() is not static, but was called statically. ( Ignorable by Annotation )

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

32
            HandleDetailsAction::/** @scrutinizer ignore-call */ 
33
                                 handleFile($row, $responsesFields);
Loading history...
33
        }
34
35
        $result = ApiResponder::makeResult(1, 'success');
36
        $result['data'] = $rows;
37
38
        return $result;
39
    }
40
41
42
    /**
43
     * @param $table
44
     * @param $data
45
     * @return mixed
46
     */
47
    private static function sortRows($table, $data)
48
    {
49
        $orderBy = request('orderby', $table.'.id,desc');
50
51
        list($orderByCol, $orderByVal) = explode(',', $orderBy);
0 ignored issues
show
Bug introduced by
It seems like $orderBy can also be of type array; however, parameter $string of explode() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

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

51
        list($orderByCol, $orderByVal) = explode(',', /** @scrutinizer ignore-type */ $orderBy);
Loading history...
52
53
        $rows = $data->orderby($orderByCol, $orderByVal)->get();
54
55
        return $rows;
56
    }
57
58
}