Passed
Push — main ( bfff7a...f85971 )
by PRATIK
13:07
created

putContentToClassFunction()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 31
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 13
nc 3
nop 4
dl 0
loc 31
rs 9.8333
c 0
b 0
f 0
1
<?php
2
3
use App\Events\PushNotificationEvent;
0 ignored issues
show
Bug introduced by
The type App\Events\PushNotificationEvent was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
4
use App\Events\GeneralPushNotificationEvent;
0 ignored issues
show
Bug introduced by
The type App\Events\GeneralPushNotificationEvent was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
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('adminetic/static/placeholder.png');
54
    }
55
}
56
57
if (!function_exists('getVerticalImagePlaceholder')) {
58
    function getVerticalImagePlaceholder()
59
    {
60
        return asset('adminetic/static/vertical_placeholder.jpg');
61
    }
62
}
63
64
if (!function_exists('getSliderPlaceholder')) {
65
    function getSliderPlaceholder()
66
    {
67
        return asset('adminetic/static/slider.jpg');
68
    }
69
}
70
71
if (!function_exists('getFoodImagePlaceholder')) {
72
    function getFoodImagePlaceholder()
73
    {
74
        return asset('adminetic/static/food_placeholder.jpg');
75
    }
76
}
77
78
if (!function_exists('getProfilePlaceholder')) {
79
    function getProfilePlaceholder($p = null)
80
    {
81
        $profile = $p ?? Auth::user()->profile ?? Auth::user()->profile()->create();
0 ignored issues
show
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

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

298
        $envFile = app()->/** @scrutinizer ignore-call */ environmentFilePath();
Loading history...
299
        $str = file_get_contents($envFile);
300
301
        if (count($values) > 0) {
302
            foreach ($values as $envKey => $envValue) {
303
304
                $str .= "\n"; // In case the searched variable is in the last line without \n
305
                $keyPosition = strpos($str, "{$envKey}=");
306
                $endOfLinePosition = strpos($str, "\n", $keyPosition);
307
                $oldLine = substr($str, $keyPosition, $endOfLinePosition - $keyPosition);
308
309
                // If key does not exist, add it
310
                if (!$keyPosition || !$endOfLinePosition || !$oldLine) {
311
                    $str .= "{$envKey}={$envValue}\n";
312
                } else {
313
                    $str = str_replace($oldLine, "{$envKey}={$envValue}", $str);
314
                }
315
            }
316
        }
317
318
        $str = substr($str, 0, -1);
319
        if (!file_put_contents($envFile, $str)) return false;
320
        return true;
321
    }
322
}
323