CacheClearCommand::configure()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 14
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 14
rs 9.4285
cc 1
eloc 7
nc 1
nop 0
1
<?php
2
3
namespace AppBundle\Command;
4
5
use Symfony\Component\Console\Input\InputInterface;
6
use Symfony\Component\Console\Output\OutputInterface;
7
use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand;
8
use AppBundle\Cache\RedisCache;
9
10
class CacheClearCommand extends ContainerAwareCommand
11
{
12
    protected function configure()
13
    {
14
        $this
15
            ->setName('app:cache:clear')
16
            ->setDescription('Clears application cache.')
17
            ->setHelp(<<<EOT
18
The <info>%command.name%</info> clears application cache.
19
20
<info>php %command.full_name%</info>
21
<info>php %command.full_name% --env=prod</info>
22
23
EOT
24
        );
25
    }
26
27
    protected function execute(InputInterface $input, OutputInterface $output)
28
    {
29
        $env = $this->getContainer()->getParameter('kernel.environment');
30
        $output->writeLn("Clearing app cache for environment <comment>{$env}</comment>...");
31
32
        $cache = $this->getContainer()->get('cache.default');
33
        if ($cache instanceof RedisCache) {
34
            if ($env === 'test') {
35
                $cache->flushDB();
36
                $output->writeln('Flushed all redis cache');
37
            } else {
38
                // flush only namespaced keys for redis cache
39
                $output->writeLn(sprintf(
40
                    "Flushed <comment>%s</comment> cache entries",
41
                    trim($cache->flushNamespacedKeys())
42
                ));
43
            }
44
        } else {
45
            // do not mind and remove all cache (array cache)
46
            $cache->flushAll();
47
        }
48
49
        $output->writeLn("Cache was cleared.");
50
    }
51
}
52