Completed
Push — 2.x-dev-kit ( 6f250c )
by
unknown
02:44
created

CacheFlushCommand::execute()   B

Complexity

Conditions 5
Paths 4

Size

Total Lines 20
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 4
Bugs 1 Features 1
Metric Value
c 4
b 1
f 1
dl 0
loc 20
rs 8.8571
cc 5
eloc 11
nc 4
nop 2
1
<?php
2
3
/*
4
 * This file is part of the Sonata Project package.
5
 *
6
 * (c) Thomas Rabaix <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Sonata\CacheBundle\Command;
13
14
use Symfony\Component\Console\Input\InputInterface;
15
use Symfony\Component\Console\Input\InputOption;
16
use Symfony\Component\Console\Output\OutputInterface;
17
18
class CacheFlushCommand extends BaseCacheCommand
19
{
20
    /**
21
     * {@inheritdoc}
22
     */
23
    public function configure()
24
    {
25
        $this->setName('sonata:cache:flush');
26
        $this->setDescription('Flush information');
27
28
        $this->addOption('keys', null, InputOption::VALUE_REQUIRED, 'Flush all elements matching the providing keys (json format)');
29
        $this->addOption('cache', null, InputOption::VALUE_REQUIRED | InputOption::VALUE_IS_ARRAY, 'Flush elements stored in given cache');
30
    }
31
32
    /**
33
     * {@inheritdoc}
34
     */
35
    public function execute(InputInterface $input, OutputInterface $output)
36
    {
37
        $keys = @json_decode($input->getOption('keys'), true);
38
39
        if (!is_array($keys)) {
40
            throw new \RuntimeException('The provided keys cannot be decoded, please provide a valid json string');
41
        }
42
43
        foreach ($this->getManager()->getCacheServices() as $name => $cache) {
44
            if ($input->getOption('cache') && !in_array($name, $input->getOption('cache'))) {
45
                continue;
46
            }
47
48
            $output->write(sprintf(' > %s : starting .... ', $name));
49
            $cache->flush($keys);
50
            $output->writeln('OK');
51
        }
52
53
        $output->writeln('<info>done!</info>');
54
    }
55
}
56