getCondition()   C
last analyzed

Complexity

Conditions 17
Paths 10

Size

Total Lines 25
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 17
eloc 16
nc 10
nop 1
dl 0
loc 25
rs 5.2166
c 1
b 0
f 0

How to fix   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
use Illuminate\Support\Facades\Cache;
4
use Illuminate\Support\Facades\Schema;
5
use Illuminate\Support\Str;
6
use Pratiksh\Adminetic\Models\Admin\Setting;
7
8
if (! function_exists('spa')) {
9
    function spa()
10
    {
11
        return setting('spa', config('adminetic.spa', true));
12
    }
13
}
14
15
if (! function_exists('getClassesList')) {
16
    function getClassesList($dir)
17
    {
18
        $classes = \File::allFiles($dir);
19
        foreach ($classes as $class) {
20
            $class->classname = str_replace(
0 ignored issues
show
Bug introduced by
The property classname does not seem to exist on Symfony\Component\Finder\SplFileInfo.
Loading history...
21
                [app_path(), '/', '.php'],
22
                ['App', '\\', ''],
23
                $class->getRealPath()
24
            );
25
        }
26
27
        return $classes;
28
    }
29
}
30
if (! function_exists('getAllModelNames')) {
31
    function getAllModelNames($dir)
32
    {
33
        $modelNames = [];
34
        $models = getClassesList($dir);
35
        foreach ($models as $model) {
36
            $model_name = explode('\\', $model->classname);
0 ignored issues
show
Bug introduced by
The property classname does not seem to exist on Symfony\Component\Finder\SplFileInfo.
Loading history...
37
            $modelNames[] = end($model_name);
38
        }
39
40
        return $modelNames;
41
    }
42
}
43
44
if (! function_exists('validImageFolder')) {
45
    function validImageFolder($name, $default = 'default')
46
    {
47
        return strtolower(str_replace([' ', '-', '$', '<', '>', '&', '{', '*', '\\', '/', ':', '.', ';', ',', "'", '"'], '_', $name ?? trim($default)));
48
    }
49
}
50
51
if (! function_exists('getImagePlaceholder')) {
52
    function getImagePlaceholder()
53
    {
54
        return asset(config('adminetic.default_image_placeholder', 'adminetic/static/placeholder.png'));
55
    }
56
}
57
58
if (! function_exists('getVerticalImagePlaceholder')) {
59
    function getVerticalImagePlaceholder()
60
    {
61
        return asset(config('adminetic.vertical_image_placeholder', 'adminetic/static/vertical_placeholder.jpg'));
62
    }
63
}
64
65
if (! function_exists('getSliderPlaceholder')) {
66
    function getSliderPlaceholder()
67
    {
68
        return asset(config('adminetic.slider_placeholder', 'adminetic/static/slider.jpg'));
69
    }
70
}
71
72
if (! function_exists('getProfilePlaceholder')) {
73
    function getProfilePlaceholder($p = null)
74
    {
75
        $profile = $p ?? Auth::user()->profile ?? Auth::user()->profile()->create();
0 ignored issues
show
Bug introduced by
Accessing profile on the interface Illuminate\Contracts\Auth\Authenticatable suggest that you code against a concrete implementation. How about adding an instanceof check?
Loading history...
Bug introduced by
The method profile() does not exist on Illuminate\Contracts\Auth\Authenticatable. It seems like you code against a sub-type of Illuminate\Contracts\Auth\Authenticatable such as Illuminate\Foundation\Auth\User. ( Ignorable by Annotation )

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

75
        $profile = $p ?? Auth::user()->profile ?? Auth::user()->/** @scrutinizer ignore-call */ profile()->create();
Loading history...
76
77
        return isset($profile->profile_pic) ? (Illuminate\Support\Str::contains($profile->profile_pic, ['https://', 'http://']) ? $profile->profile_pic : asset('storage/'.$profile->profile_pic)) : asset(config('adminetic.profile_placeholder', 'adminetic/static/profile.gif'));
78
    }
79
}
80
81
if (! function_exists('title')) {
82
    function title()
83
    {
84
        return setting('title', config('adminetic.name', 'Adminetic'));
85
    }
86
}
87
88
if (! function_exists('description')) {
89
    function description($default = null)
90
    {
91
        return setting('description', config('adminetic.description', $default ?? 'Adminetic Admin Panel'));
92
    }
93
}
94
95
if (! function_exists('keywords')) {
96
    function keywords()
97
    {
98
        return setting('keywords', config('adminetic.keywords', 'adminetic admin panel, adminetic, pratik shrestha, doctype innovations'));
99
    }
100
}
101
102
if (! function_exists('loader_enabled')) {
103
    function loader_enabled()
104
    {
105
        return setting('loader_enabled', config('adminetic.loader_enabled', true));
106
    }
107
}
108
109
if (! function_exists('favicon')) {
110
    function favicon()
111
    {
112
        return getImg(! is_null(setting('favicon')) ? setting('favicon', config('adminetic.favicon', 'adminetic/static/favicon.png')) : config('adminetic.favicon', 'adminetic/static/favicon.png'), config('adminetic.favicon', 'adminetic/static/favicon.png'));
113
    }
114
}
115
116
if (! function_exists('logo')) {
117
    function logo()
118
    {
119
        return getImg(! is_null(setting('logo')) ? setting('logo', config('adminetic.logo', 'adminetic/static/logo.png')) : config('adminetic.logo', 'adminetic/static/logo.png'), config('adminetic.logo', 'adminetic/static/logo.png'));
120
    }
121
}
122
123
if (! function_exists('dark_logo')) {
124
    function dark_logo()
125
    {
126
        return getImg(! is_null(setting('dark_logo')) ? setting('dark_logo', config('adminetic.dark_logo', 'adminetic/static/dark_logo.png')) : config('adminetic.dark_logo', 'adminetic/static/dark_logo.png'), config('adminetic.dark_logo', 'adminetic/static/dark_logo.png'));
127
    }
128
}
129
130
if (! function_exists('logoBanner')) {
131
    function logoBanner()
132
    {
133
        return getImg(! is_null(setting('logo_banner')) ? setting('logo_banner', config('adminetic.logo_banner', 'adminetic/static/logo_banner.jpg')) : config('adminetic.logo_banner', 'adminetic/static/logo_banner.jpg'), config('adminetic.logo_banner', 'adminetic/static/logo_banner.jpg'));
134
    }
135
}
136
137
if (! function_exists('login_register_bg_image')) {
138
    function login_register_bg_image()
139
    {
140
        return getImg(! is_null(setting('login_register_bg_image')) ? setting('login_register_bg_image', config('adminetic.login_register_bg_img', 'adminetic/static/login_register_bg_img.jpg')) : config('adminetic.login_register_bg_img', 'adminetic/static/login_register_bg_img.jpg'), config('adminetic.login_register_bg_img', 'adminetic/static/login_register_bg_img.jpg'));
141
    }
142
}
143
144
if (! function_exists('getLazyLoadImg')) {
145
    function getLazyLoadImg()
146
    {
147
        return asset('adminetic/static/loader.svg');
148
    }
149
}
150
151
if (! function_exists('random_color_part')) {
152
    function random_color_part()
153
    {
154
        return str_pad(dechex(mt_rand(0, 255)), 2, '0', STR_PAD_LEFT);
155
    }
156
}
157
158
if (! function_exists('random_color')) {
159
    function random_color()
160
    {
161
        return random_color_part().random_color_part().random_color_part();
162
    }
163
}
164
165
if (! function_exists('setting')) {
166
    function setting($setting_name, $default = null)
167
    {
168
        $valid_setting_name = strtolower(str_replace(' ', '_', $setting_name));
169
        $settings = Cache::has('settings') ?
170
            Cache::get('settings')
171
            : Cache::rememberForever('settings', function () {
172
                return Setting::all();
173
            });
174
        $setting = $settings->first(function ($s) use ($valid_setting_name) {
175
            return $s->getRawOriginal('setting_name') == $valid_setting_name;
176
        });
177
178
        return isset($setting->value) ? $setting->value : ($default ?? null);
179
    }
180
}
181
182
if (! function_exists('preference')) {
183
    function preference($preference_name, bool $default = null)
184
    {
185
        $valid_preference_name = strtolower(str_replace(' ', '_', $preference_name));
186
        if (auth()->check()) {
187
            $preference = auth()->user()->preferences()->where('preference', $valid_preference_name)->first();
188
189
            return isset($preference) ? $preference->pivot->enabled : ($default ?? null);
190
        } else {
191
            return null;
192
        }
193
    }
194
}
195
196
if (! function_exists('deleteImage')) {
197
    function deleteImage($image)
198
    {
199
        $image ? (\Illuminate\Support\Facades\File::exists(public_path('storage/'.$image)) ? \Illuminate\Support\Facades\File::delete(public_path('storage/'.$image)) : '') : '';
200
    }
201
}
202
203
if (! function_exists('api_paginate_limit')) {
204
    function api_paginate_limit($default = null)
205
    {
206
        return setting('api_paginate_limit', $default ?? config('adminetic.api_paginate_limit', 10));
207
    }
208
}
209
210
if (! function_exists('api_collection_return_paginate')) {
211
    function api_collection_return_paginate()
212
    {
213
        return setting('api_collection_return_paginate', config('adminetic.api_collection_return_paginate', true));
214
    }
215
}
216
217
if (! function_exists('darkMode')) {
218
    function darkMode()
219
    {
220
        return setting('dark_mode', config('adminetic.dark_mode', false));
221
    }
222
}
223
224
if (! function_exists('getCondition')) {
225
    function getCondition($conditions)
226
    {
227
        $result = null;
228
        if (! isset($conditions)) {
229
            return false;
230
        }
231
232
        if (count($conditions) > 0) {
233
            if (count($conditions) == 1) {
234
                return $conditions[0]['condition'];
235
            } else {
236
                foreach ($conditions as $condition) {
237
                    if (isset($condition['type']) && isset($condition['condition'])) {
238
                        if ($condition['type'] == 'or' || $condition['type'] == 'Or' || $condition['type'] == 'OR' || $condition['type'] == '||') {
239
                            $result = $result || $condition['condition'];
240
                        } elseif ($condition['type'] == 'and' || $condition['type'] == 'And' || $condition['type'] == 'AND' || $condition['type'] == '&&') {
241
                            $result = $result && $condition['condition'];
0 ignored issues
show
introduced by
$result is of type null, thus it always evaluated to false.
Loading history...
242
                        }
243
                    }
244
                }
245
            }
246
247
            return $result;
248
        } else {
249
            return false;
250
        }
251
    }
252
}
253
254
if (! function_exists('getImg')) {
255
    function getImg($img, $default)
256
    {
257
        if (isset($img)) {
258
            if (file_exists(public_path('storage/'.$img))) {
259
                return asset('storage/'.$img);
260
            } elseif (file_exists(public_path($img))) {
261
                return asset($img);
262
            } else {
263
                return asset($default);
264
            }
265
        } else {
266
            return asset($default);
267
        }
268
    }
269
}
270
271
if (! function_exists('putContentToClassFunction')) {
272
    function putContentToClassFunction($file, $function_name, $data, $closing_token = '}')
273
    {
274
        $data = $data."\n";
275
        // Read the contents of the file into a string
276
        $contents = file_get_contents($file);
277
278
        // Find the position of the function definition within the class
279
        $start_pos = strpos($contents, "$function_name");
280
        if ($start_pos === false) {
281
            exit('Function not found');
0 ignored issues
show
Best Practice introduced by
Using exit here is not recommended.

In general, usage of exit should be done with care and only when running in a scripting context like a CLI script.

Loading history...
282
        }
283
284
        // Find the position of the closing brace of the function
285
        $end_pos = strpos($contents, $closing_token, $start_pos);
286
        if ($end_pos === false) {
287
            exit('Function not properly defined');
0 ignored issues
show
Best Practice introduced by
Using exit here is not recommended.

In general, usage of exit should be done with care and only when running in a scripting context like a CLI script.

Loading history...
288
        }
289
290
        // Extract the function definition from the class
291
        $function_definition = substr($contents, $start_pos, $end_pos - $start_pos + 1);
292
293
        // Append the new content to the function definition
294
        $modified_function_definition = rtrim($function_definition, $closing_token).$data.$closing_token;
295
296
        // Replace the original function definition with the modified one in the class definition
297
        $modified_contents = substr_replace($contents, $modified_function_definition, $start_pos, $end_pos - $start_pos + 1);
298
299
        // Write the modified string back to the file
300
        file_put_contents($file, $modified_contents);
301
302
        return true;
303
    }
304
}
305
306
if (! function_exists('checkIfFieldIsRequired')) {
307
    function checkIfFieldIsRequired($table_name, $field)
308
    {
309
        $isNullable = null;
310
        $table = Schema::getConnection()->getDoctrineSchemaManager()->listTableDetails($table_name);
0 ignored issues
show
Deprecated Code introduced by
The function Doctrine\DBAL\Schema\Abs...ger::listTableDetails() has been deprecated: Use {@see introspectTable()} instead. ( Ignorable by Annotation )

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

310
        $table = /** @scrutinizer ignore-deprecated */ Schema::getConnection()->getDoctrineSchemaManager()->listTableDetails($table_name);

This function has been deprecated. The supplier of the function has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.

Loading history...
311
312
        foreach ($table->getColumns() as $column) {
313
            if ($column->getName() === $field) {
314
                $isNullable = $column->getNotnull() === false;
315
                break;
316
            }
317
        }
318
319
        return $isNullable;
320
    }
321
}
322
323
if (! function_exists('label')) {
324
    function label($table, $field, $default_label = null)
325
    {
326
        $display_name = $default_label ?? Str::ucfirst(str_replace('_', ' ', $field));
327
        if (! checkIfFieldIsRequired($table, $field)) {
328
            // do something if phone_number is nullable
329
            echo "{$display_name} <span class='text-danger'>*</span>";
330
        } else {
331
            echo $display_name;
332
        }
333
    }
334
}
335
336
if (! function_exists('role_theme')) {
337
    function role_theme()
338
    {
339
        $role = auth()->check() ? auth()->user()->roles->first()->name : null;
340
        $role_theme = config('adminetic.role_theme', null);
341
        if (! is_null($role_theme)) {
342
            return isset($role_theme[$role]) ? $role_theme[$role] : 'compact-sidebar';
343
        }
344
345
        return 'compact-sidebar';
346
    }
347
}
348
349
if (! function_exists('setEnvValue')) {
350
    function setEnvValue(array $values)
351
    {
352
        $envFile = app()->environmentFilePath();
0 ignored issues
show
introduced by
The method environmentFilePath() does not exist on Illuminate\Container\Container. Are you sure you never get this type here, but always one of the subclasses? ( Ignorable by Annotation )

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

352
        $envFile = app()->/** @scrutinizer ignore-call */ environmentFilePath();
Loading history...
353
        $str = file_get_contents($envFile);
354
355
        if (count($values) > 0) {
356
            foreach ($values as $envKey => $envValue) {
357
                $str .= "\n"; // In case the searched variable is in the last line without \n
358
                $keyPosition = strpos($str, "{$envKey}=");
359
                $endOfLinePosition = strpos($str, "\n", $keyPosition);
360
                $oldLine = substr($str, $keyPosition, $endOfLinePosition - $keyPosition);
361
362
                // If key does not exist, add it
363
                if (! $keyPosition || ! $endOfLinePosition || ! $oldLine) {
364
                    $str .= "{$envKey}={$envValue}\n";
365
                } else {
366
                    $str = str_replace($oldLine, "{$envKey}={$envValue}", $str);
367
                }
368
            }
369
        }
370
371
        $str = substr($str, 0, -1);
372
        if (! file_put_contents($envFile, $str)) {
373
            return false;
374
        }
375
376
        return true;
377
    }
378
}
379