Passed
Push — master ( 3252a3...420639 )
by Andreas
23:53
created

rcsdir::configure()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 4
nc 1
nop 0
dl 0
loc 6
ccs 0
cts 5
cp 0
crap 2
rs 10
c 0
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\cleanup;
10
11
use Symfony\Component\Console\Command\Command;
0 ignored issues
show
Bug introduced by
The type Symfony\Component\Console\Command\Command was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
12
use Symfony\Component\Console\Input\InputInterface;
13
use Symfony\Component\Console\Input\InputOption;
14
use Symfony\Component\Console\Output\OutputInterface;
15
use midcom_config;
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 rcsdir extends Command
28
{
29
    private int $counter = 0;
30
31
    private array $orphaned = [];
32
33
    private midcom_config $config;
34
35
    public function __construct(midcom_config $config)
36
    {
37
        $this->config = $config;
38
        parent::__construct();
39
    }
40
41
    protected function configure()
42
    {
43
        $this->setName('midcom:cleanup:rcsdir')
44
            ->setAliases(['rcsdircleanup'])
45
            ->setDescription('Cleanup the RCS dir')
46
            ->addOption('dry', 'd', InputOption::VALUE_NONE, 'If set, files will not be deleted');
47
    }
48
49
    private function check_dir(OutputInterface $output, string $outerDir)
50
    {
51
        $outerDir = rtrim($outerDir, "/");
52
        $output->write("\x0D");
53
        $output->write("Start scanning dir: <comment>" . $outerDir . "</comment>");
54
        $dirs = array_diff(scandir($outerDir), [".", ".."]);
55
        foreach ($dirs as $d) {
56
            if (is_dir($outerDir . "/" . $d)) {
57
                $this->check_dir($output, $outerDir . "/" . $d);
58
            } else {
59
                // got something
60
                $file = $outerDir . "/" . $d;
61
                if (!$this->has_repligard_entry($file)) {
62
                    $this->orphaned[] = $file;
63
                }
64
                $this->counter++;
65
            }
66
        }
67
    }
68
69
    private function has_repligard_entry(string $file) : bool
70
    {
71
        $guid = preg_replace('/^.+\/(.+?),?v?$/', '$1', $file);
72
73
        $repligard_entry = connection::get_em()
74
            ->getRepository('midgard:midgard_repligard')
75
            ->findOneBy(['guid' => $guid]);
76
77
        return !empty($repligard_entry);
78
    }
79
80
    private function cleanup_file(OutputInterface $output, string $file)
81
    {
82
        if (unlink($file)) {
83
            $output->writeln("<info>Cleanup OK</info>", Output::VERBOSITY_VERBOSE);
84
        } else {
85
            $output->writeln("<comment>Cleanup FAILED</comment>");
86
        }
87
    }
88
89
    protected function execute(InputInterface $input, OutputInterface $output) : int
90
    {
91
        $config = new midcom_services_rcs_config($this->config);
92
        $dir = $config->get_rootdir();
93
        if (!is_dir($dir)) {
94
            $output->writeln("<comment>Unable to detect RCS dir</comment> $dir");
95
            return 1;
96
        }
97
        if ($dry = $input->getOption("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->orphaned) . "</info> orphaned files:");
105
106
        if (!$dry) {
107
            $output->writeln("<comment>Deleting orphans</comment>");
108
            $progress = new ProgressBar($output, count($this->orphaned));
109
            $progress->setRedrawFrequency(100);
110
            $progress->start();
111
112
            foreach ($this->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