Passed
Push — master ( ab23d3...456c2a )
by Ferry
05:06
created

Query::repository()   F

Complexity

Conditions 20
Paths 1440

Size

Total Lines 83
Code Lines 43

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 20
eloc 43
c 1
b 0
f 0
nc 1440
nop 1
dl 0
loc 83
rs 0

How to fix   Long Method    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
2
/**
3
 * Created by PhpStorm.
4
 * User: User
5
 * Date: 8/15/2019
6
 * Time: 11:44 PM
7
 */
8
9
namespace crocodicstudio\crudbooster\controllers\traits;
10
11
use crocodicstudio\crudbooster\helpers\SchemaHelper;
12
use Illuminate\Support\Facades\Schema;
13
use Illuminate\Support\Facades\DB;
14
15
trait Query
16
{
17
18
    private function repository($callback = null)
19
    {
20
        $joins = columnSingleton()->getJoin();
21
        $columns = columnSingleton()->getColumns();
22
23
        $query = DB::table($this->data['table']);
24
25
        $query->addSelect($this->data['table'].'.'.cb()->pk($this->data['table']).' as primary_key');
26
27
        $softDelete = isset($this->data['disable_soft_delete'])?$this->data['disable_soft_delete']:true;
28
        if($softDelete === true && SchemaHelper::hasColumn($this->data['table'],'deleted_at')) {
29
            $query->whereNull($this->data['table'].'.deleted_at');
30
        }
31
32
        if(isset($joins)) {
33
            foreach($joins as $join)
34
            {
35
                $query->join($join['target_table'],
36
                    $join['target_table_primary'],
37
                    $join['operator'],
38
                    $join['source_table_foreign'],
39
                    $join['type']);
40
            }
41
        }
42
43
        foreach($columns as $column) {
44
            /** @var ColumnModel $column */
45
            if($column->getType() != "custom") {
46
                if(strpos($column->getField(),".") === false) {
47
                    if(SchemaHelper::hasColumn($this->data['table'], $column->getField())) {
48
                        $query->addSelect($this->data['table'].'.'.$column->getField());
49
                    }
50
                }else{
51
                    $query->addSelect($column->getField());
52
                }
53
            }
54
55
            $query = getTypeHook($column->getType())->query($query, $column);
56
        }
57
58
        if(request()->has('q'))
59
        {
60
            if(isset($this->data['hook_search_query'])) {
61
                $query = call_user_func($this->data['hook_search_query'], $query);
62
            }else{
63
                $query->where(function ($where) use ($columns) {
64
                    /**
65
                     * @var $where Builder
66
                     */
67
                    foreach($columns as $column)
68
                    {
69
                        if(strpos($column->getField(),".") === false) {
70
                            $field = $this->data['table'].'.'.$column->getField();
71
                        }else{
72
                            $field = $column->getField();
73
                        }
74
                        $where->orWhere($field, 'like', '%'.request('q').'%');
0 ignored issues
show
Bug introduced by
Are you sure request('q') of type Illuminate\Http\Request|array|string can be used in concatenation? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

74
                        $where->orWhere($field, 'like', '%'./** @scrutinizer ignore-type */ request('q').'%');
Loading history...
75
                    }
76
                });
77
            }
78
        }
79
80
81
        // Callback From this Method
82
        if(isset($callback) && is_callable($callback)) {
83
            $query = call_user_func($callback, $query);
84
        }
85
86
        if(isset($this->data['hook_index_query']) && is_callable($this->data['hook_index_query'])) {
87
            $query = call_user_func($this->data['hook_index_query'], $query);
88
        }
89
90
91
        if(request()->has(['order_by','order_sort']))
92
        {
93
            if(in_array(request('order_by'),columnSingleton()->getColumnNameOnly())) {
94
                $query->orderBy(request('order_by'), request('order_sort'));
95
            }
96
        }else{
97
            $query->orderBy($this->data['table'].'.'.cb()->findPrimaryKey($this->data['table']), "desc");
98
        }
99
100
        return $query;
101
    }
102
103
}