DeleteCRUD::handle()   A
last analyzed

Complexity

Conditions 6
Paths 5

Size

Total Lines 22
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 6
eloc 15
c 1
b 0
f 0
nc 5
nop 0
dl 0
loc 22
rs 9.2222
1
<?php
2
3
namespace EasyPanel\Commands\Actions;
4
5
use Illuminate\Console\Command;
6
use Illuminate\Support\Facades\File;
7
use Symfony\Component\Console\Exception\CommandNotFoundException;
8
use EasyPanel\Models\CRUD;
9
10
class DeleteCRUD extends Command
11
{
12
13
    protected $signature = 'panel:delete {name?} {--force : Force mode}';
14
    protected $description = 'Create all action for CRUDs';
15
16
    public function handle()
17
    {
18
        $names = (array) $this->argument('name') ?: CRUD::query()->where('active', true)->pluck('name')->toArray();
19
20
        if($names == null) {
21
            throw new CommandNotFoundException("There is no action in database");
22
        }
23
24
        foreach ($names as $name) {
25
            if (!in_array($name, CRUD::query()->pluck('name')->toArray())) {
26
                $this->line("$name does not exist in config file");
27
                continue;
28
            }
29
30
            if ($this->askResult($name)) {
31
                File::deleteDirectory(resource_path("/views/livewire/admin/$name"));
32
                File::deleteDirectory(app_path("/Http/Livewire/Admin/" . ucfirst($name)));
33
                File::delete(app_path("/CRUD/".ucfirst($name)."Component.php"));
34
                $this->info("{$name} files were deleted, make sure you will delete {$name} value from actions in config");
35
                CRUD::query()->where('name', $name)->delete();
36
            } else {
37
                $this->line("process for {$name} action was canceled.");
38
            }
39
40
        }
41
    }
42
43
    public function askResult($name)
44
    {
45
        if($this->option('force')) {
46
            return true;
47
        }
48
        $result = $this->confirm("Do you really want to delete {$name} files ?", true);
49
        return $result;
50
    }
51
52
}
53