Completed
Push — master ( 1e5acb...71c789 )
by Maxime
7s
created

EloquentDatatable::addOptions()   C

Complexity

Conditions 10
Paths 3

Size

Total Lines 24
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 11
CRAP Score 22.5

Importance

Changes 2
Bugs 0 Features 0
Metric Value
c 2
b 0
f 0
dl 0
loc 24
ccs 11
cts 22
cp 0.5
rs 5.2164
cc 10
eloc 14
nc 3
nop 0
crap 22.5

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 8
    public function __construct(Model $model = null)
23
    {
24 8
        $this->model = $model;
25 6
    }
26
27
    /**
28
     * @param Eloquent $model
29
     */
30 4
    public function setModel(Model $model)
31
    {
32 4
        $this->model = $model;
33 3
    }
34
35
36
    // ------------------------------------------------------------------------------------------------
37
38
    /**
39
     * @param string $name
40
     * @param \Closure $closure
41
     */
42 8
    public function add($name, $closure = null, $translation = '')
43
    {
44 8
        if (!empty($closure))
45 6
        {
46 8
            $this->colomns[] = [
47 8
                $name,
48 2
                $closure
49 6
            ];
50 6
        } else
51
        {
52 8
            $this->colomns[] = $name;
53
        }
54
55 8
        $this->addTranslation($name, $translation);
56
57 8
        return $this;
58
59
    }
60
61
    /**
62
     * @param string $translation
63
     * @param string $name
64
     */
65 8
    public function addTranslation($name, $translation)
66
    {
67 8
        $this->colomnsDisplay[] = (!empty($translation)) ? $translation : ucfirst($name);
68 6
    }
69
70
    // ------------------------------------------------------------------------------------------------
71
72 4
    public function applyFilters()
73
    {
74 4
        $allInput = Request::all();
75 4
        $columns  = \Schema::getColumnListing($this->model->getTable());
76
77 4
        foreach ($allInput as $name => $input)
78
        {
79 4
            if (in_array($name, $columns) && $input != '')
80 3
            {
81
82 4
                $this->model = $this->model->where($name, '=', $input);
83 3
            }
84 3
        }
85 3
    }
86
87
    // ------------------------------------------------------------------------------------------------
88
89 4
    public function generateColomns()
90
    {
91 4
        $this->applyFilters();
92
93 4
        $datatable        = Datatable::query($this->model);
94 4
        $colSearchAndSort = array();
95 4
        $sortOnly = array();
96
97 4
        if (!empty($this->colomns))
98 3
        {
99 4
            foreach ($this->colomns as $key => $value)
100
            {
101
102 4
                if (is_string($value))
103 3
                {
104 4
                    $datatable->showColumns($value);
105 4
                    $colSearchAndSort[] = $value;
106
107 4
                } else if (is_array($value) && count($value) == 2)
108 3
                {
109 4
                    $datatable->addColumn($value[0], $value[1]);
110 4
                    $sortOnly[] = $value[0];
111 3
                }
112
113 3
            }
114 3
        }
115
116 4
        $datatable = $this->setClassRow($datatable);
117 4
        $datatable->orderColumns(array_merge($colSearchAndSort, $sortOnly));
118 4
        $datatable->searchColumns($colSearchAndSort);
119
120 4
        return $datatable->make();
121
    }
122
123 4
    public function setClassRow($datatable)
124
    {
125
        //DT_RowClass
126
        $datatable->setRowClass(function($row)
127
        {
128 4
            return (isset($row->status) && empty($row->status)) ? 'danger' : '';
129 4
        });
130
131 4
        return $datatable;
132
    }
133
134
    // ------------------------------------------------------------------------------------------------
135 8
    public function generateHtmlRender($template = 'datatable-builder::part.datatable', $route = '')
136
    {
137 8
        return view($template, [
138 8
            'colomns_display' => $this->colomnsDisplay,
139 8
            'datatable_options' => $this->addOptions(),
140 8
            'route'           => !empty($route) ? $route : $this->getControllerNameForAction().'@getDatatable',
141 8
            'filters'         => $this->addFilter(),
142 6
        ]);
143
    }
144
145
    // ------------------------------------------------------------------------------------------------
146 8
    public function addDefaultAction($template = 'datatable-builder::form.components.datatable.actions', $route = '')
147
    {
148
149 8
        $reflection = new ReflectionClass(get_class($this));
150
151 8
        $this->add('actions', function($model) use ($template, $reflection, $route)
152
        {
153 4
            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 4
                'data'  => $model->toArray(),
155 4
                'route' => !empty($route) ? $route.'@' : $this->getControllerNameForAction().'@'
156 4
            ))->render();
157 8
        });
158 6
    }
159
160
    // ------------------------------------------------------------------------------------------------
161
    // ------------------------------------------------------------------------------------------------
162
    // ------------------------------------------------------------------------------------------------
163
164
165 8
    protected function addFilter($template = 'datatable-builder::form.components.datatable.filter')
166
    {
167 8
        $this->form = FormBuilder::plain();
168 8
        $this->filters();
169
170 8
        $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 8
            'form' => $this->form
172 8
        ])->render();
173
174
175 8
        return $filter_content;
176
177
    }
178
179
    // ------------------------------------------------------------------------------------------------
180
    // ------------------------------------------------------------------------------------------------
181
    // ------------------------------------------------------------------------------------------------
182
183
184 8
    protected function addOptions()
185
    {
186 8
        if (!array_key_exists('order', $this->datatableOptions) && !empty($this->defaultOrder)) {
187 8
            if (is_array($this->defaultOrder)) {
188 8
                foreach ($this->defaultOrder as $keyOrder => $order) {
189 8
                    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 2
                            $this->defaultOrder[$keyOrder][0] = 0;
200
                        }
201
                    }
202 6
                }
203 8
                $this->datatableOptions['order'] = $this->defaultOrder;
204 6
            }
205 6
        }
206 8
        return $this->datatableOptions;
207
    }
208
209
    // ------------------------------------------------------------------------------------------------
210
211 8
    protected function getControllerNameForAction() {
212
213 8
        $action = explode('@', \Route::currentRouteAction());
214 8
        return '\\'.$action[0];
215
    }
216
217
    // ------------------------------------------------------------------------------------------------
218
219 3
    public function filters()
220 3
    {
221
222
        //Add the fileds of the filter form here
223
224 3
    }
225
226
    // ------------------------------------------------------------------------------------------------
227
    // ------------------------------------------------------------------------------------------------
228
    // ------------------------------------------------------------------------------------------------
229
230
    abstract public function build();
231
}