Remover::handle()   A
last analyzed

Complexity

Conditions 4
Paths 4

Size

Total Lines 30
Code Lines 19

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 4
eloc 19
c 1
b 0
f 0
nc 4
nop 0
dl 0
loc 30
rs 9.6333
1
<?php
2
3
namespace Shamaseen\Repository\Commands;
4
5
use FilesystemIterator;
6
use Illuminate\Console\Command;
7
use Illuminate\Support\Facades\Config;
8
use Shamaseen\Generator\Ungenerator;
9
use Shamaseen\Repository\Events\RepositoryFilesRemoved;
10
use Shamaseen\Repository\PathResolver;
11
12
class Remover extends Command
13
{
14
    /**
15
     * The name and signature of the console command.
16
     *
17
     * @var string
18
     */
19
    protected $signature = 'ungenerate:repository
20
    {name : Class (singular) for example User}
21
    {--base= : Base path to inject the files\folders in}
22
    {--f|force : force deleting the files}';
23
24
    /**
25
     * The console command description.
26
     *
27
     * @var string
28
     */
29
    protected $description = 'Remove repository files';
30
31
    protected array $userPaths;
32
    protected string $modelName;
33
    protected string $userPath;
34
    protected string $basePath;
35
    private Ungenerator $ungenerator;
36
    private PathResolver $pathResolver;
37
38
    /**
39
     * Create a new command instance.
40
     */
41
    public function __construct(Ungenerator $ungenerator)
42
    {
43
        parent::__construct();
44
        $this->ungenerator = $ungenerator;
45
    }
46
47
    /**
48
     * Execute the console command.
49
     */
50
    public function handle(): int
51
    {
52
        $this->userPaths = preg_split(' ([/\\\]) ', $this->argument('name'));
0 ignored issues
show
Bug introduced by
It seems like $this->argument('name') can also be of type array; however, parameter $subject of preg_split() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

52
        $this->userPaths = preg_split(' ([/\\\]) ', /** @scrutinizer ignore-type */ $this->argument('name'));
Loading history...
53
54
        $this->modelName = array_pop($this->userPaths);
55
        $this->userPath = implode('/', str_replace('\\', '/', $this->userPaths));
56
        $this->basePath = $this->option('base') ? $this->option('base') : Config::get('repository.base_path');
57
58
        $this->pathResolver = new PathResolver($this->modelName, $this->userPath, $this->basePath);
59
        // Configure the generator
60
        config(['generator.base_path' => base_path($this->basePath)]);
61
62
        if (!$this->option('force') && !$this->confirm('This will delete '.$this->modelName.' files and folder, Do you want to continue ?')) {
63
            return false;
0 ignored issues
show
Bug Best Practice introduced by
The expression return false returns the type false which is incompatible with the type-hinted return integer.
Loading history...
64
        }
65
66
        $this->remove('Controller');
67
        $this->remove('Model');
68
        $this->remove('Request');
69
        $this->remove('Repository');
70
        $this->remove('Resource');
71
        $this->remove('Collection');
72
        $this->remove('Policy');
73
        $this->remove('Test');
74
75
        RepositoryFilesRemoved::dispatch($this->basePath, $this->userPath, $this->modelName);
76
77
        $this->dumpAutoload();
78
79
        return self::SUCCESS;
80
    }
81
82
    public function remove($type): bool
83
    {
84
        $fileToDelete = $this->pathResolver->outputPath($type);
85
86
        if (is_file($this->ungenerator->absolutePath($fileToDelete))) {
87
            $this->ungenerator->output($fileToDelete);
88
        }
89
90
        $pathsToDelete = count($this->userPaths);
91
        for ($i = 0; $i < $pathsToDelete; ++$i) {
92
            $pathFromBase = $this->ungenerator->absolutePath(
93
                $this->pathResolver->directionFromBase($type)
94
            );
95
96
            if (is_dir($pathFromBase) && !(new FilesystemIterator($pathFromBase))->valid()) {
97
                rmdir($pathFromBase);
98
            }
99
        }
100
101
        return true;
102
    }
103
104
    private function dumpAutoload()
105
    {
106
        shell_exec('composer dump-autoload');
107
    }
108
}
109