Passed
Pull Request — master (#1111)
by Iman
04:07
created

CRUDBooster::isLocked()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 1
nc 1
nop 0
dl 0
loc 3
c 0
b 0
f 0
cc 1
rs 10
1
<?php
2
3
namespace crocodicstudio\crudbooster\helpers;
4
5
use crocodicstudio\crudbooster\CBCoreModule\CbUsersRepo;
6
use crocodicstudio\crudbooster\CBCoreModule\RouteController;
7
use crocodicstudio\crudbooster\CBCoreModule\ViewHelpers;
8
use crocodicstudio\crudbooster\Modules\AuthModule\Helpers;
9
use crocodicstudio\crudbooster\Modules\LogsModule\LogsRepository;
10
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...
11
use crocodicstudio\crudbooster\Modules\PrivilegeModule\PrivilegeHelpers;
12
use Session;
13
use Request;
14
use Schema;
15
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...
16
use DB;
17
use Route;
18
use Config;
19
use Validator;
20
21
class CRUDBooster
22
{
23
    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_visible, $is_delete, $is_create, $is_edit
Loading history...
24
25
    public static function get($table, $string_conditions = null, $orderby = null, $limit = null, $skip = null)
26
    {
27
        $table = self::parseSqlTable($table);
28
        $table = $table['table'];
29
        $query = DB::table($table);
30
        if ($string_conditions) {
31
            $query->whereraw($string_conditions);
32
        }
33
        if ($orderby) {
34
            $query->orderbyraw($orderby);
35
        }
36
        if ($limit) {
37
            $query->take($limit);
38
        }
39
        if ($skip) {
40
            $query->skip($skip);
41
        }
42
43
        return $query->get();
44
    }
45
46
    public static function parseSqlTable($table)
47
    {
48
        $f = explode('.', $table);
49
50
        if (count($f) == 1) {
51
            return ["table" => $f[0], "database" => cbConfig('MAIN_DB_DATABASE')];
52
        }
53
        if (count($f) == 2) {
54
            return ["database" => $f[0], "table" => $f[1]];
55
        }
56
57
        if (count($f) == 3) {
58
            return ["table" => $f[0], "schema" => $f[1], "table" => $f[2]];
59
        }
60
61
        return false;
62
    }
63
64
    public static function adminPath($path = null)
65
    {
66
        return url(cbAdminPath().'/'.$path);
67
    }
68
69
    public static function deleteConfirm($redirectTo)
70
    {
71
        ViewHelpers::delConfirm($redirectTo);
72
    }
73
74
    public static function getValueFilter($field)
75
    {
76
        self::getFilter($field, 'value');
77
    }
78
79
    private static function getFilter($field, $index)
80
    {
81
        $filter = request('filter_column');
82
        if ($filter[$field]) {
83
            return $filter[$field][$index];
84
        }
85
    }
86
87
    public static function getSortingFilter($field)
88
    {
89
        return self::getFilter($field, 'sorting');
90
    }
91
92
    public static function getTypeFilter($field)
93
    {
94
        return self::getFilter($field, 'type');
95
    }
96
97
/*    public static function stringBetween($string, $start, $end)
98
    {
99
        $string = ' '.$string;
100
        $ini = strpos($string, $start);
101
        if ($ini == 0) {
102
            return '';
103
        }
104
        $ini += strlen($start);
105
        $len = strpos($string, $end, $ini) - $ini;
106
107
        return substr($string, $ini, $len);
108
    }*/
109
110
    public static function first($table, $id)
111
    {
112
        $table = self::parseSqlTable($table)['table'];
113
        if (! is_array($id)) {
114
            $pk = self::pk($table);
0 ignored issues
show
Bug introduced by
The method pk() does not exist on crocodicstudio\crudbooster\helpers\CRUDBooster. ( Ignorable by Annotation )

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

114
            /** @scrutinizer ignore-call */ 
115
            $pk = self::pk($table);

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
115
116
            return DB::table($table)->where($pk, $id)->first();
117
        }
118
119
        $first = DB::table($table);
120
        foreach ($id as $k => $v) {
121
            $first->where($k, $v);
122
        }
123
124
        return $first->first();
125
    }
126
127
    public static function getTableForeignKey($fieldName)
128
    {
129
        if (substr($fieldName, 0, 3) == 'id_' || substr($fieldName, -3) == '_id') {
130
            return str_replace(['_id', 'id_'], '', $fieldName);
131
        }
132
    }
133
134
    public static function urlFilterColumn($key, $type, $value = '', $singleSorting = true)
135
    {
136
        return \crocodicstudio\crudbooster\CBCoreModule\Index\ViewHelpers::urlFilterColumn($key, $type, $value, $singleSorting);
137
    }
138
    public static function mainpath($path = null)
139
    {
140
        $controllerName = strtok(Route::currentRouteAction(), '@');
141
        // $controllerName = array_pop(explode('\\', $controllerName));
142
        $controllerName = basename($controllerName);
143
        $route_url = route($controllerName.'GetIndex');
144
145
        if (! $path) {
146
            return trim($route_url, '/');
147
        }
148
149
        if (substr($path, 0, 1) == '?') {
150
            return trim($route_url, '/').$path;
151
        }
152
153
        return $route_url.'/'.$path;
154
    }
155
156
    public static function insertLog($description)
157
    {
158
        LogsRepository::insertLog('crudbooster: '.$description, self::myId());
159
    }
160
161
    public static function insertTryLog($action, $name = '')
162
    {
163
        self::insertLog(trans("logging.log_try_".$action, ['name' => $name, 'module' => GetCurrentX::getCurrentModule()]));
0 ignored issues
show
Bug introduced by
Are you sure the usage of crocodicstudio\crudboost...ntX::getCurrentModule() targeting crocodicstudio\crudboost...ntX::getCurrentModule() seems to always return null.

This check looks for function or method calls that always return null and whose return value is used.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
if ($a->getObject()) {

The method getObject() can return nothing but null, so it makes no sense to use the return value.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
164
    }
165
166
    public static function referer()
167
    {
168
        return Request::server('HTTP_REFERER');
169
    }
170
171
    public static function listCbTables()
172
    {
173
        $tablesList = [];
174
        foreach (DbInspector::listTables() as $tableObj) {
175
176
            $tableName = $tableObj->TABLE_NAME;
177
            if ($tableName == config('database.migrations')) {
178
                continue;
179
            }
180
            if (substr($tableName, 0, 4) == 'cms_' && $tableName != 'cms_users') {
181
                continue;
182
            }
183
184
            $tablesList[] = $tableName;
185
        }
186
187
        return $tablesList;
188
    }
189
190
    public static function getUrlParameters($exception = null)
191
    {
192
        return ViewHelpers::getUrlParameters($exception);
193
    }
194
195
/*    public static function isExistsController($table)
196
    {
197
        $ctrlName = ucwords(str_replace('_', ' ', $table));
198
        $ctrlName = str_replace(' ', '', $ctrlName).'Controller.php';
199
        $path = base_path(controllers_dir());
200
        $path2 = base_path(controllers_dir()."ControllerMaster/");
201
202
        if (file_exists($path.'Admin'.$ctrlName) || file_exists($path2.'Admin'.$ctrlName) || file_exists($path2.$ctrlName)) {
203
            return true;
204
        }
205
206
        return false;
207
    }*/
208
209
    public static function routeController($prefix, $controller, $namespace = null)
210
    {
211
        RouteController::routeController($prefix, $controller, $namespace);
212
    }
213
214
    /*
215
    | -------------------------------------------------------------
216
    | Alternate route for Laravel Route::controller
217
    | -------------------------------------------------------------
218
    | $prefix       = path of route
219
    | $controller   = controller name
220
    | $namespace    = namespace of controller (optional)
221
    |
222
    */
223
224
    public static function redirect($to, $message, $type = 'warning')
225
    {
226
        if (Request::ajax()) {
227
            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

227
            sendAndTerminate(response()->/** @scrutinizer ignore-call */ json(['message' => $message, 'message_type' => $type, 'redirect_url' => $to]));
Loading history...
228
        }
229
230
        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

230
        sendAndTerminate(/** @scrutinizer ignore-type */ redirect($to)->with(['message' => $message, 'message_type' => $type]));
Loading history...
231
    }
232
233
    public static function componentsPath($type = '')
234
    {
235
        $componentPath = implode(DIRECTORY_SEPARATOR, ['vendor', 'crocodicstudio', 'crudbooster', 'src', 'views', 'default', 'type_components', $type]);
236
        return base_path($componentPath);
237
238
    }
239
240
    public static function PublishedComponentsPath($type = '')
241
    {
242
        $Path = implode(DIRECTORY_SEPARATOR, ['views', 'vendor', 'crudbooster', 'type_components', $type]);
243
        return resource_path($Path);
244
    }
245
}
246