Completed
Push — master ( 914f3a...0222da )
by Eliurkis
01:29
created

CrudDataTable   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 73
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 11
c 1
b 0
f 0
lcom 1
cbo 1
dl 0
loc 73
rs 10

5 Methods

Rating   Name   Duplication   Size   Complexity  
A index_source() 0 21 1
A getSortInformation() 0 7 1
A getRowsTotals() 0 10 3
A getRowsTotal() 0 8 1
B applySearchScope() 0 18 5
1
<?php
2
3
namespace Eliurkis\Crud;
4
5
use DB;
6
use Illuminate\Http\Request;
7
8
trait CrudDataTable
9
{
10
    protected $dataTableActivated = true;
11
12
    public function index_source(Request $request)
13
    {
14
        list($colSortBy, $colOrderBy) = $this->getSortInformation($this->columns, $request);
0 ignored issues
show
Bug introduced by
The property columns does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
15
        list($totalRows, $totalRowsFiltered) = $this->getRowsTotals($request->get('search')['value'] ?? null);
16
17
        $query = $this->entity->orderBy($colSortBy, $colOrderBy);
0 ignored issues
show
Bug introduced by
The property entity does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
18
        $query = $this->applySearchScope($query, $request->get('search')['value'] ?? null);
19
20
        $rows = $query->offset($request->get('start') ?? 0)
21
            ->limit($request->get('length') ?? $totalRows)
22
            ->get();
23
24
        return response()->json([
25
            'data'            => $rows,
26
            'draw'            => (int) ($request->get('draw') ?? 0),
27
            'recordsFiltered' => $totalRowsFiltered,
28
            'recordsTotal'    => $totalRows,
29
            'colSortBy'       => $colSortBy,
30
            'colOrderBy'      => $colOrderBy,
31
        ]);
32
    }
33
34
    protected function getSortInformation($cols, $request)
35
    {
36
        return [
37
            $cols[$request->get('order')[0]['column'] ?? 0],
38
            $request->get('order')[0]['dir'] ?? 'asc',
39
        ];
40
    }
41
42
    protected function getRowsTotals($searchValue = null)
43
    {
44
        $totalRows = $totalRowsFiltered = $this->getRowsTotal();
45
46
        if ($searchValue != '' && $this->searchable) {
0 ignored issues
show
Bug introduced by
The property searchable does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
47
            $totalRowsFiltered = $this->getRowsTotal($searchValue);
48
        }
49
50
        return [$totalRows, $totalRowsFiltered];
51
    }
52
53
    protected function getRowsTotal($searchValue = null)
54
    {
55
        $query = $this->entity->select(DB::raw('count(*) as total'));
56
        $query = $this->applySearchScope($query, $searchValue);
57
58
        return $query->first()
59
            ->total;
60
    }
61
62
    protected function applySearchScope($query, $searchValue = null)
63
    {
64
        if ($searchValue == '' || !$this->searchable) {
65
            return $query;
66
        }
67
68
        $searchable = $this->searchable;
69
70
        return $query->where(function ($query) use ($searchValue, $searchable) {
71
            foreach ($searchable as $key => $field) {
72
                $query = $key === 0
73
                    ? $query->where($field, 'like', '%'.$searchValue.'%')
74
                    : $query->orWhere($field, 'like', '%'.$searchValue.'%');
75
76
            }
77
            return $query;
78
        });
79
    }
80
}
81