Completed
Push — master ( b9fa67...e31f65 )
by Andreas
24:00 queued 04:20
created

rcsdircleanup::has_repligard_entry()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 5
nc 1
nop 1
dl 0
loc 9
ccs 0
cts 6
cp 0
crap 2
rs 10
c 1
b 0
f 0
1
<?php
2
/**
3
 * @package midcom.console
4
 * @author CONTENT CONTROL http://www.contentcontrol-berlin.de/
5
 * @copyright CONTENT CONTROL http://www.contentcontrol-berlin.de/
6
 * @license http://www.gnu.org/licenses/gpl.html GNU General Public License
7
 */
8
9
namespace midcom\console\command;
10
11
use Symfony\Component\Console\Command\Command;
12
use Symfony\Component\Console\Input\InputInterface;
13
use Symfony\Component\Console\Input\InputOption;
14
use Symfony\Component\Console\Output\OutputInterface;
15
use midcom;
16
use midcom_services_rcs_config;
17
use midgard\portable\storage\connection;
18
use Symfony\Component\Console\Output\Output;
19
use Symfony\Component\Console\Helper\ProgressBar;
20
21
/**
22
 * Cleanup the RCS dir
23
 * Searches for RCS files that don't have a corresponding entry in the repligard table
24
 *
25
 * @package midcom.console
26
 */
27
class rcsdircleanup extends Command
28
{
29
    /**
30
     * @var int
31
     */
32
    private $counter = 0;
33
34
    private $dry = false;
35
36
    private $findings = [
37
        'orphaned' => []
38
    ];
39
40 1
    protected function configure()
41
    {
42 1
        $this->setName('midcom:rcsdircleanup')
43 1
            ->setAliases(['rcsdircleanup'])
44 1
            ->setDescription('Cleanup the RCS dir')
45 1
            ->addOption('dry', 'd', InputOption::VALUE_NONE, 'If set, files will not be deleted');
46 1
    }
47
48
    private function check_dir(OutputInterface $output, string $outerDir)
49
    {
50
        $outerDir = rtrim($outerDir, "/");
51
        $output->write("\x0D");
52
        $output->write("Start scanning dir: <comment>" . $outerDir . "</comment>");
53
        $dirs = array_diff(scandir($outerDir), [".", ".."]);
0 ignored issues
show
Bug introduced by
It seems like scandir($outerDir) can also be of type false; however, parameter $array1 of array_diff() does only seem to accept array, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

53
        $dirs = array_diff(/** @scrutinizer ignore-type */ scandir($outerDir), [".", ".."]);
Loading history...
54
        foreach ($dirs as $d) {
55
            if (is_dir($outerDir . "/" . $d)) {
56
                $this->check_dir($output, $outerDir . "/" . $d);
57
            } else {
58
                // got something
59
                $file = $outerDir . "/" . $d;
60
                if (!$this->has_repligard_entry($file)) {
61
                    $this->findings['orphaned'][] = $file;
62
                }
63
                $this->counter++;
64
            }
65
        }
66
    }
67
68
    private function has_repligard_entry(string $file) : bool
69
    {
70
        $guid = preg_replace('/^.+\/(.+?)\,?v?$/', '$1', $file);
71
72
        $repligard_entry = connection::get_em()
73
            ->getRepository('midgard:midgard_repligard')
74
            ->findOneBy(['guid' => $guid]);
75
76
        return !empty($repligard_entry);
77
    }
78
79
    private function cleanup_file(OutputInterface $output, string $file)
80
    {
81
        if (unlink($file)) {
82
            $output->writeln("<info>Cleanup OK</info>", Output::VERBOSITY_VERBOSE);
83
        } else {
84
            $output->writeln("<comment>Cleanup FAILED</comment>");
85
        }
86
    }
87
88
    protected function execute(InputInterface $input, OutputInterface $output) : int
89
    {
90
        $config = new midcom_services_rcs_config(midcom::get()->config);
91
        $dir = $config->get_rcs_root();
92
        if (!is_dir($dir)) {
93
            $output->writeln("<comment>Unable to detect RCS dir</comment> $dir");
94
            return 1;
95
        }
96
        $this->dry = $input->getOption("dry");
97
        if ($this->dry) {
98
            $output->writeln("<comment>Running in dry mode!</comment>");
99
        }
100
101
        $this->check_dir($output, $dir);
102
103
        $output->writeln("\nScanned <info>" . $this->counter . "</info> files");
104
        $output->writeln("Found <info>" . count($this->findings['orphaned']) . "</info> orphaned files:");
105
106
        if (!$this->dry) {
107
            $output->writeln("<comment>Deleting orphans</comment>");
108
            $progress = new ProgressBar($output, count($this->findings['orphaned']));
109
            $progress->setRedrawFrequency(100);
110
            $progress->start();
111
112
            foreach ($this->findings['orphaned'] as $file) {
113
                $this->cleanup_file($output, $file);
114
                $progress->advance();
115
            }
116
            $progress->finish();
117
        }
118
119
        $output->writeln("\n<comment>Done</comment>");
120
        return 0;
121
    }
122
}
123