PhpfastcacheClearCommand   A
last analyzed

Complexity

Total Complexity 12

Size/Duplication

Total Lines 96
Duplicated Lines 20.83 %

Coupling/Cohesion

Components 2
Dependencies 7

Importance

Changes 0
Metric Value
wmc 12
lcom 2
cbo 7
dl 20
loc 96
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 7 1
A configure() 0 12 1
C execute() 20 52 10

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

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
declare(strict_types=1);
17
18
namespace Phpfastcache\Bundle\Command;
19
20
use Phpfastcache\Bundle\Service\Phpfastcache;
21
use Phpfastcache\Exceptions\PhpfastcacheDriverCheckException;
22
use Symfony\Component\Console\Command\Command;
23
use Symfony\Component\Console\Input\InputArgument;
24
use Symfony\Component\Console\Input\InputInterface;
25
use Symfony\Component\Console\Output\OutputInterface;
26
use Symfony\Component\Console\Style\SymfonyStyle;
27
28
class PhpfastcacheClearCommand extends Command
29
{
30
    /**
31
     * @var \Phpfastcache\Bundle\Service\Phpfastcache
32
     */
33
    protected $phpfastcache;
34
35
    /**
36
     * @var
37
     */
38
    protected $parameters;
39
40
    /**
41
     * PhpfastcacheCommand constructor.
42
     * @param \Phpfastcache\Bundle\Service\Phpfastcache $phpfastcache
43
     * @param $parameters
44
     */
45
    public function __construct(Phpfastcache $phpfastcache, $parameters)
46
    {
47
        $this->phpfastcache = $phpfastcache;
48
        $this->parameters = $parameters;
49
50
        parent::__construct();
51
    }
52
53
    protected function configure()
54
    {
55
        $this
56
          ->setName('phpfastcache:clear')
57
          ->setAliases(['phpfastcache:clean', 'phpfastcache:flush'])
58
          ->setDescription('Clear phpfastcache cache')
59
          ->addArgument(
60
            'driver',
61
            InputArgument::OPTIONAL,
62
            'Cache name to clear'
63
          );
64
    }
65
66
    /**
67
     * @param \Symfony\Component\Console\Input\InputInterface $input
68
     * @param \Symfony\Component\Console\Output\OutputInterface $output
69
     * @return int|null|void
70
     */
71
    protected function execute(InputInterface $input, OutputInterface $output)
72
    {
73
        $failedInstances = [];
74
        $io = new SymfonyStyle($input, $output);
75
76
        $driver = $input->getArgument('driver');
77
78
        $output->writeln('<bg=yellow;fg=red>Clearing cache operation can take a while, please be patient...</>');
79
80
        $callback = function ($name) use ($output, &$failedInstances) {
81
            try {
82
                if (OutputInterface::VERBOSITY_VERBOSE <= $output->getVerbosity()) {
83
                    $output->writeln("<fg=yellow>Clearing instance {$name} cache...</>");
84
                }
85
                $this->phpfastcache->get($name)->clear();
86
                if (OutputInterface::VERBOSITY_VERBOSE <= $output->getVerbosity()) {
87
                    $output->writeln("<fg=green>Cache instance {$name} cleared</>");
88
                }
89
            } catch (PhpfastcacheDriverCheckException $e) {
90
                $failedInstances[] = $name;
91
                if (OutputInterface::VERBOSITY_VERBOSE <= $output->getVerbosity()) {
92
                    $output->writeln("<fg=red>Cache instance {$name} not cleared, got exception: " . '<bg=red;options=bold>' . $e->getMessage() . '</>');
93
                } else {
94
                    $output->writeln("<fg=red>Cache instance {$name} not cleared (increase verbosity to get more information).</>");
95
                }
96
            }
97
        };
98
99
        $caches = $this->parameters;
100
101
        if ($driver) {
102 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...
103
                $callback($driver);
104
                if (!\count($failedInstances)) {
105
                    $io->success("Cache instance {$driver} cleared");
106
                } else {
107
                    $io->error("Cache instance {$driver} not cleared");
108
                }
109
            } else {
110
                $io->error("Cache instance {$driver} does not exists");
111
            }
112 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...
113
            foreach ($caches[ 'drivers' ] as $name => $parameters) {
114
                $callback($name);
115
            }
116
            if (!\count($failedInstances)) {
117
                $io->success('All caches instances got cleared');
118
            } else {
119
                $io->success('Almost all caches instances got cleared, except these: ' . \implode(', ', $failedInstances));
120
            }
121
        }
122
    }
123
}