DataTablesAdapter   A
last analyzed

Complexity

Total Complexity 10

Size/Duplication

Total Lines 74
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Importance

Changes 0
Metric Value
wmc 10
lcom 1
cbo 0
dl 0
loc 74
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
B bind() 0 21 8
A getResponse() 0 15 2
1
<?php
2
3
namespace BWC\Share\App;
4
5
use Symfony\Component\HttpFoundation\Request;
6
use Symfony\Component\HttpFoundation\Response;
7
8
class DataTablesAdapter
9
{
10
    /** @var string */
11
    public $echo;
12
13
    /** @var string */
14
    public $search;
15
16
    /** @var int */
17
    public $pageSize;
18
19
    /** @var int */
20
    public $offset;
21
22
    /** @var int */
23
    public $pageNum;
24
25
    /** @var array columnIndex=>ASC|DESC */
26
    public $sortIndexInfo;
27
28
    /** @var array columnName=>bool */
29
    public $sortInfo;
30
31
32
    /**
33
     * @param Request $request  Where to read input parameters from
34
     * @param array $columns   Optional list of column names from table need to build sortInfo, if omitted only sortIndexInfo would be available
35
     */
36
    function bind(Request $request, array $columns = null) {
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
37
        $this->echo = $request->get('sEcho');
38
        $this->search = $request->get('sSearch');
39
        $this->pageSize = intval($request->get('iDisplayLength'));
40
        $this->offset = intval($request->get('iDisplayStart'));
41
        $this->pageNum = $this->pageSize ? floor($this->offset / $this->pageSize)+1 : 1;
0 ignored issues
show
Documentation Bug introduced by
It seems like $this->pageSize ? floor(...this->pageSize) + 1 : 1 can also be of type double. However, the property $pageNum is declared as type integer. Maybe add an additional type check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.

For example, imagine you have a variable $accountId that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to the id property of an instance of the Account class. This class holds a proper account, so the id value must no longer be false.

Either this assignment is in error or a type check should be added for that assignment.

class Id
{
    public $id;

    public function __construct($id)
    {
        $this->id = $id;
    }

}

class Account
{
    /** @var  Id $id */
    public $id;
}

$account_id = false;

if (starsAreRight()) {
    $account_id = new Id(42);
}

$account = new Account();
if ($account instanceof Id)
{
    $account->id = $account_id;
}
Loading history...
42
        $this->sortIndexInfo = array();
43
        for ($i=0; $i<10; $i++) {
44
            $key = 'iSortCol_'.$i;
45
            if (!$request->query->has($key)) break;
46
            $sortColIdx = intval($request->get($key));
47
            $sortDir =  strtoupper($request->get('sSortDir_0'));
48
            if ($sortDir != 'ASC' && $sortDir != 'DESC') {
49
                $sortDir = 'ASC';
50
            }
51
            $this->sortIndexInfo[] = array($sortColIdx, $sortDir);
52
            if ($columns && isset($columns[$sortColIdx])) {
53
                $this->sortInfo[$columns[$sortColIdx]] = $sortDir == 'ASC';
54
            }
55
        }
56
    }
57
58
59
60
    /**
61
     * @param object[] $array
62
     * @param int|bool $totalRecords
63
     * @return Response
64
     */
65
    function getResponse(array $array, $totalRecords = false) {
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
66
        $data = new \stdClass();
67
        if ($totalRecords) {
68
            $data->iTotalDisplayRecords = $totalRecords;
69
            $data->iTotalRecords = $totalRecords;
70
        } else {
71
            $data->iTotalDisplayRecords = count($array);
72
            $data->iTotalRecords = count($array);
73
        }
74
        //$data->iTotalRecords = $totalRecords === false ? $data->iTotalDisplayRecords : $totalRecords; // SELECT FOUND_ROWS()
75
        $data->sEcho = $this->echo;
76
        $data->aaData = $array;
77
        $json = json_encode($data);
78
        return new Response($json, 200, array('Content-Type'=>'application/json'));
79
    }
80
81
}