Test Failed
Push — master ( c099c4...77bf15 )
by Stephen
03:55 queued 36s
created

CruiseUninstall::deleteFileTree()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 5
c 0
b 0
f 0
dl 0
loc 9
rs 10
cc 3
nc 3
nop 1
1
<?php
2
3
namespace Sfneal\Cruise\Commands;
4
5
use Illuminate\Console\Command;
6
use Illuminate\Support\Facades\File;
7
use Symfony\Component\Process\Process;
8
9
class CruiseUninstall extends Command
10
{
11
    /**
12
     * The name and signature of the console command.
13
     *
14
     * @var string
15
     */
16
    protected $signature = 'cruise:uninstall';
17
18
    /**
19
     * The console command description.
20
     *
21
     * @var string
22
     */
23
    protected $description = 'Uninstall the cruise package and remove publish files';
24
25
    /**
26
     * Execute the console command.
27
     */
28
    public function handle(): int
29
    {
30
        // Remove config
31
        if (File::exists(config_path('cruise.php'))) {
32
            File::delete(config_path('cruise.php'));
33
            $this->info('Removed cruise config files');
34
        }
35
36
        // Remove docker files
37
        $docker_files = [
38
            'changelog.txt',
39
            'docker-compose.yml',
40
            'docker-compose-dev.yml',
41
            'docker-compose-dev-db.yml',
42
            'docker-compose-dev-node.yml',
43
            'docker-compose-tests.yml',
44
            'Dockerfile',
45
            'Dockerfile.dev',
46
            'Dockerfile.dev.node',
47
            'version.txt',
48
        ];
49
        $this->info('Removing docker related files...');
50
        foreach ($docker_files as $file) {
51
            $file_path = base_path($file);
52
            if (File::exists($file_path)) {
53
                File::delete($file_path);
54
                $this->info("Removed {$file} from application root");
55
            }
56
        }
57
        self::deleteFileTree(base_path('docker'));
58
        $this->info("Removed 'docker' directory from application root");
59
60
        // Remove compose scripts
61
        $this->removeComposerScript('start-dev');
62
        $this->removeComposerScript('start-dev-db');
63
        $this->removeComposerScript('start-dev-node');
64
        $this->removeComposerScript('start-test');
65
        $this->removeComposerScript('stop');
66
        $this->removeComposerScript('build');
67
        $this->info('Removed composer scripts for starting/stopping docker services');
68
69
        return self::SUCCESS;
70
    }
71
72
    private function removeComposerScript(string $script): void
73
    {
74
        (new Process(['composer', 'config', '--unset', "scripts.$script", '--working-dir='.base_path()]))->run();
75
76
        $this->info("Removed 'composer $script' command to composer.json");
77
    }
78
79
    private static function deleteFileTree(string $directory): void
80
    {
81
        foreach (array_diff(scandir($directory), ['.', '..']) as $file) {
82
            is_dir("$directory/$file")
83
                ? self::deleteFileTree("$directory/$file")
84
                : unlink("$directory/$file");
85
        }
86
87
        rmdir($directory);
88
    }
89
}
90