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

privileges::execute()   C

Complexity

Conditions 11
Paths 164

Size

Total Lines 57
Code Lines 37

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 132

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 11
eloc 37
nc 164
nop 2
dl 0
loc 57
ccs 0
cts 37
cp 0
crap 132
rs 6.7833
c 2
b 0
f 0

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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
use midcom_services_auth;
17
18
/**
19
 * Cleanup dangling privileges
20
 *
21
 * @package midcom.console
22
 */
23
class privileges extends Command
24
{
25
    private midcom_services_auth $auth;
26
27
    public function __construct(midcom_services_auth $auth)
28
    {
29
        $this->auth = $auth;
30
        parent::__construct();
31
    }
32
33
    protected function configure()
34
    {
35
        $this->setName('midcom:cleanup:privileges')
36
            ->setAliases(['privilegecleanup'])
37
            ->setDescription('Cleanup dangling privileges')
38
            ->addOption('dry', 'd', InputOption::VALUE_NONE, 'If set, privileges will not be deleted');
39
    }
40
41
    protected function execute(InputInterface $input, OutputInterface $output) : int
42
    {
43
        if ($dry = $input->getOption("dry")) {
44
            $output->writeln("<comment>Running in dry mode!</comment>");
45
        }
46
47
        $qb = \midcom_core_privilege_db::new_query_builder();
48
        $output->writeln("<comment>Checking privileges</comment>");
49
50
        $progress = new ProgressBar($output, $qb->count());
51
        $progress->setRedrawFrequency(100);
52
        $progress->start();
53
54
        $seen_parents = $seen_assignees = [];
55
        $checker = new \midcom_core_privilege;
56
        $to_delete = [];
57
        foreach ($qb->iterate() as $priv) {
58
            if (!array_key_exists($priv->objectguid, $seen_parents)) {
59
                try {
60
                    \midgard_object_class::get_object_by_guid($priv->objectguid);
61
                    $seen_parents[$priv->objectguid] = true;
62
                } catch (\Exception $e) {
63
                    $seen_parents[$priv->objectguid] = false;
64
                }
65
            }
66
            if (!$seen_parents[$priv->objectguid]) {
67
                $to_delete[$priv->guid] = $priv;
68
            }
69
            if (!$checker->is_magic_assignee($priv->assignee)) {
70
                if (!array_key_exists($priv->assignee, $seen_assignees)) {
71
                    $seen_assignees[$priv->assignee] = (bool) $this->auth->get_assignee($priv->assignee);
72
                }
73
                if (!$seen_assignees[$priv->assignee]) {
74
                    $to_delete[$priv->guid] = $priv;
75
                }
76
            }
77
78
            $progress->advance();
79
        }
80
        $progress->finish();
81
82
        $output->writeln("\nFound <info>" . count($to_delete) . "</info> dangling privileges");
83
84
        if (!$dry) {
85
            $output->writeln("<comment>Deleting privileges</comment>");
86
            $progress = new ProgressBar($output, count($to_delete));
87
            $progress->start();
88
89
            foreach ($to_delete as $priv) {
90
                $priv->purge();
91
                $progress->advance();
92
            }
93
            $progress->finish();
94
        }
95
96
        $output->writeln("\nDone");
97
        return 0;
98
    }
99
}
100