Completed
Push — master ( 17dfca...2f9de9 )
by Samuel
01:17
created

OpcacheResetFileCacheCommand   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 72
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 4

Test Coverage

Coverage 16.66%

Importance

Changes 0
Metric Value
wmc 8
lcom 2
cbo 4
dl 0
loc 72
ccs 6
cts 36
cp 0.1666
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A configure() 0 8 1
A execute() 0 34 4
A performDelete() 0 16 3
1
<?php
2
3
/*
4
 * This file is part of CacheTool.
5
 *
6
 * (c) Samuel Gordalina <[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 CacheTool\Command;
13
14
use Symfony\Component\Console\Input\InputInterface;
15
use Symfony\Component\Console\Input\InputOption;
16
use Symfony\Component\Console\Output\OutputInterface;
17
use Symfony\Component\Console\Question\ConfirmationQuestion;
18
19
class OpcacheResetFileCacheCommand extends AbstractOpcacheCommand
20
{
21
    /**
22
     * {@inheritdoc}
23
     */
24 19
    protected function configure()
25
    {
26
        $this
27 19
            ->setName('opcache:reset:file-cache')
28 19
            ->setDescription('Deletes all contents of the file cache directory')
29 19
            ->addOption('force', 'f', InputOption::VALUE_NONE, 'Delete files without questioning')
30 19
            ->setHelp('');
31 19
    }
32
33
    /**
34
     * {@inheritdoc}
35
     */
36
    protected function execute(InputInterface $input, OutputInterface $output): int
37
    {
38
        $this->ensureExtensionLoaded('Zend OPcache');
39
40
        $force = $input->getOption('force');
0 ignored issues
show
Unused Code introduced by
$force is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
41
        $info = $this->getCacheTool()->opcache_get_status();
42
        $fileCache = $info['file_cache'] ?? false;
43
44
        if (!$fileCache) {
45
            throw new \RuntimeException('opcache.file_cache is not set.');
46
        }
47
48
        if (!$input->getOption('force')) {
49
            $question = new ConfirmationQuestion(
50
                "Are you sure you want to delete the contents of <comment>${fileCache}</comment>? [no] ",
51
                false,
52
                '/^y/i'
53
            );
54
55
            $helper = $this->getHelper('question');
56
            $result = $helper->ask($input, $output, $question);
57
58
            if (!$result) {
59
                $output->writeln('<info>Aborted file deletion</info>');
60
                return 0;
61
            }
62
        }
63
64
65
        $deleted = $this->performDelete($fileCache);
66
        $output->writeln("<info>Deleted <comment>{$deleted}</comment> files.</info>");
67
68
        return 0;
69
    }
70
71
    /**
72
     * @param string $directory
73
     */
74
    protected function performDelete($directory) {
75
        $count = 0;
76
        $it = new \RecursiveDirectoryIterator($directory, \RecursiveDirectoryIterator::SKIP_DOTS);
77
        $files = new \RecursiveIteratorIterator($it, \RecursiveIteratorIterator::CHILD_FIRST);
78
79
        foreach($files as $file) {
80
            if ($file->isDir()){
81
                rmdir($file->getRealPath());
82
            } else {
83
                unlink($file->getRealPath());
84
            }
85
            $count += 1;
86
        }
87
88
        return $count;
89
    }
90
}
91