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

purgedeleted::execute()   A

Complexity

Conditions 4
Paths 5

Size

Total Lines 30
Code Lines 23

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 20

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 4
eloc 23
nc 5
nop 2
dl 0
loc 30
ccs 0
cts 24
cp 0
crap 20
rs 9.552
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\console\loginhelper;
16
17
/**
18
 * Purge deleted objects
19
 *
20
 * @package midcom.console
21
 */
22
class purgedeleted extends Command
23
{
24
    use loginhelper;
0 ignored issues
show
Bug introduced by
The trait midcom\console\loginhelper requires the property $auth which is not provided by midcom\console\command\purgedeleted.
Loading history...
25
26 1
    protected function configure()
27
    {
28 1
        $config = new \midcom_config;
29 1
        $this->setName('midcom:purgedeleted')
30 1
            ->setAliases(['purgedeleted'])
31 1
            ->setDescription('Purge deleted objects')
32 1
            ->addOption('days', 'd', InputOption::VALUE_REQUIRED, 'Grace period in days', $config->get('cron_purge_deleted_after'));
33 1
    }
34
35
    protected function interact(InputInterface $input, OutputInterface $output)
36
    {
37
        $dialog = $this->getHelperSet()->get('question');
38
        $this->require_admin($dialog, $input, $output);
39
    }
40
41
    protected function execute(InputInterface $input, OutputInterface $output) : int
42
    {
43
        $handler = new \midcom_cron_purgedeleted;
44
        $handler->set_cutoff((int) $input->getOption('days'));
45
46
        $output->writeln('Purging entries deleted before ' . gmdate('Y-m-d H:i:s', $handler->get_cutoff()));
47
48
        $total_purged = 0;
49
        $total_errors = 0;
50
        $start = microtime(true);
51
        foreach ($handler->get_classes() as $mgdschema) {
52
            $output->writeln("\n\nProcessing class <info>{$mgdschema}</info>");
53
            $purged = 0;
54
            $errors = 0;
55
            $stats = $handler->process_class($mgdschema);
56
            foreach ($stats['errors'] as $error) {
57
                $output->writeln('  <error>' . $error . '</error>');
58
            }
59
            if ($purged > 0) {
60
                $output->write("\x0D");
61
            }
62
            $purged += $stats['purged'];
63
            $errors += count($stats['errors']);
64
            $output->write("  Purged <info>{$purged}</info> deleted objects, <comment>" . $errors . " failures</comment>");
65
            $total_purged += $purged;
66
            $total_errors += $errors;
67
        }
68
        $elapsed = round(microtime(true) - $start, 2);
69
        $output->writeln("\n\nPurged <info>{$total_purged}</info> deleted objects in {$elapsed}s, <comment>" . $total_errors . " failures</comment>");
70
        return 0;
71
    }
72
}
73