Datatable::__construct()   A
last analyzed

Complexity

Conditions 6
Paths 8

Size

Total Lines 32
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 18
CRAP Score 6

Importance

Changes 0
Metric Value
cc 6
eloc 17
nc 8
nop 13
dl 0
loc 32
ccs 18
cts 18
cp 1
crap 6
rs 9.0777
c 0
b 0
f 0

How to fix   Many Parameters   

Many Parameters

Methods with many parameters are not only hard to understand, but their parameters also often become inconsistent when you need more, or different data.

There are several approaches to avoid long parameter lists:

1
<?php
2
3
namespace JeroenNoten\LaravelAdminLte\View\Components\Tool;
4
5
use Illuminate\Support\Arr;
6
use Illuminate\View\Component;
7
8
class Datatable extends Component
9
{
10
    /**
11
     * The table identification (id) attribute. Required in order to manage
12
     * the internal or external (JS) initialization.
13
     *
14
     * @var string
15
     */
16
    public $id;
17
18
    /**
19
     * An array with the set of headers (titles) for the table columns. Each
20
     * header can be a string or an array with next properties: label, width
21
     * and no-export.
22
     *
23
     * @var array
24
     */
25
    public $heads;
26
27
    /**
28
     * The table theme (light, dark, primary, secondary, info, warning or
29
     * danger).
30
     *
31
     * @var string
32
     */
33
    public $theme;
34
35
    /**
36
     * The table head theme (light or dark).
37
     *
38
     * @var string
39
     */
40
    public $headTheme;
41
42
    /**
43
     * The datatables plugin configuration parameters. Array with key => value
44
     * pairs, where the key should be an existing configuration property of
45
     * the datatables plugin.
46
     *
47
     * @var array
48
     */
49
    public $config;
50
51
    /**
52
     * Enables a footer with header cells. The footer can be fully customized
53
     * using the 'footerCallback' option of the plugin.
54
     *
55
     * @var mixed
56
     */
57
    public $withFooter;
58
59
    /**
60
     * The table footer theme (light or dark).
61
     *
62
     * @var string
63
     */
64
    public $footerTheme;
65
66
    /**
67
     * When enabled, borders will be displayed around the table.
68
     *
69
     * @var mixed
70
     */
71
    public $bordered;
72
73
    /**
74
     * When enabled, a hover effect will be available for the table rows.
75
     *
76
     * @var mixed
77
     */
78
    public $hoverable;
79
80
    /**
81
     * When enabled, a striped effect will be available for the table rows.
82
     *
83
     * @var mixed
84
     */
85
    public $striped;
86
87
    /**
88
     * When enabled, the table will be compressed using less white space between
89
     * cells and rows.
90
     *
91
     * @var mixed
92
     */
93
    public $compressed;
94
95
    /**
96
     * When enabled, the table cells will be vertically and horizontally
97
     * centered.
98
     *
99
     * @var mixed
100
     */
101
    public $beautify;
102
103
    /**
104
     * When enabled, a set of tool buttons for export the table will be
105
     * displayed.
106
     *
107
     * @var mixed
108
     */
109
    public $withButtons;
110
111
    /**
112
     * Create a new component instance.
113
     *
114
     * @return void
115
     */
116 3
    public function __construct(
117
        $id, $heads, $theme = null, $headTheme = null, $bordered = null,
118
        $hoverable = null, $striped = null, $compressed = null,
119
        $withFooter = null, $footerTheme = null, $beautify = null,
120
        $withButtons = null, $config = []
121
    ) {
122 3
        $this->id = $id;
123 3
        $this->heads = $heads;
124 3
        $this->theme = $theme;
125 3
        $this->headTheme = $headTheme;
126 3
        $this->bordered = $bordered;
127 3
        $this->hoverable = $hoverable;
128 3
        $this->striped = $striped;
129 3
        $this->compressed = $compressed;
130 3
        $this->withFooter = $withFooter;
131 3
        $this->footerTheme = $footerTheme;
132 3
        $this->beautify = $beautify;
133 3
        $this->withButtons = $withButtons;
134
135 3
        $this->config = is_array($config) ? $config : [];
136
137
        // When buttons are enabled, change the default table layout.
138
139 3
        if (isset($withButtons) && ! isset($this->config['dom'])) {
140 1
            $this->config['dom'] = $this->makeDomCfg();
141
        }
142
143
        // When buttons are enabled, setup the set of visible buttons and they
144
        // default style.
145
146 3
        if (isset($withButtons) && ! isset($this->config['buttons'])) {
147 1
            $this->config['buttons'] = $this->makeButtonsCfg();
148
        }
149
    }
150
151
    /**
152
     * Make the table class.
153
     *
154
     * @return string
155
     */
156 2
    public function makeTableClass()
157
    {
158 2
        $classes = ['table'];
159
160 2
        if (isset($this->bordered)) {
161 1
            $classes[] = 'table-bordered';
162
        }
163
164 2
        if (isset($this->hoverable)) {
165 1
            $classes[] = 'table-hover';
166
        }
167
168 2
        if (isset($this->striped)) {
169 1
            $classes[] = 'table-striped';
170
        }
171
172 2
        if (isset($this->compressed)) {
173 1
            $classes[] = 'table-sm';
174
        }
175
176 2
        if (isset($this->theme)) {
177 1
            $classes[] = "table-{$this->theme}";
178
        }
179
180 2
        return implode(' ', $classes);
181
    }
182
183
    /**
184
     * Make the Datatables 'dom' configuration with the buttons extension.
185
     *
186
     * @return string
187
     */
188 1
    protected function makeDomCfg()
189
    {
190
        // Give bootstrap style to table elements.
191
        // The built-in table control elements in DataTables are:
192
        // l - Length changing input control.
193
        // f - Filtering input.
194
        // t - The table!
195
        // i - Table information summary.
196
        // p - Pagination control.
197
        // r - Processing display element.
198
        // B - buttons extension.
199
200 1
        return '<"row" <"col-md-8" B> <"col-md-4" f> >
201
                <"row" <"col-12" tr> >
202 1
                <"row" <"col-md-5" i> <"col-md-7" p> >';
203
    }
204
205
    /**
206
     * Make the Datatables 'buttons' configuration object to define the set of
207
     * visible buttons and they style.
208
     *
209
     * @return array
210
     */
211 1
    protected function makeButtonsCfg()
212
    {
213
        // Configure the export columns selector. We are not going to export
214
        // columns that explicitly have the 'dt-no-export' attribute.
215
216 1
        $colSelector = ':not([dt-no-export])';
217
218
        // Button to change the page length of tables.
219
220 1
        $lengthBtn = [
221 1
            'extend' => 'pageLength',
222 1
            'className' => 'btn-default',
223 1
        ];
224
225
        // Button to print the data.
226
227 1
        $printBtn = [
228 1
            'extend' => 'print',
229 1
            'className' => 'btn-default',
230 1
            'text' => '<i class="fas fa-fw fa-lg fa-print"></i>',
231 1
            'titleAttr' => 'Print',
232 1
            'exportOptions' => ['columns' => $colSelector],
233 1
        ];
234
235
        // Button to export data to CSV file.
236
237 1
        $csvBtn = [
238 1
            'extend' => 'csv',
239 1
            'className' => 'btn-default',
240 1
            'text' => '<i class="fas fa-fw fa-lg fa-file-csv text-primary"></i>',
241 1
            'titleAttr' => 'Export to CSV',
242 1
            'exportOptions' => ['columns' => $colSelector],
243 1
        ];
244
245
        // Button to export data to Excel file.
246
247 1
        $excelBtn = [
248 1
            'extend' => 'excel',
249 1
            'className' => 'btn-default',
250 1
            'text' => '<i class="fas fa-fw fa-lg fa-file-excel text-success"></i>',
251 1
            'titleAttr' => 'Export to Excel',
252 1
            'exportOptions' => ['columns' => $colSelector],
253 1
        ];
254
255
        // Button to export data to PDF file.
256
257 1
        $pdfBtn = [
258 1
            'extend' => 'pdf',
259 1
            'className' => 'btn-default',
260 1
            'text' => '<i class="fas fa-fw fa-lg fa-file-pdf text-danger"></i>',
261 1
            'titleAttr' => 'Export to PDF',
262 1
            'exportOptions' => ['columns' => $colSelector],
263 1
        ];
264
265
        // The length change button should not be added if the configuration of
266
        // datatables explicitly disable it.
267
268 1
        $buttons = [$printBtn, $csvBtn, $excelBtn, $pdfBtn];
269
270 1
        if ($this->isLengthButtonEnabled()) {
271 1
            $buttons = Arr::prepend($buttons, $lengthBtn);
272
        }
273
274
        // Return the set of configured buttons.
275
276 1
        return [
277 1
            'dom' => ['button' => ['className' => 'btn']],
278 1
            'buttons' => $buttons,
279 1
        ];
280
    }
281
282
    /**
283
     * Check if length button should be available on the set of tool buttons.
284
     *
285
     * @return bool
286
     */
287 1
    protected function isLengthButtonEnabled()
288
    {
289 1
        if (! isset($this->config['lengthChange'])) {
290 1
            return true;
291
        }
292
293 1
        return (bool) $this->config['lengthChange'];
294
    }
295
296
    /**
297
     * Get the view / contents that represent the component.
298
     *
299
     * @return \Illuminate\View\View|string
300
     */
301 1
    public function render()
302
    {
303 1
        return view('adminlte::components.tool.datatable');
304
    }
305
}
306