Remover::handle()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 31
Code Lines 21

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 3
eloc 21
nc 3
nop 0
dl 0
loc 31
rs 9.584
c 1
b 0
f 1
1
<?php
2
/**
3
 * Created by PhpStorm.
4
 * User: Shamaseen
5
 * Date: 10/08/19
6
 * Time: 04:48 م
7
 */
8
9
namespace Shamaseen\Repository\Generator\Commands;
10
11
use FilesystemIterator;
12
use Illuminate\Console\Command;
13
use Illuminate\Support\Facades\Config;
14
use Illuminate\Support\Str;
15
16
class Remover extends Command
17
{
18
    /**
19
     * The name and signature of the console command.
20
     *
21
     * @var string
22
     */
23
    protected $signature = 'remove:repository
24
    {name : Class (singular) for example User} {--only-view}';
25
26
    /**
27
     * The console command description.
28
     *
29
     * @var string
30
     */
31
    protected $description = 'Remove repository files';
32
33
    /**
34
     * The repository name.
35
     *
36
     * @var string
37
     */
38
    protected $repoName;
39
40
    /**
41
     * Create a new command instance.
42
     */
43
    public function __construct()
44
    {
45
        parent::__construct();
46
    }
47
48
    /**
49
     * Execute the console command.
50
     *
51
     */
52
    public function handle()
53
    {
54
        $file = preg_split(' (/|\\\\) ', (string)$this->argument('name')) ?? [];
55
56
        if (!$file) {
57
            return 'Something wrong with the inputs !';
58
        }
59
60
        $this->repoName = $file[count($file) - 1];
61
62
        unset($file[count($file) - 1]);
63
        $path = implode('\\', $file);
64
65
        if (!$this->confirm('This will delete ' . $this->repoName . ' files and folder, Do you want to continue ?')) {
66
            return false;
67
        }
68
69
        $model = Str::plural(Config::get('repository.model'));
70
        $interface = Str::plural(Config::get('repository.interface'));
71
        $repository = Str::plural(Config::get('repository.repository'));
72
        $controllerFolder = Config::get('repository.controllers_folder');
73
        $requestFolder = Config::get('repository.requests_folder');
74
        $resourceFolder = Config::get('repository.resources_folder');
75
76
        $this->remove('Entity', $model, $path);
77
        $this->remove('Controller', $controllerFolder, $path);
78
        $this->remove('Resource', $resourceFolder, $path);
79
        $this->remove('Request', $requestFolder, $path);
80
        $this->remove('Repository', $repository, $path);
81
        $this->remove('Interface', $interface, $path);
82
        return true;
83
    }
84
85
    public function remove($type, $folder, $relativePath): bool
86
    {
87
        $folder = str_replace('\\', '/', $folder);
88
        $relativePath = str_replace('\\', '/', $relativePath);
89
90
        switch ($type) {
91
            case 'Entity':
92
                $filePath = Config::get('repository.app_path') . "/$folder/$relativePath/";
93
                $fileName = "$this->repoName.php";
94
                break;
95
            case 'Controller':
96
            case 'Request':
97
            case 'Resource':
98
            case 'Repository':
99
            case 'Interface':
100
            default:
101
                $filePath = Config::get('repository.app_path') . "/$folder/$relativePath/";
102
                $fileName = "$this->repoName$type.php";
103
        }
104
        if (!is_file($filePath . $fileName)) {
105
            $this->warn($filePath . $fileName . ' is not a valid file');
106
            return false;
107
        }
108
109
        unlink($filePath . $fileName);
110
        if (!(new FilesystemIterator($filePath))->valid()) {
111
            rmdir($filePath);
112
        }
113
        return true;
114
    }
115
}
116