Passed
Push — master ( 523107...3d7bf1 )
by Andreas
25:53
created

parameters::execute()   B

Complexity

Conditions 8
Paths 36

Size

Total Lines 47
Code Lines 31

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 72

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 8
eloc 31
nc 36
nop 2
dl 0
loc 47
ccs 0
cts 40
cp 0
crap 72
rs 8.1795
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\cleanup;
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 Symfony\Component\Console\Helper\ProgressBar;
16
17
/**
18
 * Cleanup dangling parameters
19
 *
20
 * @package midcom.console
21
 */
22
class parameters extends Command
23
{
24
    protected function configure()
25
    {
26
        $this->setName('midcom:cleanup:parameters')
27
            ->setAliases(['parametercleanup'])
28
            ->setDescription('Cleanup dangling parameters')
29
            ->addOption('dry', 'd', InputOption::VALUE_NONE, 'If set, parameters will not be deleted');
30
    }
31
32
    protected function execute(InputInterface $input, OutputInterface $output) : int
33
    {
34
        if ($dry = $input->getOption("dry")) {
35
            $output->writeln("<comment>Running in dry mode!</comment>");
36
        }
37
38
        $qb = \midgard_parameter::new_query_builder();
39
        $output->writeln("<comment>Checking parameters</comment>");
40
41
        $progress = new ProgressBar($output, $qb->count());
42
        $progress->setRedrawFrequency(100);
43
        $progress->start();
44
45
        $seen = [];
46
        $to_delete = [];
47
        foreach ($qb->iterate() as $param) {
48
            if (!array_key_exists($param->parentguid, $seen)) {
49
                try {
50
                    \midgard_object_class::get_object_by_guid($param->parentguid);
51
                    $seen[$param->parentguid] = true;
52
                } catch (\Exception $e) {
53
                    $seen[$param->parentguid] = false;
54
                }
55
            }
56
            if (!$seen[$param->parentguid]) {
57
                $to_delete[] = $param;
58
            }
59
            $progress->advance();
60
        }
61
        $progress->finish();
62
63
        $output->writeln("\nFound <info>" . count($to_delete) . "</info> dangling parameters");
64
65
        if (!$dry) {
66
            $output->writeln("<comment>Deleting parameters</comment>");
67
            $progress = new ProgressBar($output, count($to_delete));
68
            $progress->start();
69
70
            foreach ($to_delete as $param) {
71
                $param->delete();
72
                $progress->advance();
73
            }
74
            $progress->finish();
75
        }
76
77
        $output->writeln("\nDone");
78
        return 0;
79
    }
80
}
81