Passed
Push — master ( afb6cf...b53137 )
by Iman
04:24
created

CRUDBooster::get()   B

Complexity

Conditions 5
Paths 16

Size

Total Lines 19
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 12
nc 16
nop 5
dl 0
loc 19
c 0
b 0
f 0
cc 5
rs 8.8571
1
<?php
2
3
namespace crocodicstudio\crudbooster\helpers;
4
5
use crocodicstudio\crudbooster\CBCoreModule\Facades\CbRouter;
6
use crocodicstudio\crudbooster\CBCoreModule\ViewHelpers;
7
use crocodicstudio\crudbooster\Modules\AuthModule\Helpers;
8
use crocodicstudio\crudbooster\Modules\LogsModule\LogsRepository;
9
use crocodicstudio\crudbooster\Modules\PrivilegeModule\GetCurrentX;
0 ignored issues
show
Bug introduced by
This use statement conflicts with another class in this namespace, crocodicstudio\crudbooster\helpers\GetCurrentX. Consider defining an alias.

Let?s assume that you have a directory layout like this:

.
|-- OtherDir
|   |-- Bar.php
|   `-- Foo.php
`-- SomeDir
    `-- Foo.php

and let?s assume the following content of Bar.php:

// Bar.php
namespace OtherDir;

use SomeDir\Foo; // This now conflicts the class OtherDir\Foo

If both files OtherDir/Foo.php and SomeDir/Foo.php are loaded in the same runtime, you will see a PHP error such as the following:

PHP Fatal error:  Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.php

However, as OtherDir/Foo.php does not necessarily have to be loaded and the error is only triggered if it is loaded before OtherDir/Bar.php, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias:

// Bar.php
namespace OtherDir;

use SomeDir\Foo as SomeDirFoo; // There is no conflict anymore.
Loading history...
10
use crocodicstudio\crudbooster\Modules\PrivilegeModule\PrivilegeHelpers;
11
use Session;
12
use Request;
13
use Schema;
14
use Cache;
0 ignored issues
show
Bug introduced by
This use statement conflicts with another class in this namespace, crocodicstudio\crudbooster\helpers\Cache. Consider defining an alias.

Let?s assume that you have a directory layout like this:

.
|-- OtherDir
|   |-- Bar.php
|   `-- Foo.php
`-- SomeDir
    `-- Foo.php

and let?s assume the following content of Bar.php:

// Bar.php
namespace OtherDir;

use SomeDir\Foo; // This now conflicts the class OtherDir\Foo

If both files OtherDir/Foo.php and SomeDir/Foo.php are loaded in the same runtime, you will see a PHP error such as the following:

PHP Fatal error:  Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.php

However, as OtherDir/Foo.php does not necessarily have to be loaded and the error is only triggered if it is loaded before OtherDir/Bar.php, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias:

// Bar.php
namespace OtherDir;

use SomeDir\Foo as SomeDirFoo; // There is no conflict anymore.
Loading history...
15
use DB;
16
use Route;
17
use Config;
18
use Validator;
19
20
class CRUDBooster
21
{
22
    use PrivilegeHelpers, GetCurrentX, Helpers;
0 ignored issues
show
introduced by
The trait crocodicstudio\crudboost...Module\PrivilegeHelpers requires some properties which are not provided by crocodicstudio\crudbooster\helpers\CRUDBooster: $path, $is_read, $is_superadmin, $is_visible, $id_cms_privileges, $is_delete, $is_create, $is_edit, $theme_color
Loading history...
23
24
    public static function parseSqlTable($table)
25
    {
26
        $f = explode('.', $table);
27
28
        if (count($f) == 1) {
29
            return ["table" => $f[0], "database" => cbConfig('MAIN_DB_DATABASE')];
30
        }
31
        if (count($f) == 2) {
32
            return ["database" => $f[0], "table" => $f[1]];
33
        }
34
35
        if (count($f) == 3) {
36
            return ["table" => $f[0], "schema" => $f[1], "table" => $f[2]];
37
        }
38
39
        return false;
40
    }
41
42
    public static function adminPath($path = null)
43
    {
44
        return url(cbAdminPath().'/'.$path);
45
    }
46
47
    public static function deleteConfirm($redirectTo)
48
    {
49
        return ViewHelpers::delConfirm($redirectTo);
50
    }
51
52
    public static function getValueFilter($field)
53
    {
54
        return self::getFilter($field, 'value');
55
    }
56
57
    private static function getFilter($field, $index)
58
    {
59
        $filter = request('filter_column');
60
        if ($filter[$field]) {
61
            return $filter[$field][$index];
62
        }
63
    }
64
65
    public static function getSortingFilter($field)
66
    {
67
        return self::getFilter($field, 'sorting');
68
    }
69
70
    public static function getTypeFilter($field)
71
    {
72
        return self::getFilter($field, 'type');
73
    }
74
75
    public static function first($table, $id)
76
    {
77
        $table = self::parseSqlTable($table)['table'];
78
        if (! is_array($id)) {
79
            $pk = DbInspector::findPK($table);
80
81
            return DB::table($table)->where($pk, $id)->first();
82
        }
83
84
        $first = DB::table($table);
85
        foreach ($id as $k => $v) {
86
            $first->where($k, $v);
87
        }
88
89
        return $first->first();
90
    }
91
92
    public static function urlFilterColumn($key, $type, $value = '', $singleSorting = true)
93
    {
94
        return \crocodicstudio\crudbooster\CBCoreModule\Index\ViewHelpers::urlFilterColumn($key, $type, $value, $singleSorting);
95
    }
96
97
    public static function mainpath($path = null)
98
    {
99
        $controllerName = strtok(Route::currentRouteAction(), '@');
100
        // $controllerName = array_pop(explode('\\', $controllerName));
101
        $controllerName = basename($controllerName);
102
        $routeUrl = route($controllerName.'GetIndex');
103
104
        if (! $path) {
105
            return trim($routeUrl, '/');
106
        }
107
108
        if (substr($path, 0, 1) == '?') {
109
            return trim($routeUrl, '/').$path;
110
        }
111
112
        return $routeUrl.'/'.$path;
113
    }
114
115
    public static function insertTryLog($action, $name = '')
116
    {
117
        self::insertLog(trans("crudbooster_logging.log_try_".$action, ['name' => $name, 'module' => GetCurrentX::getCurrentModule()]));
118
    }
119
120
    public static function insertLog($description)
121
    {
122
        LogsRepository::insertLog('crudbooster: '.$description, auth('cbAdmin')->id());
123
    }
124
125
    public static function listCbTables()
126
    {
127
        $tables = DbInspector::listTables();
128
129
        $filter = function ($tableName) {
130
131
            if ($tableName == config('database.migrations')) {
132
                return false;
133
            }
134
135
            if ($tableName == 'cms_users') {
136
                return true;
137
            }
138
139
            if (starts_with($tableName, 'cms_')) {
140
                return false;
141
            }
142
143
            return true;
144
        };
145
146
        return array_filter($tables, $filter);
147
    }
148
149
    public static function getUrlParameters($exception = null)
150
    {
151
        return ViewHelpers::getUrlParameters($exception);
152
    }
153
154
    /*    public static function isExistsController($table)
155
        {
156
            $ctrlName = ucwords(str_replace('_', ' ', $table));
157
            $ctrlName = str_replace(' ', '', $ctrlName).'Controller.php';
158
            $path = base_path(controllers_dir());
159
            $path2 = base_path(controllers_dir()."ControllerMaster/");
160
161
            if (file_exists($path.'Admin'.$ctrlName) || file_exists($path2.'Admin'.$ctrlName) || file_exists($path2.$ctrlName)) {
162
                return true;
163
            }
164
165
            return false;
166
        }*/
167
168
    public static function routeController(string $prefix, string $controller, $namespace = '')
169
    {
170
        CbRouter::routeController($prefix, $controller, $namespace);
171
    }
172
173
    /*
174
    | -------------------------------------------------------------
175
    | Alternate route for Laravel Route::controller
176
    | -------------------------------------------------------------
177
    | $prefix       = path of route
178
    | $controller   = controller name
179
    | $namespace    = namespace of controller (optional)
180
    |
181
    */
182
183
    public static function redirect($to, $message, $type = 'warning')
184
    {
185
        if (Request::ajax()) {
186
            sendAndTerminate(response()->json(['message' => $message, 'message_type' => $type, 'redirect_url' => $to]));
0 ignored issues
show
Bug introduced by
The method json() does not exist on Symfony\Component\HttpFoundation\Response. It seems like you code against a sub-type of Symfony\Component\HttpFoundation\Response such as Illuminate\Http\Response or Illuminate\Http\JsonResponse or Illuminate\Http\RedirectResponse. ( Ignorable by Annotation )

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

186
            sendAndTerminate(response()->/** @scrutinizer ignore-call */ json(['message' => $message, 'message_type' => $type, 'redirect_url' => $to]));
Loading history...
187
        }
188
189
        sendAndTerminate(redirect($to)->with(['message' => $message, 'message_type' => $type]));
0 ignored issues
show
Bug introduced by
It seems like redirect($to)->with(arra...essage_type' => $type)) can also be of type Illuminate\Routing\Redirector; however, parameter $response of sendAndTerminate() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

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

189
        sendAndTerminate(/** @scrutinizer ignore-type */ redirect($to)->with(['message' => $message, 'message_type' => $type]));
Loading history...
190
    }
191
}
192