Passed
Push — master ( f17b13...8bf1e0 )
by Radu
01:33
created

AbstractDataTables::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 1
dl 0
loc 3
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
1
<?php
2
namespace WebServCo\Framework\DataTables;
3
4
use WebServCo\Framework\Database\Order;
0 ignored issues
show
Bug introduced by
This use statement conflicts with another class in this namespace, WebServCo\Framework\DataTables\Order. Consider defining an alias.

Let?s assume that you have a directory layout like this:

.
|-- OtherDir
|   |-- Bar.php
|   `-- Foo.php
`-- SomeDir
    `-- Foo.php

and let?s assume the following content of Bar.php:

// Bar.php
namespace OtherDir;

use SomeDir\Foo; // This now conflicts the class OtherDir\Foo

If both files OtherDir/Foo.php and SomeDir/Foo.php are loaded in the same runtime, you will see a PHP error such as the following:

PHP Fatal error:  Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.php

However, as OtherDir/Foo.php does not necessarily have to be loaded and the error is only triggered if it is loaded before OtherDir/Bar.php, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias:

// Bar.php
namespace OtherDir;

use SomeDir\Foo as SomeDirFoo; // There is no conflict anymore.
Loading history...
5
6
abstract class AbstractDataTables
7
{
8
    protected $db;
9
10
    abstract protected function getDatabaseColumnName($dataTablesColumnName);
11
    abstract protected function getQuery($searchQuery, $orderQuery, $limitQuery);
12
    abstract protected function getRecordsTotalQuery();
13
14
    public function __construct(\WebServCo\Framework\Interfaces\DatabaseInterface $db)
15
    {
16
        $this->db = $db;
17
    }
18
19
    public function getResponse(Request $request)
20
    {
21
        $params = [];
22
        $limitQuery = null;
23
24
        $columnArrayObject = $request->getColumns();
25
26
        $searchQuery = $this->getSearchQuery($columnArrayObject);
27
28
        $orderArrayObject = $request->getOrder();
29
        $orderQuery = $this->getOrderQuery($columnArrayObject, $orderArrayObject);
30
31
        $length = $request->getLength();
32
        if (-1 != $length) {
33
            $limitQuery = 'LIMIT ?, ?';
34
            $params[] = $request->getStart();
35
            $params[] = $length;
36
        }
37
38
        $pdoStatement = $this->db->query(
39
            $this->getQuery($searchQuery, $orderQuery, $limitQuery),
40
            $params
41
        );
42
43
        $data = $this->getData($columnArrayObject, $pdoStatement);
44
45
        $recordsFiltered = $this->getRecordsFiltered();
46
47
        $recordsTotal = $this->getRecordsTotal($recordsFiltered, $searchQuery);
48
49
        return new Response(
50
            $request->getDraw(),
51
            $recordsTotal,
52
            $recordsFiltered,
53
            $data
54
        );
55
    }
56
57
    protected function getData(ColumnArrayObject $columnArrayObject, \PDOStatement $pdoStatement)
58
    {
59
        $data = [];
60
        while ($row = $pdoStatement->fetch(\PDO::FETCH_ASSOC)) {
61
            $item = [];
62
            foreach ($columnArrayObject as $column) {
63
                $name = $column->getData();
64
                $item[$name] = isset($row[$name]) ? $row[$name] : null;
65
            }
66
            $data[] = $item;
67
        }
68
        return $data;
69
    }
70
71
    protected function getOrderQuery(ColumnArrayObject $columnArrayObject, OrderArrayObject $orderArrayObject)
72
    {
73
        $orderQuery = null;
74
        $orderTotal = $orderArrayObject->count();
75
        if (0 < $orderTotal) {
76
            $orderQuery = "ORDER BY";
77
            $items = [];
78
            foreach ($orderArrayObject as $order) {
79
                if ($columnArrayObject[$order->getColumn()]->getOrderable()) {
80
                    $dir = strtoupper($order->getDir());
81
                    $items[] = sprintf(
82
                        ' %s %s',
83
                        $this->getDatabaseColumnName($columnArrayObject[$order->getColumn()]->getData()),
84
                        in_array($dir, [Order::ASC, Order::DESC]) ? $dir : Order::ASC
85
                    );
86
                }
87
            }
88
            $orderQuery .= implode(",", $items);
89
        }
90
        return $orderQuery;
91
    }
92
93
    protected function getRecordsFiltered()
94
    {
95
        return $this->db->getColumn("SELECT FOUND_ROWS()", [], 0);
96
    }
97
98
    protected function getRecordsTotal($recordsFiltered, $searchQuery)
99
    {
100
        if (empty($searchQuery)) {
101
            return $recordsFiltered;
102
        }
103
        return $this->db->getColumn( // grand total - query without the search, order, limits
104
            $this->getRecordsTotalQuery(),
105
            []
106
        );
107
    }
108
109
    protected function getSearchQuery(\WebServCo\Framework\DataTables\ColumnArrayObject $columnArrayObject)
110
    {
111
        $searchQuery = null;
112
        foreach ($columnArrayObject as $column) {
113
            if ($column->getSearchable()) {
114
                $search = $column->getSearch();
115
                $searchValue = $search->getValue();
116
                if (!empty($searchValue)) {
117
                    $searchQuery .= sprintf(
118
                        " AND %s REGEXP '%s'",
119
                        $this->getDatabaseColumnName($column->getData()),
120
                        $searchValue
121
                    );
122
                }
123
            }
124
        }
125
        return $searchQuery;
126
    }
127
}
128