Passed
Push — master ( edbc79...094e7b )
by Korotkov
03:53 queued 01:46
created

CacheClearCommand::deleteDirectory()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 11
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 5
c 1
b 0
f 0
dl 0
loc 11
rs 10
cc 4
nc 4
nop 1
1
<?php
2
3
namespace App\Ship\Command;
4
5
use Rudra\Cli\ConsoleFacade as Cli;
6
use Rudra\Auth\AuthFacade as Auth;
7
use Rudra\Container\Facades\Request;
8
9
class CacheClearCommand
10
{
11
    public function actionIndex(): void
12
    {
13
        Cli::printer("Enter cache type (empty for all): ", "magneta");
14
        $type = str_replace(PHP_EOL, "", Cli::reader());
15
16
        $folderPath = !empty($type)
17
            ? dirname(__DIR__, 2) . "/cache/$type"
18
            : dirname(__DIR__, 2) . "/cache";
19
20
        if ($this->deleteDirectory($folderPath)) {
21
            Cli::printer(!empty($type)
22
                ? "Cache $type was cleared" . PHP_EOL
23
                : "Cache was cleared" . PHP_EOL, "light_green");
24
        } else {
25
            Cli::printer(!empty($type)
26
                ? "Cache type '$type' does not exist." . PHP_EOL
27
                : "Cache directory does not exist." . PHP_EOL, "red");
28
        }
29
    }
30
31
    /**
32
     * @param string $dir
33
     * @return bool
34
     */
35
    private function deleteDirectory(string $dir): bool
36
    {
37
        if (!is_dir($dir)) {
38
            return false;
39
        }
40
41
        foreach (glob($dir . '/*') as $file) {
42
            is_dir($file) ? $this->deleteDirectory($file) : unlink($file);
43
        }
44
45
        return rmdir($dir);
46
    }
47
}
48