OpcacheResetFileCacheCommand   A
last analyzed

Complexity

Total Complexity 8

Size/Duplication

Total Lines 69
Duplicated Lines 0 %

Test Coverage

Coverage 17.14%

Importance

Changes 3
Bugs 0 Features 0
Metric Value
eloc 34
dl 0
loc 69
ccs 6
cts 35
cp 0.1714
rs 10
c 3
b 0
f 0
wmc 8

3 Methods

Rating   Name   Duplication   Size   Complexity  
A execute() 0 32 4
A performDelete() 0 15 3
A configure() 0 7 1
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 24
    protected function configure()
25
    {
26
        $this
27 24
            ->setName('opcache:reset:file-cache')
28 24
            ->setDescription('Deletes all contents of the file cache directory')
29 24
            ->addOption('force', 'f', InputOption::VALUE_NONE, 'Delete files without questioning')
30 24
            ->setHelp('');
31 24
    }
32
33
    /**
34
     * {@inheritdoc}
35
     */
36
    protected function execute(InputInterface $input, OutputInterface $output): int
37
    {
38
        $this->ensureExtensionLoaded('Zend OPcache');
39
40
        $info = $this->getCacheTool()->opcache_get_status();
41
        $fileCache = $info['file_cache'] ?? false;
42
43
        if (!$fileCache) {
44
            throw new \RuntimeException('opcache.file_cache is not set.');
45
        }
46
47
        if (!$input->getOption('force')) {
48
            $question = new ConfirmationQuestion(
49
                "Are you sure you want to delete the contents of <comment>${fileCache}</comment>? [no] ",
50
                false,
51
                '/^y/i'
52
            );
53
54
            $helper = $this->getHelper('question');
55
            $result = $helper->ask($input, $output, $question);
56
57
            if (!$result) {
58
                $output->writeln('<info>Aborted file deletion</info>');
59
                return 0;
60
            }
61
        }
62
63
64
        $deleted = $this->performDelete($fileCache);
65
        $output->writeln("<info>Deleted <comment>{$deleted}</comment> files.</info>");
66
67
        return 0;
68
    }
69
70
    /**
71
     * @param string $directory
72
     */
73
    protected function performDelete($directory) {
74
        $count = 0;
75
        $it = new \RecursiveDirectoryIterator($directory, \RecursiveDirectoryIterator::SKIP_DOTS);
76
        $files = new \RecursiveIteratorIterator($it, \RecursiveIteratorIterator::CHILD_FIRST);
77
78
        foreach($files as $file) {
79
            if ($file->isDir()){
80
                $this->getCacheTool()->_eval("rmdir('{$file->getRealPath()}');");
81
            } else {
82
                $this->getCacheTool()->_eval("unlink('{$file->getRealPath()}');");
83
            }
84
            $count += 1;
85
        }
86
87
        return $count;
88
    }
89
}
90