Passed
Push — master ( d1c6ec...66cfc7 )
by Iman
06:37
created

CRUDBooster::getUrlParameters()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 1
dl 0
loc 3
rs 10
c 0
b 0
f 0
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\LogsModule\LogsRepository;
8
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...
9
use crocodicstudio\crudbooster\Modules\PrivilegeModule\PrivilegeHelpers;
10
use Session;
11
use Request;
12
use Schema;
13
use Cache;
14
use DB;
15
use Route;
16
use Config;
17
use Validator;
18
19
class CRUDBooster
20
{
21
    use PrivilegeHelpers, GetCurrentX;
0 ignored issues
show
Bug introduced by
The trait crocodicstudio\crudboost...ilegeModule\GetCurrentX requires the property $name which is not provided by crocodicstudio\crudbooster\helpers\CRUDBooster.
Loading history...
introduced by
The trait crocodicstudio\crudboost...Module\PrivilegeHelpers requires some properties which are not provided by crocodicstudio\crudbooster\helpers\CRUDBooster: $path, $is_superadmin, $cms_roles_id, $can_create, $can_delete, $can_see_module, $can_edit, $theme_color, $can_read
Loading history...
22
23
    public static function parseSqlTable($table)
24
    {
25
        $f = explode('.', $table);
26
27
        if (count($f) == 1) {
28
            return ["table" => $f[0], "database" => cbConfig('MAIN_DB_DATABASE')];
29
        }
30
        if (count($f) == 2) {
31
            return ["database" => $f[0], "table" => $f[1]];
32
        }
33
34
        if (count($f) == 3) {
35
            return ["table" => $f[0], "schema" => $f[1], "table" => $f[2]];
36
        }
37
38
        return false;
39
    }
40
41
    public static function adminPath($path = null)
42
    {
43
        return url(cbAdminPath().'/'.$path);
44
    }
45
46
    public static function deleteConfirm($redirectTo)
47
    {
48
        return view('_admin_template.deleteConfirmModal', ['redirectTo'=> $redirectTo])->render();
49
    }
50
51
    public static function getValueFilter($field)
52
    {
53
        return self::getFilter($field, 'value');
54
    }
55
56
    private static function getFilter($field, $index)
57
    {
58
        $filter = request('filter_column');
59
        if ($filter[$field]) {
60
            return $filter[$field][$index];
61
        }
62
    }
63
64
    public static function getSortingFilter($field)
65
    {
66
        return self::getFilter($field, 'sorting');
67
    }
68
69
    public static function getTypeFilter($field)
70
    {
71
        return self::getFilter($field, 'type');
72
    }
73
74
    public static function first($table, $id)
75
    {
76
        $table = self::parseSqlTable($table)['table'];
77
78
        if (! is_array($id)) {
79
            $id = [DbInspector::findPK($table) => $id];
80
        }
81
82
        return DB::table($table)->where($id)->first();
83
    }
84
85
    public static function urlFilterColumn($key, $type, $value = '', $singleSorting = true)
86
    {
87
        return \crocodicstudio\crudbooster\CBCoreModule\Index\ViewHelpers::urlFilterColumn($key, $type, $value, $singleSorting);
88
    }
89
90
    public static function mainpath($path = null)
91
    {
92
        $controllerName = strtok(Route::currentRouteAction(), '@');
93
        // $controllerName = array_pop(explode('\\', $controllerName));
94
        $controllerName = basename($controllerName);
95
        $routeUrl = route($controllerName.'GetIndex');
96
97
        if (! $path) {
98
            return trim($routeUrl, '/');
99
        }
100
101
        if (substr($path, 0, 1) == '?') {
102
            return trim($routeUrl, '/').$path;
103
        }
104
105
        return $routeUrl.'/'.$path;
106
    }
107
108
    public static function listCbTables()
109
    {
110
        $tables = DbInspector::listTables();
111
112
        $filter = function ($tableName) {
113
114
            if ($tableName == config('database.migrations')) {
115
                return false;
116
            }
117
118
            if ($tableName == 'cms_users') {
119
                return true;
120
            }
121
122
            if (starts_with($tableName, 'cms_')) {
123
                return false;
124
            }
125
126
            return true;
127
        };
128
129
        return array_filter($tables, $filter);
130
    }
131
132
    public static function getUrlParameters($exception = null)
133
    {
134
        return ViewHelpers::getUrlParameters($exception);
135
    }
136
137
    /*    public static function isExistsController($table)
138
        {
139
            $ctrlName = ucwords(str_replace('_', ' ', $table));
140
            $ctrlName = str_replace(' ', '', $ctrlName).'Controller.php';
141
            $path = base_path(controllers_dir());
142
            $path2 = base_path(controllers_dir()."ControllerMaster/");
143
144
            if (file_exists($path.'Admin'.$ctrlName) || file_exists($path2.'Admin'.$ctrlName) || file_exists($path2.$ctrlName)) {
145
                return true;
146
            }
147
148
            return false;
149
        }*/
150
151
    public static function routeController(string $prefix, string $controller, $namespace = '')
152
    {
153
        CbRouter::routeController($prefix, $controller, $namespace);
154
    }
155
156
    /*
157
    | -------------------------------------------------------------
158
    | Alternate route for Laravel Route::controller
159
    | -------------------------------------------------------------
160
    | $prefix       = path of route
161
    | $controller   = controller name
162
    | $namespace    = namespace of controller (optional)
163
    |
164
    */
165
166
    public static function redirect($to, $message, $type = 'warning')
167
    {
168
        if (Request::ajax()) {
169
            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

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

172
        sendAndTerminate(/** @scrutinizer ignore-type */ redirect($to)->with(['message' => $message, 'message_type' => $type]));
Loading history...
173
    }
174
175
    public static function allowOnlySuperAdmin()
176
    {
177
        event('cb.unauthorizedTryToSuperAdminArea', [cbUser(), request()->fullUrlWithQuery()]);
0 ignored issues
show
Bug introduced by
The call to Illuminate\Http\Request::fullUrlWithQuery() has too few arguments starting with query. ( Ignorable by Annotation )

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

177
        event('cb.unauthorizedTryToSuperAdminArea', [cbUser(), request()->/** @scrutinizer ignore-call */ fullUrlWithQuery()]);

This check compares calls to functions or methods with their respective definitions. If the call has less arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.

Loading history...
178
        if (self::isSuperadmin()) {
179
            return true;
180
        }
181
182
        self::denyAccess();
183
    }
184
}
185