Completed
Pull Request — master (#5)
by
unknown
09:53
created

EloquentDatatable::addOptions()   C

Complexity

Conditions 10
Paths 3

Size

Total Lines 24
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
c 2
b 0
f 0
dl 0
loc 24
rs 5.2164
cc 10
eloc 14
nc 3
nop 0

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php namespace Distilleries\DatatableBuilder;
2
3
use \Datatable;
4
use Illuminate\Database\Eloquent\Model;
5
use \ReflectionClass;
6
use \FormBuilder;
7
use \Request;
8
9
abstract class EloquentDatatable {
10
11
    protected $model;
12
    protected $colomns;
13
    protected $form = null;
14
    protected $colomnsDisplay = [];
15
    protected $datatableOptions = [];
16
17
    // 0 can be an integer to represents the column's number or it can be a string that references the column's name
18
    protected $defaultOrder = [[0, 'desc']];
19
20
    // ------------------------------------------------------------------------------------------------
21
22
    public function __construct(Model $model = null)
23
    {
24
        $this->model = $model;
25
    }
26
27
    /**
28
     * @param Eloquent $model
29
     */
30
    public function setModel(Model $model)
31
    {
32
        $this->model = $model;
33
    }
34
35
36
    // ------------------------------------------------------------------------------------------------
37
38
    /**
39
     * @param string $name
40
     * @param \Closure $closure
41
     */
42
    public function add($name, $closure = null, $translation = '')
43
    {
44
        if (!empty($closure))
45
        {
46
            $this->colomns[] = [
47
                $name,
48
                $closure
49
            ];
50
        } else
51
        {
52
            $this->colomns[] = $name;
53
        }
54
55
        $this->addTranslation($name, $translation);
56
57
        return $this;
58
59
    }
60
61
    /**
62
     * @param string $translation
63
     * @param string $name
64
     */
65
    public function addTranslation($name, $translation)
66
    {
67
        $this->colomnsDisplay[] = (!empty($translation)) ? $translation : ucfirst($name);
68
    }
69
70
    // ------------------------------------------------------------------------------------------------
71
72
    public function applyFilters()
73
    {
74
        $allInput = Request::all();
75
        $columns  = \Schema::getColumnListing($this->model->getTable());
76
77
        foreach ($allInput as $name => $input)
78
        {
79
            if (in_array($name, $columns) && $input != '')
80
            {
81
82
                $this->model = $this->model->where($name, '=', $input);
83
            }
84
        }
85
    }
86
87
    // ------------------------------------------------------------------------------------------------
88
89
    public function generateColomns()
90
    {
91
        $this->applyFilters();
92
93
        $datatable        = Datatable::query($this->model);
94
        $colSearchAndSort = array();
95
        $sortOnly = array();
96
97
        if (!empty($this->colomns))
98
        {
99
            foreach ($this->colomns as $key => $value)
100
            {
101
102
                if (is_string($value))
103
                {
104
                    $datatable->showColumns($value);
105
                    $colSearchAndSort[] = $value;
106
107
                } else if (is_array($value) && count($value) == 2)
108
                {
109
                    $datatable->addColumn($value[0], $value[1]);
110
                    $sortOnly[] = $value[0];
111
                }
112
113
            }
114
        }
115
116
        $datatable = $this->setClassRow($datatable);
117
        $datatable->orderColumns(array_merge($colSearchAndSort, $sortOnly));
118
        $datatable->searchColumns($colSearchAndSort);
119
120
        return $datatable->make();
121
    }
122
123
    public function setClassRow($datatable)
124
    {
125
        //DT_RowClass
126
        $datatable->setRowClass(function($row)
127
        {
128
            return (isset($row->status) && empty($row->status)) ? 'danger' : '';
129
        });
130
131
        return $datatable;
132
    }
133
134
    // ------------------------------------------------------------------------------------------------
135
    public function generateHtmlRender($template = 'datatable-builder::part.datatable', $route = '')
136
    {
137
        return view($template, [
138
            'colomns_display' => $this->colomnsDisplay,
139
            'datatable_options' => $this->addOptions(),
140
            'route'           => !empty($route) ? $route : $this->getControllerNameForAction().'@getDatatable',
141
            'filters'         => $this->addFilter(),
142
        ]);
143
    }
144
145
    // ------------------------------------------------------------------------------------------------
146
    public function addDefaultAction($template = 'datatable-builder::form.components.datatable.actions', $route = '')
147
    {
148
149
        $reflection = new ReflectionClass(get_class($this));
150
151
        $this->add('actions', function($model) use ($template, $reflection, $route)
152
        {
153
            return view($template, array(
0 ignored issues
show
Bug introduced by
The method render does only exist in Illuminate\View\View, but not in Illuminate\Contracts\View\Factory.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
154
                'data'  => $model->toArray(),
155
                'route' => !empty($route) ? $route.'@' : $this->getControllerNameForAction().'@'
156
            ))->render();
157
        });
158
    }
159
160
    // ------------------------------------------------------------------------------------------------
161
    // ------------------------------------------------------------------------------------------------
162
    // ------------------------------------------------------------------------------------------------
163
164
165
    protected function addFilter($template = 'datatable-builder::form.components.datatable.filter')
166
    {
167
        $this->form = FormBuilder::plain();
168
        $this->filters();
169
170
        $filter_content = view($template, [
0 ignored issues
show
Bug introduced by
The method render does only exist in Illuminate\View\View, but not in Illuminate\Contracts\View\Factory.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
171
            'form' => $this->form
172
        ])->render();
173
174
175
        return $filter_content;
176
177
    }
178
179
    // ------------------------------------------------------------------------------------------------
180
    // ------------------------------------------------------------------------------------------------
181
    // ------------------------------------------------------------------------------------------------
182
183
184
    protected function addOptions()
185
    {
186
        if (!array_key_exists('order', $this->datatableOptions) && !empty($this->defaultOrder)) {
187
            if (is_array($this->defaultOrder)) {
188
                foreach ($this->defaultOrder as $keyOrder => $order) {
189
                    if (is_string($order[0])) {
190
                        foreach ($this->colomns as $key => $colomn) {
191
                            if (is_array($colomn)) {
192
                                $colomn = $colomn[0];
193
                            }
194
                            if ($colomn == $order[0]) {
195
                                $this->defaultOrder[$keyOrder][0] = $key;
196
                            }
197
                        }
198
                        if (is_string($this->defaultOrder[$keyOrder][0])) {
199
                            $this->defaultOrder[$keyOrder][0] = 0;
200
                        }
201
                    }
202
                }
203
                $this->datatableOptions['order'] = $this->defaultOrder;
204
            }
205
        }
206
        return $this->datatableOptions;
207
    }
208
209
    // ------------------------------------------------------------------------------------------------
210
211
    protected function getControllerNameForAction() {
212
213
        $action = explode('@', \Route::currentRouteAction());
214
        return '\\'.$action[0];
215
    }
216
217
    // ------------------------------------------------------------------------------------------------
218
219
    public function filters()
220
    {
221
222
        //Add the fileds of the filter form here
223
224
    }
225
226
    // ------------------------------------------------------------------------------------------------
227
    // ------------------------------------------------------------------------------------------------
228
    // ------------------------------------------------------------------------------------------------
229
230
    abstract public function build();
231
}