Passed
Push — master ( 62919d...1f65fa )
by Iman
04:23
created

HandleListAction   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 52
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
dl 0
loc 52
rs 10
c 0
b 0
f 0
wmc 5

3 Methods

Rating   Name   Duplication   Size   Complexity  
A handle() 0 9 2
A handleRows() 0 10 2
A sortRows() 0 9 1
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
     * @param $ctrl
12
     * @return array
13
     */
14
    public static function handle($table, $data, $responsesFields, $ctrl)
15
    {
16
        $rows = self::sortRows($table, $data);
17
        if ($rows) {
18
            return self::handleRows($responsesFields, $rows);
19
        }
20
        $result = ApiResponder::makeResult(0, 'No data found !');
21
        $result['data'] = [];
22
        ApiResponder::send($result, request()->all(), $ctrl);
23
    }
24
25
    /**
26
     * @param $responsesFields
27
     * @param $rows
28
     * @return array
29
     */
30
    private static function handleRows($responsesFields, $rows)
31
    {
32
        foreach ($rows as &$row) {
33
            HandleDetailsAction::handleFile($row, $responsesFields);
34
        }
35
36
        $result = ApiResponder::makeResult(1, 'success');
37
        $result['data'] = $rows;
38
39
        return $result;
40
    }
41
42
43
    /**
44
     * @param $table
45
     * @param $data
46
     * @return mixed
47
     */
48
    private static function sortRows($table, $data)
49
    {
50
        $orderBy = request('orderby', $table.'.id,desc');
51
52
        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

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