Completed
Pull Request — master (#1)
by ANTHONIUS
03:43
created

ClearCacheCommand::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 5
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 3
nc 1
nop 3
1
<?php
2
3
/*
4
 * This file is part of the dotfiles project.
5
 *
6
 * (c) Anthonius Munthi <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was disstributed with this source code.
10
 */
11
12
namespace Dotfiles\Core\Command;
13
14
15
use Dotfiles\Core\Config\Config;
16
use Dotfiles\Core\Util\Filesystem;
17
use Psr\Log\LoggerInterface;
18
use Symfony\Component\Console\Command\Command;
19
use Symfony\Component\Console\Input\InputInterface;
20
use Symfony\Component\Console\Output\OutputInterface;
21
use Symfony\Component\Finder\Finder;
22
23
class ClearCacheCommand extends Command implements CommandInterface
24
{
25
    /**
26
     * @var Config
27
     */
28
    private $config;
29
30
    /**
31
     * @var LoggerInterface
32
     */
33
    private $logger;
34
35
    public function __construct(?string $name = null, Config $config, LoggerInterface $logger)
36
    {
37
        parent::__construct($name);
38
        $this->config = $config;
39
        $this->logger = $logger;
40
    }
41
42
    protected function configure()
43
    {
44
        $this
45
            ->setName('clear-cache')
46
            ->setAliases(['cc'])
47
        ;
48
    }
49
50
    protected function execute(InputInterface $input, OutputInterface $output)
51
    {
52
        $config = $this->config;
53
        $cacheDir = $config->get('dotfiles.cache_dir');
54
55
        $finder = Finder::create()
56
            ->in($cacheDir)
57
            ->files()
58
        ;
59
60
        $fs = new Filesystem();
61
62
        /* @var \Symfony\Component\Finder\SplFileInfo $file */
63
        foreach($finder->files() as $file){
64
            $fs->remove($file);
65
            $relPath = 'var/cache/'.$file->getRelativePathname();
66
            $this->logger->debug("-removed <comment>$relPath</comment>");
67
        }
68
    }
69
}
70