Passed
Push — master ( 1ed4cb...574031 )
by Reza
10:56
created

Create::getModels()   B

Complexity

Conditions 8
Paths 12

Size

Total Lines 19
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 8
eloc 11
nc 12
nop 1
dl 0
loc 19
rs 8.4444
c 1
b 0
f 0
1
<?php
2
3
namespace EasyPanel\Http\Livewire\CRUD;
4
5
use Livewire\Component;
6
use EasyPanel\Models\CRUD;
7
use Illuminate\Database\Eloquent\Model;
8
use Illuminate\Support\Facades\Artisan;
9
use Illuminate\Support\Facades\File;
10
use Illuminate\Support\Str;
11
12
class Create extends Component
13
{
14
    public $model;
15
    public $route;
16
    public $models;
17
    public $dropdown;
18
    protected $listeners = ['closeModal'];
19
20
    protected $rules = [
21
        'model' => 'required|min:8|unique:cruds',
22
        'route' => 'required|min:2|unique:cruds',
23
    ];
24
25
    public function closeModal()
26
    {
27
        $this->hideDropdown();
28
    }
29
30
    public function setModel()
31
    {
32
        $this->models = $this->getModels();
33
        $this->showDropdown();
34
    }
35
36
    public function setSuggestedModel($key)
37
    {
38
        $this->model = $this->models[$key];
39
        $this->route = Str::lower($this->getModelName($this->model));
40
        $this->hideDropdown();
41
    }
42
43
    public function updatedModel($value)
44
    {
45
        $value = $value == '' ? null : $value;
46
        $this->models = $this->getModels($value);
47
        $this->showDropdown();
48
    }
49
50
    public function hideDropdown()
51
    {
52
        $this->dropdown = false;
53
    }
54
55
    public function showDropdown()
56
    {
57
        $this->dropdown = true;
58
    }
59
60
    public function create()
61
    {
62
        $this->validate();
63
64
        if (!class_exists($this->model) or ! app()->make($this->model) instanceof Model){
65
            $this->addError('model', __('Namespace is invalid'));
66
67
            return;
68
        }
69
70
        if (!preg_match('/^([a-z]|[0-9])+/', $this->route)){
71
            $this->addError('route', __('Route is invalid'));
72
73
            return;
74
        }
75
76
        try{
77
            $name = strtolower($this->getModelName($this->model));
78
            CRUD::create([
79
                'model' => trim($this->model, '\\'),
80
                'name' => $name,
81
                'route' => trim($this->route, '\\'),
82
            ]);
83
84
            Artisan::call('panel:config', [
85
                'name' => $name,
86
                '--force' => true
87
            ]);
88
89
            $this->dispatchBrowserEvent('show-message', ['type' => 'success', 'message' => __('CreatedMessage', ['name' => __('CRUD') ])]);
90
        } catch(\Exception $exception){
91
            $this->dispatchBrowserEvent('show-message', ['type' => 'error', 'message' => __('UnknownError') ]);
92
        }
93
94
95
        $this->emit('crudUpdated');
96
        $this->reset();
97
    }
98
99
    public function render()
100
    {
101
        return view('admin::livewire.crud.create')
102
            ->layout('admin::layouts.app');
103
    }
104
105
    private function getModelName($model){
106
        $model = explode('\\', $model);
107
108
        return end($model);
109
    }
110
111
    private function getModels($query = null)
112
    {
113
        $files = File::exists(app_path('/Models')) ? File::files(app_path('/Models')) : File::allFiles(app_path('/'));
114
        $array = [];
115
        foreach ($files as $file) {
116
            if (!Str::contains($file->getFilename(), '.php') or (!is_null($query) and !Str::contains(Str::lower($file->getFilename()), Str::lower($query)))){
117
                continue;
118
            }
119
120
            $namespace = File::exists(app_path('/Models')) ? "App\\Models" : "\\App";
121
            $fileName = str_replace('.php', null, $file->getFilename());
122
            $fullNamespace =  $namespace."\\{$fileName}";
123
124
            if (app()->make($fullNamespace) instanceof Model) {
125
                $array[] = $fullNamespace;
126
            }
127
        }
128
129
        return $array;
130
    }
131
}
132