Completed
Branch V3 (ec4077)
by PastisD
11:29 queued 09:28
created

PhpfastcacheCommand::configure()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 12
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 12
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 8
nc 1
nop 0
1
<?php
2
3
/**
4
 *
5
 * This file is part of phpFastCache.
6
 *
7
 * @license MIT License (MIT)
8
 *
9
 * For full copyright and license information, please see the docs/CREDITS.txt file.
10
 *
11
 * @author Georges.L (Geolim4)  <[email protected]>
12
 * @author PastisD https://github.com/PastisD
13
 * @author Khoa Bui (khoaofgod)  <[email protected]> http://www.phpfastcache.com
14
 *
15
 */
16
17
namespace Phpfastcache\Bundle\Command;
18
19
use Phpfastcache\Core\Pool\ExtendedCacheItemPoolInterface;
20
use Phpfastcache\Exceptions\PhpfastcacheDriverCheckException;
21
use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand;
22
use Symfony\Component\Console\Input\InputArgument;
23
use Symfony\Component\Console\Input\InputInterface;
24
use Symfony\Component\Console\Output\OutputInterface;
25
use Symfony\Component\Console\Style\SymfonyStyle;
26
27
class PhpfastcacheCommand extends ContainerAwareCommand
28
{
29
    protected function configure()
30
    {
31
        $this
32
            ->setName('phpfastcache:clear')
33
            ->setDescription('Clear phpfastcache cache')
34
            ->addArgument(
35
                'driver',
36
                InputArgument::OPTIONAL,
37
                'Cache name to clear'
38
            )
39
        ;
40
    }
41
    protected function execute(InputInterface $input, OutputInterface $output)
42
    {
43
        $failedInstances = [];
44
        $io = new SymfonyStyle($input, $output);
45
46
        $phpFastCache = $this->getContainer()->get('phpfastcache');
47
        $driver = $input->getArgument('driver');
48
49
        $output->writeln("<bg=yellow;fg=red>Clearing cache operation can take a while, please be patient...</>");
50
51
        $callback = function($name) use ($phpFastCache, $output, &$failedInstances)
52
        {
53
            try{
54
                if (OutputInterface::VERBOSITY_VERBOSE <= $output->getVerbosity()) {
55
                    $output->writeln("<fg=yellow>Clearing instance {$name} cache...</>");
56
                }
57
                $phpFastCache->get($name)->clear();
58
                if (OutputInterface::VERBOSITY_VERBOSE <= $output->getVerbosity()) {
59
                    $output->writeln("<fg=green>Cache instance {$name} cleared</>");
60
                }
61
            }catch (PhpfastcacheDriverCheckException $e){
62
                $failedInstances[] = $name;
63
                if (OutputInterface::VERBOSITY_VERBOSE <= $output->getVerbosity()) {
64
                    $output->writeln("<fg=red>Cache instance {$name} not cleared, got exception: " . "<bg=red;options=bold>" . $e->getMessage() ."</>");
65
                }else{
66
                    $output->writeln("<fg=red>Cache instance {$name} not cleared (increase verbosity to get more information).</>");
67
                }
68
            }
69
        };
70
        $caches = $this->getContainer()->getParameter('phpfastcache');
71
72
        if($driver) {
73 View Code Duplication
            if(\array_key_exists($driver, $caches['drivers'])){
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
74
                $callback($driver);
75
                if(!\count($failedInstances)){
76
                    $io->success("Cache instance {$driver} cleared");
77
                }else{
78
                    $io->error("Cache instance {$driver} not cleared");
79
                }
80
            }else{
81
                $io->error("Cache instance {$driver} does not exists");
82
            }
83 View Code Duplication
        } else {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
84
            foreach($caches['drivers'] as $name => $parameters) {
85
                $callback($name);
86
            }
87
            if(!\count($failedInstances)){
88
                $io->success('All caches instances got cleared');
89
            }else{
90
                $io->success('Almost all caches instances got cleared, except these: ' . \implode(', ', $failedInstances));
91
            }
92
        }
93
    }
94
}