Completed
Push — master ( 3cc06d...bb89f7 )
by Anton
14s queued 11s
created

CleanCommand::perform()   B

Complexity

Conditions 6
Paths 9

Size

Total Lines 37
Code Lines 20

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 1 Features 0
Metric Value
eloc 20
c 1
b 1
f 0
dl 0
loc 37
rs 8.9777
cc 6
nc 9
nop 2
1
<?php
2
/**
3
 * Spiral Framework.
4
 *
5
 * @license   MIT
6
 * @author    Anton Titov (Wolfy-J)
7
 */
8
declare(strict_types=1);
9
10
namespace Spiral\Command;
11
12
use Spiral\Boot\DirectoriesInterface;
13
use Spiral\Console\Command;
14
use Spiral\Core\Container\SingletonInterface;
15
use Spiral\Files\FilesInterface;
16
17
final class CleanCommand extends Command implements SingletonInterface
18
{
19
    protected const NAME        = 'cache:clean';
20
    protected const DESCRIPTION = 'Clean application runtime cache';
21
22
    /**
23
     * @param FilesInterface       $files
24
     * @param DirectoriesInterface $directories
25
     */
26
    public function perform(FilesInterface $files, DirectoriesInterface $directories)
27
    {
28
        $cacheDirectory = $directories->get('cache');
29
        if (!$files->exists($cacheDirectory)) {
30
            $this->writeln("Cache directory is missing, no cache to be cleaned.");
31
32
            return;
33
        }
34
35
        if ($this->isVerbose()) {
36
            $this->writeln("<info>Cleaning application cache:</info>");
37
        }
38
39
        foreach ($files->getFiles($cacheDirectory) as $filename) {
40
            try {
41
                $files->delete($filename);
42
            } catch (\Throwable $e) {
43
                // @codeCoverageIgnoreStart
44
                $this->sprintf(
45
                    "<fg=red>[errored]</fg=red> `%s`: <fg=red>%s</fg=red>\n",
46
                    $files->relativePath($filename, $cacheDirectory),
47
                    $e->getMessage()
48
                );
49
50
                continue;
51
                // @codeCoverageIgnoreEnd
52
            }
53
54
            if ($this->isVerbose()) {
55
                $this->sprintf(
56
                    "<fg=green>[deleted]</fg=green> `%s`\n",
57
                    $files->relativePath($filename, $cacheDirectory)
58
                );
59
            }
60
        }
61
62
        $this->writeln("<info>Runtime cache has been cleared.</info>");
63
    }
64
}
65