Completed
Push — master ( f2deb9...8da88e )
by
unknown
01:32
created

CacheFlushCommand::execute()   B

Complexity

Conditions 7
Paths 7

Size

Total Lines 25

Duplication

Lines 0
Ratio 0 %

Importance

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