CacheFlushCommand   A
last analyzed

Complexity

Total Complexity 8

Size/Duplication

Total Lines 52
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 5

Importance

Changes 0
Metric Value
wmc 8
lcom 0
cbo 5
dl 0
loc 52
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A configure() 0 18 1
B execute() 0 27 7
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): int
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
        return 0;
71
    }
72
}
73