Passed
Push — main ( 6259dd...fa2223 )
by PRATIK
03:48
created

logoBanner()   A

Complexity

Conditions 2
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 1
nc 1
nop 0
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php
2
3
use Pratiksh\Adminetic\Models\Admin\Setting;
4
5
if (!function_exists('spa')) {
6
    function spa()
7
    {
8
        return setting('spa', config('adminetic.spa', true));
9
    }
10
}
11
12
if (!function_exists('getClassesList')) {
13
    function getClassesList($dir)
14
    {
15
        $classes = \File::allFiles($dir);
16
        foreach ($classes as $class) {
17
            $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...
18
                [app_path(), '/', '.php'],
19
                ['App', '\\', ''],
20
                $class->getRealPath()
21
            );
22
        }
23
24
        return $classes;
25
    }
26
}
27
if (!function_exists('getAllModelNames')) {
28
    function getAllModelNames($dir)
29
    {
30
        $modelNames = [];
31
        $models = getClassesList($dir);
32
        foreach ($models as $model) {
33
            $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...
34
            $modelNames[] = end($model_name);
35
        }
36
37
        return $modelNames;
38
    }
39
}
40
41
if (!function_exists('validImageFolder')) {
42
    function validImageFolder($name, $default = 'default')
43
    {
44
        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...
45
    }
46
}
47
48
if (!function_exists('getImagePlaceholder')) {
49
    function getImagePlaceholder()
50
    {
51
        return asset('adminetic/static/placeholder.png');
52
    }
53
}
54
55
if (!function_exists('getVerticalImagePlaceholder')) {
56
    function getVerticalImagePlaceholder()
57
    {
58
        return asset('adminetic/static/vertical_placeholder.jpg');
59
    }
60
}
61
62
if (!function_exists('getSliderPlaceholder')) {
63
    function getSliderPlaceholder()
64
    {
65
        return asset('adminetic/static/slider.jpg');
66
    }
67
}
68
69
if (!function_exists('getFoodImagePlaceholder')) {
70
    function getFoodImagePlaceholder()
71
    {
72
        return asset('adminetic/static/food_placeholder.jpg');
73
    }
74
}
75
76
if (!function_exists('getProfilePlaceholder')) {
77
    function getProfilePlaceholder($p = null)
78
    {
79
        $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

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

308
        $envFile = app()->/** @scrutinizer ignore-call */ environmentFilePath();
Loading history...
309
        $str = file_get_contents($envFile);
310
311
        if (count($values) > 0) {
312
            foreach ($values as $envKey => $envValue) {
313
                $str .= "\n"; // In case the searched variable is in the last line without \n
314
                $keyPosition = strpos($str, "{$envKey}=");
315
                $endOfLinePosition = strpos($str, "\n", $keyPosition);
316
                $oldLine = substr($str, $keyPosition, $endOfLinePosition - $keyPosition);
317
318
                // If key does not exist, add it
319
                if (!$keyPosition || !$endOfLinePosition || !$oldLine) {
320
                    $str .= "{$envKey}={$envValue}\n";
321
                } else {
322
                    $str = str_replace($oldLine, "{$envKey}={$envValue}", $str);
323
                }
324
            }
325
        }
326
327
        $str = substr($str, 0, -1);
328
        if (!file_put_contents($envFile, $str)) {
329
            return false;
330
        }
331
332
        return true;
333
    }
334
}
335