BaseModel::getAllColumnsNames()   B
last analyzed

Complexity

Conditions 8
Paths 21

Size

Total Lines 51
Code Lines 38

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 14
CRAP Score 24.1252

Importance

Changes 3
Bugs 0 Features 0
Metric Value
eloc 38
c 3
b 0
f 0
dl 0
loc 51
ccs 14
cts 38
cp 0.3684
rs 8.0675
cc 8
nc 21
nop 0
crap 24.1252

How to fix   Long Method   

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
namespace Distilleries\Expendable\Models;
4
5
use Distilleries\Security\Helpers\Security;
6
use Distilleries\Expendable\Models\Traits\ReservedKeyWord;
7
use Distilleries\Expendable\Models\Traits\ReservedKeyWordTrait;
8
use Illuminate\Database\Eloquent\Model;
9
use \DB;
10
use \Exception;
11
12
class BaseModel extends Model implements ReservedKeyWord
13
{
14
15
    use ReservedKeyWordTrait;
16
17
18 14
    public static function getChoice()
19
    {
20
21 14
        $data   = self::all();
22 14
        $result = [];
23 14
        foreach ($data as $item) {
24 14
            $result[$item['id']] = isset($item['libelle']) ? $item['libelle'] : $item['id'];
25
        }
26
27 14
        return $result;
28
    }
29
30 4
    public function scopeSearch($query, $searchQuery)
31
    {
32
33
        return $query->where(function($query) use ($searchQuery) {
34 4
            $columns = $this->getAllColumnsNames();
35
36 4
            foreach ($columns as $column) {
37 4
                $column = $this->isReserved($column) ? '"'.$column.'"' : $column;
38
39 4
                if ((DB::connection()->getDriverName()) == 'oracle') {
40
                    $query->orWhereRaw('LOWER('.$column.') like ? ESCAPE \'\\\'', ['%'.Security::escapeLike(strtolower($searchQuery)).'%']);
41
                } else {
42 4
                    $query->orwhere($column, 'like', '%'.Security::escapeLike($searchQuery, '\\\'').'%');
43
                }
44
45
            }
46 4
        });
47
    }
48
49 6
    public function getAllColumnsNames()
50
    {
51 6
        switch (DB::connection()->getDriverName()) {
52 3
            case 'pgsql':
53
                $query       = "SELECT column_name FROM information_schema.columns WHERE table_name = '".$this->getTable()."'";
54
                $column_name = 'column_name';
55
                $reverse     = true;
56
                break;
57
58 3
            case 'sqlite':
59 6
                $query       = "PRAGMA table_info('".$this->getTable()."')";
60 6
                $column_name = 'name';
61 6
                $reverse     = true;
62 6
                break;
63
64
65
            case 'mysql':
66
                $query       = 'SHOW COLUMNS FROM '.$this->getTable();
67
                $column_name = 'Field';
68
                $reverse     = false;
69
                break;
70
71
            case 'sqlsrv':
72
                $parts       = explode('.', $this->getTable());
73
                $num         = (count($parts) - 1);
74
                $table       = $parts[$num];
75
                $query       = "SELECT column_name FROM ".DB::connection()->getConfig('database').".INFORMATION_SCHEMA.COLUMNS WHERE TABLE_NAME = N'".$table."'";
76
                $column_name = 'column_name';
77
                $reverse     = false;
78
                break;
79
            case 'oracle':
80
                $query       = 'SELECT COLUMN_NAME from ALL_TAB_COLUMNS WHERE TABLE_NAME=\''.strtoupper($this->getTable()).'\' AND DATA_TYPE <> \'CLOB\' AND DATA_TYPE <> \'NUMBER\' AND DATA_TYPE <> \'TIMESTAMP\'';
81
                $column_name = 'column_name';
82
                $reverse     = false;
83
                break;
84
            default:
85
                $error = 'Database driver not supported: '.DB::connection()->getConfig('driver');
86
                throw new Exception($error);
87
        }
88
89 6
        $columns = [];
90
91 6
        foreach (DB::select($query) as $column) {
92 6
            $columns[] = $column->$column_name;
93
        }
94
95 6
        if ($reverse) {
96 6
            $columns = array_reverse($columns);
97
        }
98
99 6
        return $columns;
100
    }
101
102 10
    public function scopeBetweenCreate($query, $start, $end)
103
    {
104 10
        return $query->whereBetween($this->getTable().'.created_at', [$start, $end]);
105
    }
106
107 4
    public function scopeBetweenUpdate($query, $start, $end)
108
    {
109 4
        return $query->whereBetween($this->getTable().'.updated_at', [$start, $end]);
110
    }
111
112
113
}