Issues (14)

hooks/hook_cron.php (1 issue)

1
<?php
2
3
declare(strict_types=1);
4
5
use Exception;
6
use SimpleSAML\Assert\Assert;
7
use SimpleSAML\Configuration;
8
use SimpleSAML\Logger;
9
use SimpleSAML\Module\statistics\Aggregator;
10
11
/**
12
 * Hook to run a cron job.
13
 *
14
 * @param array &$croninfo  Output
15
 */
16
function statistics_hook_cron(array &$croninfo): void
17
{
18
    Assert::keyExists($croninfo, 'summary');
19
    Assert::keyExists($croninfo, 'tag');
20
21
    $statconfig = Configuration::getConfig('module_statistics.php');
22
23
    if (is_null($statconfig->getOptionalValue('cron_tag', null))) {
24
        return;
25
    }
26
    if ($statconfig->getOptionalValue('cron_tag', null) !== $croninfo['tag']) {
27
        return;
28
    }
29
30
    $maxtime = $statconfig->getOptionalInteger('time_limit', null);
31
    if ($maxtime) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $maxtime of type integer|null is loosely compared to true; this is ambiguous if the integer can be 0. You might want to explicitly use !== null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For integer values, zero is a special case, in particular the following results might be unexpected:

0   == false // true
0   == null  // true
123 == false // false
123 == null  // false

// It is often better to use strict comparison
0 === false // false
0 === null  // false
Loading history...
32
        set_time_limit($maxtime);
33
    }
34
35
    try {
36
        $aggregator = new Aggregator();
37
        $results = $aggregator->aggregate();
38
        if (empty($results)) {
39
            Logger::notice('Output from statistics aggregator was empty.');
40
        } else {
41
            $aggregator->store($results);
42
        }
43
    } catch (Exception $e) {
44
        $message = 'Loganalyzer threw exception: ' . $e->getMessage();
45
        Logger::warning($message);
46
        $croninfo['summary'][] = $message;
47
    }
48
}
49