-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathDataTables.php
More file actions
131 lines (102 loc) · 3.94 KB
/
DataTables.php
File metadata and controls
131 lines (102 loc) · 3.94 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
<?php
/*
* This file is part of the Gesdinet MetronicDataTableBundle package.
*
* (c) Gesdinet <marcos@gesdinet.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Gesdinet\MetronicDataTableBundle;
use Psr\Log\LoggerInterface;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Validator\Validator\ValidatorInterface;
/**
* DataTables lookup service.
*/
class DataTables implements DataTablesInterface
{
protected $logger;
protected $validator;
/** @var DataTableHandlerInterface[] List of registered DataTable services. */
protected $services = [];
/**
* Dependency Injection constructor.
*/
public function __construct(LoggerInterface $logger, ValidatorInterface $validator)
{
$this->logger = $logger;
$this->validator = $validator;
}
/**
* Registers specified DataTable handler.
*
* @param DataTableHandlerInterface $service service of the DataTable handler
* @param string $id dataTable ID
*/
public function addService(DataTableHandlerInterface $service, string $id = null)
{
$service_id = $id ?? $service::getServiceId();
$this->services[$service_id] = $service;
}
/**
* {@inheritdoc}
*/
public function handle(Request $request, string $id): DataTableResults
{
$this->logger->debug('Handle DataTable request', [$id]);
// Retrieve sent parameters.
$params = new Parameters();
$keyParams = [
'datatable', // deprecated in v5.0.7
'pagination',
'query',
'sort',
];
$pagination = $request->get('pagination');
$start = ($pagination['page'] - 1) * $pagination['perpage'];
$params->start = ($start > 0 && (!isset($pagination['total']) || $start < $pagination['total'])) ? $start : 0;
$params->length = $pagination['perpage'];
$params->search = $request->get('query')['generalSearch'] ?? '';
$params->order = [$request->get('sort')];
$allParams = $request->isMethod(Request::METHOD_POST)
? $request->request->all()
: $request->query->all();
$params->customData = array_diff_key($allParams, array_flip($keyParams));
// Validate sent parameters.
$violations = $this->validator->validate($params);
if (is_countable($violations) ? count($violations) : 0) {
$message = $violations->get(0)->getMessage();
$this->logger->error($message, ['request']);
throw new DataTableException($message);
}
// Check for valid handler is registered.
if (!array_key_exists($id, $this->services)) {
$message = 'Unknown DataTable ID.';
$this->logger->error($message, [$id]);
throw new DataTableException($message);
}
// Convert sent parameters into data model.
$query = new DataTableQuery($params);
// Pass the data model to the handler.
$result = null;
$timer_started = microtime(true);
try {
$result = $this->services[$id]->handle($query);
} catch (\Exception $e) {
$this->logger->error($e->getMessage(), [$this->services[$id]]);
throw new DataTableException($e->getMessage());
} finally {
$timer_stopped = microtime(true);
$this->logger->debug('DataTable processing time', [$timer_stopped - $timer_started, $this->services[$id]]);
}
// Validate results returned from handler.
$violations = $this->validator->validate($result);
if (is_countable($violations) ? count($violations) : 0) {
$message = $violations->get(0)->getMessage();
$this->logger->error($message, ['response']);
throw new DataTableException($message);
}
return $result;
}
}