Issues (806)

lib/midcom/cron/purgedeleted.php (1 issue)

Labels
Severity
1
<?php
2
/**
3
 * @package midcom.services
4
 * @author The Midgard Project, http://www.midgard-project.org
5
 * @copyright The Midgard Project, http://www.midgard-project.org
6
 * @license http://www.gnu.org/licenses/lgpl.html GNU Lesser General Public License
7
 */
8
9
/**
10
 * @package midcom.services
11
 */
12
class midcom_cron_purgedeleted extends midcom_baseclasses_components_cron_handler
13
{
14
    private int $_cutoff;
15
16
    public function set_cutoff(int $days)
17
    {
18
        $this->_cutoff = strtotime($days . ' days ago');
19
    }
20
21
    public function get_cutoff() : int
22
    {
23
        if (empty($this->_cutoff)) {
24
            $this->set_cutoff(midcom::get()->config->get('cron_purge_deleted_after'));
0 ignored issues
show
It seems like midcom::get()->config->g...n_purge_deleted_after') can also be of type null; however, parameter $days of midcom_cron_purgedeleted::set_cutoff() does only seem to accept integer, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

24
            $this->set_cutoff(/** @scrutinizer ignore-type */ midcom::get()->config->get('cron_purge_deleted_after'));
Loading history...
25
        }
26
        return $this->_cutoff;
27
    }
28
29
    public function get_classes() : array
30
    {
31
        return array_filter(midcom_connection::get_schema_types(), function($mgdschema) {
32
            return midgard_reflector_object::has_metadata_class($mgdschema);
33
        });
34
    }
35
36
    public function execute()
37
    {
38
        $cut_off = $this->get_cutoff();
39
        debug_add('Purging entries deleted before ' . gmdate('Y-m-d H:i:s', $cut_off) . "\n");
40
        foreach ($this->get_classes() as $mgdschema) {
41
            debug_add("Processing class {$mgdschema}");
42
            $stats = $this->process_class($mgdschema);
43
44
            foreach ($stats['errors'] as $error) {
45
                debug_add($error, MIDCOM_LOG_ERROR);
46
            }
47
            if ($stats['found'] > 0) {
48
                debug_add("  Found {$stats['found']} deleted {$mgdschema} objects, purged {$stats['purged']}\n", MIDCOM_LOG_INFO);
49
            } else {
50
                debug_add("  No {$mgdschema} objects deleted before cutoff date found\n");
51
            }
52
        }
53
    }
54
55
    public function process_class(string $mgdschema) : array
56
    {
57
        $cut_off = $this->get_cutoff();
58
        $qb = new midgard_query_builder($mgdschema);
59
        $qb->add_constraint('metadata.deleted', '<>', 0);
60
        $qb->add_constraint('metadata.revised', '<', gmdate('Y-m-d H:i:s', $cut_off));
61
        $qb->include_deleted();
62
63
        $stats = [
64
            'found' => 0,
65
            'purged' => 0,
66
            'errors' => []
67
        ];
68
69
        foreach ($qb->iterate() as $obj) {
70
            $stats['found']++;
71
            if (!$obj->purge()) {
72
                $stats['errors'][] = "Failed to purge {$obj->guid}, deleted: {$obj->metadata->deleted}, revised: {$obj->metadata->revised}. errstr: " . midcom_connection::get_error_string();
73
                debug_print_r('Purge failed for object', $obj);
74
                continue;
75
            }
76
            $stats['purged']++;
77
        }
78
        return $stats;
79
    }
80
}
81