Passed
Push — main ( 8963f0...f48e40 )
by PRATIK
12:38
created

getFoodImagePlaceholder()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 5
Bugs 1 Features 1
Metric Value
cc 1
eloc 1
c 5
b 1
f 1
nc 1
nop 0
dl 0
loc 3
rs 10
1
<?php
2
3
use Illuminate\Support\Facades\Schema;
4
use Illuminate\Support\Str;
5
use Pratiksh\Adminetic\Models\Admin\Setting;
6
7
if (!function_exists('spa')) {
8
    function spa()
9
    {
10
        return setting('spa', config('adminetic.spa', true));
11
    }
12
}
13
14
if (!function_exists('getClassesList')) {
15
    function getClassesList($dir)
16
    {
17
        $classes = \File::allFiles($dir);
18
        foreach ($classes as $class) {
19
            $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...
20
                [app_path(), '/', '.php'],
21
                ['App', '\\', ''],
22
                $class->getRealPath()
23
            );
24
        }
25
26
        return $classes;
27
    }
28
}
29
if (!function_exists('getAllModelNames')) {
30
    function getAllModelNames($dir)
31
    {
32
        $modelNames = [];
33
        $models = getClassesList($dir);
34
        foreach ($models as $model) {
35
            $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...
36
            $modelNames[] = end($model_name);
37
        }
38
39
        return $modelNames;
40
    }
41
}
42
43
if (!function_exists('validImageFolder')) {
44
    function validImageFolder($name, $default = 'default')
45
    {
46
        return strtolower(str_replace([' ', '-', '$', '<', '>', '&', '{', $closing_token, '*', '\\', '/', ':', '.', ';', ',', "'", '"'], '_', $name ?? trim($default)));
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $closing_token seems to be never defined.
Loading history...
47
    }
48
}
49
50
if (!function_exists('getImagePlaceholder')) {
51
    function getImagePlaceholder()
52
    {
53
        return asset(config('adminetic.default_image_placeholder' . 'adminetic/static/placeholder.png'));
54
    }
55
}
56
57
if (!function_exists('getVerticalImagePlaceholder')) {
58
    function getVerticalImagePlaceholder()
59
    {
60
        return asset(config('adminetic.vertical_image_placeholder', 'adminetic/static/vertical_placeholder.jpg'));
61
    }
62
}
63
64
if (!function_exists('getSliderPlaceholder')) {
65
    function getSliderPlaceholder()
66
    {
67
        return asset(config('adminetic.slider_placeholder', 'adminetic/static/slider.jpg'));
68
    }
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'));
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'));
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'));
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'));
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'));
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
        $setting = Setting::where('setting_name', $valid_setting_name)->first();
170
171
        return isset($setting->value) ? $setting->value : ($default ?? null);
172
    }
173
}
174
175
if (!function_exists('preference')) {
176
    function preference($preference_name, bool $default = null)
177
    {
178
        $valid_preference_name = strtolower(str_replace(' ', '_', $preference_name));
179
        if (auth()->check()) {
180
            $preference = auth()->user()->preferences()->where('preference', $valid_preference_name)->first();
181
182
            return isset($preference) ? $preference->pivot->enabled : ($default ?? null);
183
        } else {
184
            return null;
185
        }
186
    }
187
}
188
189
if (!function_exists('deleteImage')) {
190
    function deleteImage($image)
191
    {
192
        $image ? (\Illuminate\Support\Facades\File::exists(public_path('storage/' . $image)) ? \Illuminate\Support\Facades\File::delete(public_path('storage/' . $image)) : '') : '';
193
    }
194
}
195
196
if (!function_exists('api_paginate_limit')) {
197
    function api_paginate_limit($default = null)
198
    {
199
        return setting('api_paginate_limit', $default ?? config('adminetic.api_paginate_limit', 10));
200
    }
201
}
202
203
if (!function_exists('api_collection_return_paginate')) {
204
    function api_collection_return_paginate()
205
    {
206
        return setting('api_collection_return_paginate', config('adminetic.api_collection_return_paginate', true));
207
    }
208
}
209
210
if (!function_exists('darkMode')) {
211
    function darkMode()
212
    {
213
        return setting('dark_mode', config('adminetic.dark_mode', false));
214
    }
215
}
216
217
if (!function_exists('getCondition')) {
218
    function getCondition($conditions)
219
    {
220
        $result = null;
221
        if (!isset($conditions)) {
222
            return false;
223
        }
224
225
        if (count($conditions) > 0) {
226
            if (count($conditions) == 1) {
227
                return $conditions[0]['condition'];
228
            } else {
229
                foreach ($conditions as $condition) {
230
                    if (isset($condition['type']) && isset($condition['condition'])) {
231
                        if ($condition['type'] == 'or' || $condition['type'] == 'Or' || $condition['type'] == 'OR' || $condition['type'] == '||') {
232
                            $result = $result || $condition['condition'];
233
                        } elseif ($condition['type'] == 'and' || $condition['type'] == 'And' || $condition['type'] == 'AND' || $condition['type'] == '&&') {
234
                            $result = $result && $condition['condition'];
0 ignored issues
show
introduced by
$result is of type null, thus it always evaluated to false.
Loading history...
235
                        }
236
                    }
237
                }
238
            }
239
240
            return $result;
241
        } else {
242
            return false;
243
        }
244
    }
245
}
246
247
if (!function_exists('getImg')) {
248
    function getImg($img, $default)
249
    {
250
        if (isset($img)) {
251
            if (file_exists(public_path('storage/' . $img))) {
252
                return asset('storage/' . $img);
253
            } elseif (file_exists(public_path($img))) {
254
                return asset($img);
255
            } else {
256
                return asset($default);
257
            }
258
        } else {
259
            return asset($default);
260
        }
261
    }
262
}
263
264
if (!function_exists('putContentToClassFunction')) {
265
    function putContentToClassFunction($file, $function_name, $data, $closing_token = '}')
266
    {
267
        $data = $data . "\n";
268
        // Read the contents of the file into a string
269
        $contents = file_get_contents($file);
270
271
        // Find the position of the function definition within the class
272
        $start_pos = strpos($contents, "$function_name");
273
        if ($start_pos === false) {
274
            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...
275
        }
276
277
        // Find the position of the closing brace of the function
278
        $end_pos = strpos($contents, $closing_token, $start_pos);
279
        if ($end_pos === false) {
280
            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...
281
        }
282
283
        // Extract the function definition from the class
284
        $function_definition = substr($contents, $start_pos, $end_pos - $start_pos + 1);
285
286
        // Append the new content to the function definition
287
        $modified_function_definition = rtrim($function_definition, $closing_token) . $data . $closing_token;
288
289
        // Replace the original function definition with the modified one in the class definition
290
        $modified_contents = substr_replace($contents, $modified_function_definition, $start_pos, $end_pos - $start_pos + 1);
291
292
        // Write the modified string back to the file
293
        file_put_contents($file, $modified_contents);
294
295
        return true;
296
    }
297
}
298
299
if (!function_exists('checkIfFieldIsRequired')) {
300
    function checkIfFieldIsRequired($table_name, $field)
301
    {
302
        $isNullable = null;
303
        $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

303
        $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...
304
305
        foreach ($table->getColumns() as $column) {
306
            if ($column->getName() === $field) {
307
                $isNullable = $column->getNotnull() === false;
308
                break;
309
            }
310
        }
311
312
        return $isNullable;
313
    }
314
}
315
316
if (!function_exists('label')) {
317
    function label($table, $field, $default_label = null)
318
    {
319
        $display_name = $default_label ?? Str::ucfirst(str_replace('_', ' ', $field));
320
        if (!checkIfFieldIsRequired($table, $field)) {
321
            // do something if phone_number is nullable
322
            echo "{$display_name} <span class='text-danger'>*</span>";
323
        } else {
324
            echo $display_name;
325
        }
326
    }
327
}
328
329
if (!function_exists('role_theme')) {
330
    function role_theme()
331
    {
332
        $role = auth()->check() ? auth()->user()->roles->first()->name : null;
333
        $role_theme = config('adminetic.role_theme', null);
334
        if (!is_null($role_theme)) {
335
            return isset($role_theme[$role]) ? $role_theme[$role] : 'compact-sidebar';
336
        }
337
338
        return 'compact-sidebar';
339
    }
340
}
341
342
if (!function_exists('setEnvValue')) {
343
    function setEnvValue(array $values)
344
    {
345
        $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

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