Passed
Push — master ( afb865...5313f5 )
by Reza
03:19
created

getCrudConfig()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 14
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 7
c 0
b 0
f 0
nc 4
nop 1
dl 0
loc 14
rs 10
1
<?php
2
3
use EasyPanel\Contracts\CRUDComponent;
4
5
if(! function_exists('getRouteName')) {
6
    function getRouteName(){
7
        $routeName = config('easy_panel.route_prefix');
8
        $routeName = trim($routeName, '/');
9
        $routeName = str_replace('/', '.', $routeName);
10
        return $routeName;
11
    }
12
}
13
14
if(! function_exists('getCrudConfig')) {
15
    function getCrudConfig($name){
16
        $namespace = "\\App\\CRUD\\{$name}Component";
17
18
        if (!class_exists($namespace)){
19
            abort(403, "Class with {$namespace} namespace doesn't exist");
20
        }
21
22
        $instance = app()->make($namespace);
23
24
        if (!$instance instanceof CRUDComponent){
25
            abort(403, "{$namespace} should implement CRUDComponent interface");
26
        }
27
28
        return $instance;
29
    }
30
}
31
32
if(! function_exists('get_icon')) {
33
    function get_icon($type){
34
        $array = [
35
            'file-text' => ['posts', 'article', 'stories', 'post', 'articles', 'story'],
36
            'users' => ['users', 'user', 'accounts', 'account', 'admins', 'admin', 'employee', 'employees'],
37
            'file' => ['files', 'file'],
38
            'mic' => ['episode', 'episodes', 'voices', 'voice'],
39
            'book' => ['book', 'books'],
40
            'tag' => ['tag', 'tags'],
41
            'bookmark' => ['bookmarks', 'bookmark'],
42
            'heart' => ['likes', 'like', 'favorite', 'favorites'],
43
            'music' => ['musics', 'music', 'audios', 'audio'],
44
            'bell' => ['notifications', 'notification'],
45
            'layers' => ['request', 'requests'],
46
            'settings' => ['settings', 'setting'],
47
            'truck' => ['product', 'products', 'shops', 'shop'],
48
            'message-circle' => ['comments', 'messages', 'pm', 'comment', 'message', 'chats', 'chat'],
49
        ];
50
        foreach ($array as $key => $arrayValues){
51
            if(in_array($type, $arrayValues)){
52
                $val = $key;
53
            }
54
        }
55
        return $val ?? 'grid';
56
    }
57
}
58
59
if(! function_exists('registerActionRoutes')){
60
    function registerActionRoutes($prefix, $component, $crudConfig)
61
    {
62
        Route::prefix($prefix)->name("$prefix.")->group(function () use ($component, $crudConfig) {
63
64
            if(@class_exists("$component\\Read")) {
65
                Route::get('/', "$component\\Read")->name('read');
66
            }
67
68
            if (@$crudConfig->create and @class_exists("$component\\Create")) {
69
                Route::get('/create', "$component\\Create")->name('create');
70
            }
71
72
            if (@$crudConfig->update and @class_exists("$component\\Update")) {
73
                Route::get('/update/{' . $action . '}', "$component\\Update")->name('update');
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $action seems to be never defined.
Loading history...
74
            }
75
76
        });
77
    }
78
}
79