Passed
Push — master ( cd8ed8...ef1fc1 )
by Andreas
10:21
created

privileges::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 1
Bugs 0 Features 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 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;
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 Symfony\Component\Console\Helper\ProgressBar;
16
17
/**
18
 * Cleanup dangling privileges
19
 *
20
 * @package midcom.console
21
 */
22
class privileges extends Command
23
{
24
    protected function configure()
25
    {
26
        $this->setName('midcom:cleanup:privileges')
27
            ->setAliases(['privilegecleanup'])
28
            ->setDescription('Cleanup dangling privileges')
29
            ->addOption('dry', 'd', InputOption::VALUE_NONE, 'If set, privileges 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 = \midcom_core_privilege_db::new_query_builder();
39
        $output->writeln("<comment>Checking privileges</comment>");
40
41
        $progress = new ProgressBar($output, $qb->count());
42
        $progress->setRedrawFrequency(100);
43
        $progress->start();
44
45
        $seen_parents = $seen_assignees = [];
46
        $checker = new \midcom_core_privilege;
47
        $to_delete = [];
48
        foreach ($qb->iterate() as $priv) {
49
            if (!array_key_exists($priv->objectguid, $seen_parents)) {
50
                try {
51
                    \midgard_object_class::get_object_by_guid($priv->objectguid);
52
                    $seen_parents[$priv->objectguid] = true;
53
                } catch (\Exception $e) {
54
                    $seen_parents[$priv->objectguid] = false;
55
                }
56
            }
57
            if (!$seen_parents[$priv->objectguid]) {
58
                $to_delete[] = $priv;
59
            }
60
            if (!$checker->is_magic_assignee($priv->assignee)) {
61
                if (!array_key_exists($priv->assignee, $seen_assignees)) {
62
                    $seen_assignees[$priv->assignee] = (bool) \midcom::get()->auth->get_assignee($priv->assignee);
63
                }
64
                if (!$seen_assignees[$priv->assignee]) {
65
                    $to_delete[] = $priv;
66
                }
67
            }
68
69
            $progress->advance();
70
        }
71
        $progress->finish();
72
73
        $output->writeln("\nFound <info>" . count($to_delete) . "</info> dangling privileges");
74
75
        if (!$dry) {
76
            $output->writeln("<comment>Deleting privileges</comment>");
77
            $progress = new ProgressBar($output, count($to_delete));
78
            $progress->start();
79
80
            foreach ($to_delete as $priv) {
81
                $priv->purge();
82
                $progress->advance();
83
            }
84
            $progress->finish();
85
        }
86
87
        $output->writeln("\nDone");
88
        return 0;
89
    }
90
}
91