org_openpsa_directmarketing_cron_cleartokens   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 35
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
eloc 19
dl 0
loc 35
ccs 0
cts 19
cp 0
rs 10
c 0
b 0
f 0
wmc 5

2 Methods

Rating   Name   Duplication   Size   Complexity  
A _on_initialize() 0 10 2
A execute() 0 16 3
1
<?php
2
/**
3
 * @package org.openpsa.directmarketing
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
 * Cron handler for clearing tokens from old send receipts
11
 *
12
 * @package org.openpsa.directmarketing
13
 */
14
class org_openpsa_directmarketing_cron_cleartokens extends midcom_baseclasses_components_cron_handler
15
{
16
    private int $cutoff;
17
18
    public function _on_initialize() : bool
19
    {
20
        $days = $this->_config->get('send_token_max_age');
21
        if ($days == 0) {
22
            debug_add('send_token_max_age evaluates to zero, aborting');
23
            return false;
24
        }
25
        $this->cutoff = time() - ($days * 3600 * 24);
26
27
        return true;
28
    }
29
30
    /**
31
     * Find all old send tokens and clear them.
32
     */
33
    public function execute()
34
    {
35
        //Disable limits, TODO: think if this could be done in smaller chunks to save memory.
36
        midcom::get()->disable_limits();
37
38
        $qb = org_openpsa_directmarketing_campaign_messagereceipt_dba::new_query_builder();
39
        $qb->add_constraint('token', '<>', '');
40
        $qb->add_constraint('metadata.created', '<', $this->cutoff);
41
        $qb->add_constraint('orgOpenpsaObtype', '=', org_openpsa_directmarketing_campaign_messagereceipt_dba::SENT);
42
        $ret = $qb->execute_unchecked();
43
44
        foreach ($ret as $receipt) {
45
            debug_add("clearing token '{$receipt->token}' from receipt #{$receipt->id}");
46
            $receipt->token = '';
47
            if (!$receipt->update()) {
48
                debug_add("FAILED to update receipt #{$receipt->id}, errstr: " . midcom_connection::get_error_string(), MIDCOM_LOG_WARN);
49
            }
50
        }
51
    }
52
}
53