DataTablesWrapper   A
last analyzed

Complexity

Total Complexity 28

Size/Duplication

Total Lines 301
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 28
eloc 59
dl 0
loc 301
rs 10
c 1
b 0
f 0

24 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 1
A addColumn() 0 8 2
A setProvider() 0 3 1
A getMapping() 0 2 1
A getServerSide() 0 2 1
A getOptions() 0 2 1
A getUrl() 0 2 1
A getColumns() 0 2 1
A setUrl() 0 3 1
A setColumns() 0 3 1
A getRequest() 0 2 1
A setOptions() 0 3 1
A setResponse() 0 3 1
A setMethod() 0 3 2
A setMapping() 0 3 1
A getColumn() 0 7 2
A setProcessing() 0 3 1
A getResponse() 0 2 1
A getProvider() 0 2 1
A getMethod() 0 2 1
A setServerSide() 0 3 1
A getProcessing() 0 2 1
A setRequest() 0 3 1
A removeColumn() 0 9 2
1
<?php
2
3
/*
4
 * This file is part of the jquery-datatables-bundle package.
5
 *
6
 * (c) 2018 WEBEWEB
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace WBW\Bundle\JQuery\DataTablesBundle\Model;
13
14
use WBW\Bundle\CoreBundle\Security\Core\User\UserTrait;
15
use WBW\Bundle\JQuery\DataTablesBundle\Api\DataTablesColumnInterface;
16
use WBW\Bundle\JQuery\DataTablesBundle\Api\DataTablesMappingInterface;
17
use WBW\Bundle\JQuery\DataTablesBundle\Api\DataTablesOptionsInterface;
18
use WBW\Bundle\JQuery\DataTablesBundle\Api\DataTablesRequestInterface;
19
use WBW\Bundle\JQuery\DataTablesBundle\Api\DataTablesResponseInterface;
20
use WBW\Bundle\JQuery\DataTablesBundle\Api\DataTablesWrapperInterface;
21
use WBW\Bundle\JQuery\DataTablesBundle\Provider\DataTablesProviderInterface;
22
23
/**
24
 * DataTables wrapper.
25
 *
26
 * @author webeweb <https://github.com/webeweb>
27
 * @package WBW\Bundle\JQuery\DataTablesBundle\Model
28
 */
29
class DataTablesWrapper implements DataTablesWrapperInterface {
30
31
    use UserTrait {
32
        setUser as public;
33
    }
34
35
    /**
36
     * Columns.
37
     *
38
     * @var DataTablesColumnInterface[]
39
     */
40
    private $columns;
41
42
    /**
43
     * Mapping.
44
     *
45
     * @var DataTablesMappingInterface
46
     */
47
    private $mapping;
48
49
    /**
50
     * Method.
51
     *
52
     * @var string
53
     */
54
    private $method;
55
56
    /**
57
     * Options.
58
     *
59
     * @var DataTablesOptionsInterface|null
60
     */
61
    private $options;
62
63
    /**
64
     * Processing.
65
     *
66
     * @var bool|null
67
     */
68
    private $processing;
69
70
    /**
71
     * Provider.
72
     *
73
     * @var DataTablesProviderInterface|null
74
     */
75
    private $provider;
76
77
    /**
78
     * Request.
79
     *
80
     * @var DataTablesRequestInterface|null
81
     */
82
    private $request;
83
84
    /**
85
     * Response.
86
     *
87
     * @var DataTablesResponseInterface|null
88
     */
89
    private $response;
90
91
    /**
92
     * Server side.
93
     *
94
     * @var bool|null
95
     */
96
    private $serverSide;
97
98
    /**
99
     * URL.
100
     *
101
     * @var string|null
102
     */
103
    private $url;
104
105
    /**
106
     * Constructor.
107
     */
108
    public function __construct() {
109
        $this->setColumns([]);
110
        $this->setMapping(new DataTablesMapping());
111
        $this->setMethod("POST");
112
        $this->setProcessing(true);
113
        $this->setServerSide(true);
114
    }
115
116
    /**
117
     * {@inheritDoc}
118
     */
119
    public function addColumn(DataTablesColumnInterface $column): DataTablesWrapperInterface {
120
121
        if (null === $column->getMapping()->getPrefix()) {
122
            $column->getMapping()->setPrefix($this->mapping->getPrefix());
123
        }
124
125
        $this->columns[$column->getData()] = $column;
126
        return $this;
127
    }
128
129
    /**
130
     * {@inheritDoc}
131
     */
132
    public function getColumn(string $data): ?DataTablesColumnInterface {
133
134
        if (true === array_key_exists($data, $this->columns)) {
135
            return $this->columns[$data];
136
        }
137
138
        return null;
139
    }
140
141
    /**
142
     * {@inheritDoc}
143
     */
144
    public function getColumns(): array {
145
        return $this->columns;
146
    }
147
148
    /**
149
     * {@inheritDoc}
150
     */
151
    public function getMapping(): DataTablesMappingInterface {
152
        return $this->mapping;
153
    }
154
155
    /**
156
     * {@inheritDoc}
157
     */
158
    public function getMethod(): string {
159
        return $this->method;
160
    }
161
162
    /**
163
     * {@inheritDoc}
164
     */
165
    public function getOptions(): ?DataTablesOptionsInterface {
166
        return $this->options;
167
    }
168
169
    /**
170
     * {@inheritDoc}
171
     */
172
    public function getProcessing(): ?bool {
173
        return $this->processing;
174
    }
175
176
    /**
177
     * {@inheritDoc}
178
     */
179
    public function getProvider(): ?DataTablesProviderInterface {
180
        return $this->provider;
181
    }
182
183
    /**
184
     * {@inheritDoc}
185
     */
186
    public function getRequest(): ?DataTablesRequestInterface {
187
        return $this->request;
188
    }
189
190
    /**
191
     * {@inheritDoc}
192
     */
193
    public function getResponse(): ?DataTablesResponseInterface {
194
        return $this->response;
195
    }
196
197
    /**
198
     * {@inheritDoc}
199
     */
200
    public function getServerSide(): ?bool {
201
        return $this->serverSide;
202
    }
203
204
    /**
205
     * {@inheritDoc}
206
     */
207
    public function getUrl(): ?string {
208
        return $this->url;
209
    }
210
211
    /**
212
     * Remove a column.
213
     *
214
     * @param DataTablesColumnInterface $column The column.
215
     * @return DataTablesWrapperInterface Returns this wrapper.
216
     */
217
    public function removeColumn(DataTablesColumnInterface $column): DataTablesWrapperInterface {
218
219
        if (true === array_key_exists($column->getData(), $this->columns)) {
220
221
            $this->columns[$column->getData()]->getMapping()->setPrefix(null);
222
            unset($this->columns[$column->getData()]);
223
        }
224
225
        return $this;
226
    }
227
228
    /**
229
     * Set the columns.
230
     *
231
     * @param DataTablesColumnInterface[] $columns The columns.
232
     * @return DataTablesWrapperInterface Returns this wrapper.
233
     */
234
    private function setColumns(array $columns): DataTablesWrapperInterface {
235
        $this->columns = $columns;
236
        return $this;
237
    }
238
239
    /**
240
     * Set the mapping.
241
     *
242
     * @param DataTablesMappingInterface $mapping The mapping
243
     * @return DataTablesWrapperInterface Returns this wrapper.
244
     */
245
    public function setMapping(DataTablesMappingInterface $mapping): DataTablesWrapperInterface {
246
        $this->mapping = $mapping;
247
        return $this;
248
    }
249
250
    /**
251
     * Set the method.
252
     *
253
     * @param string|null $method The method.
254
     * @return DataTablesWrapperInterface Returns this wrapper.
255
     */
256
    public function setMethod(?string $method): DataTablesWrapperInterface {
257
        $this->method = (true === in_array($method, ["GET", "POST"]) ? $method : "POST");
258
        return $this;
259
    }
260
261
    /**
262
     * Set the options.
263
     *
264
     * @param DataTablesOptionsInterface|null $options The options.
265
     * @return DataTablesWrapperInterface Returns this wrapper.
266
     */
267
    public function setOptions(?DataTablesOptionsInterface $options): DataTablesWrapperInterface {
268
        $this->options = $options;
269
        return $this;
270
    }
271
272
    /**
273
     * Set the processing.
274
     *
275
     * @param bool|null $processing The processing.
276
     * @return DataTablesWrapperInterface Returns this wrapper.
277
     */
278
    public function setProcessing(?bool $processing): DataTablesWrapperInterface {
279
        $this->processing = !(false === $processing);
280
        return $this;
281
    }
282
283
    /**
284
     * Set the provider.
285
     *
286
     * @param DataTablesProviderInterface|null $provider The provider.
287
     * @return DataTablesWrapperInterface Returns this wrapper.
288
     */
289
    public function setProvider(?DataTablesProviderInterface $provider): DataTablesWrapperInterface {
290
        $this->provider = $provider;
291
        return $this;
292
    }
293
294
    /**
295
     * {@inheritDoc}
296
     */
297
    public function setRequest(?DataTablesRequestInterface $request): DataTablesWrapperInterface {
298
        $this->request = $request;
299
        return $this;
300
    }
301
302
    /**
303
     * {@inheritDoc}
304
     */
305
    public function setResponse(?DataTablesResponseInterface $response): DataTablesWrapperInterface {
306
        $this->response = $response;
307
        return $this;
308
    }
309
310
    /**
311
     * Set the server side.
312
     *
313
     * @param bool|null $serverSide The server side.
314
     * @return DataTablesWrapperInterface Returns this wrapper.
315
     */
316
    public function setServerSide(?bool $serverSide): DataTablesWrapperInterface {
317
        $this->serverSide = !(false === $serverSide);
318
        return $this;
319
    }
320
321
    /**
322
     * Set the URL.
323
     *
324
     * @param string|null $url The UTL.
325
     * @return DataTablesWrapperInterface Returns this wrapper.
326
     */
327
    public function setUrl(?string $url): DataTablesWrapperInterface {
328
        $this->url = $url;
329
        return $this;
330
    }
331
}
332