Passed
Push — master ( 52d8c5...e14e38 )
by Tim
02:49 queued 46s
created

metarefresh_hook_cron()   F

Complexity

Conditions 15
Paths 5390

Size

Total Lines 120
Code Lines 72

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 15
eloc 72
c 1
b 0
f 0
nc 5390
nop 1
dl 0
loc 120
rs 1.7499

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
use SimpleSAML\Configuration;
4
use SimpleSAML\Logger;
5
use Webmozart\Assert\Assert;
6
7
/**
8
 * Hook to run a cron job.
9
 *
10
 * @param array &$croninfo  Output
11
 * @return void
12
 */
13
function metarefresh_hook_cron(array &$croninfo): void
14
{
15
    Assert::keyExists($croninfo, 'summary');
16
    Assert::keyExists($croninfo, 'tag');
17
18
    Logger::info('cron [metarefresh]: Running cron in cron tag [' . $croninfo['tag'] . '] ');
19
20
    try {
21
        $config = Configuration::getInstance();
22
        $mconfig = Configuration::getOptionalConfig('config-metarefresh.php');
23
24
        $sets = $mconfig->getArray('sets', []);
25
        /** @var string $datadir */
26
        $datadir = $config->getPathValue('datadir', 'data/');
27
        $stateFile = $datadir . 'metarefresh-state.php';
28
29
        foreach ($sets as $setkey => $set) {
30
            $set = Configuration::loadFromArray($set);
31
32
            // Only process sets where cron matches the current cron tag
33
            $cronTags = $set->getArray('cron');
34
            if (!in_array($croninfo['tag'], $cronTags, true)) {
35
                continue;
36
            }
37
38
            Logger::info('cron [metarefresh]: Executing set [' . $setkey . ']');
39
40
            $expireAfter = $set->getInteger('expireAfter', null);
41
            if ($expireAfter !== null) {
42
                $expire = time() + $expireAfter;
43
            } else {
44
                $expire = null;
45
            }
46
47
            $outputDir = $set->getString('outputDir');
48
            $outputDir = $config->resolvePath($outputDir);
49
            if ($outputDir === null) {
50
                throw new \Exception("Invalid outputDir specified.");
51
            }
52
53
            $outputFormat = $set->getValueValidate('outputFormat', ['flatfile', 'serialize'], 'flatfile');
54
55
            $oldMetadataSrc = \SimpleSAML\Metadata\MetaDataStorageSource::getSource([
56
                'type' => $outputFormat,
57
                'directory' => $outputDir,
58
            ]);
59
60
            $metaloader = new \SimpleSAML\Module\metarefresh\MetaLoader($expire, $stateFile, $oldMetadataSrc);
61
62
            // Get global blacklist, whitelist, attributewhitelist and caching info
63
            $blacklist = $mconfig->getArray('blacklist', []);
64
            $whitelist = $mconfig->getArray('whitelist', []);
65
            $attributewhitelist = $mconfig->getArray('attributewhitelist', []);
66
            $conditionalGET = $mconfig->getBoolean('conditionalGET', false);
67
68
            // get global type filters
69
            $available_types = [
70
                'saml20-idp-remote',
71
                'saml20-sp-remote',
72
                'attributeauthority-remote'
73
            ];
74
            $set_types = $set->getArrayize('types', $available_types);
75
76
            foreach ($set->getArray('sources') as $source) {
77
                // filter metadata by type of entity
78
                if (isset($source['types'])) {
79
                    $metaloader->setTypes($source['types']);
80
                } else {
81
                    $metaloader->setTypes($set_types);
82
                }
83
84
                // Merge global and src specific blacklists
85
                if (isset($source['blacklist'])) {
86
                    $source['blacklist'] = array_unique(array_merge($source['blacklist'], $blacklist));
87
                } else {
88
                    $source['blacklist'] = $blacklist;
89
                }
90
91
                // Merge global and src specific whitelists
92
                if (isset($source['whitelist'])) {
93
                    $source['whitelist'] = array_unique(array_merge($source['whitelist'], $whitelist));
94
                } else {
95
                    $source['whitelist'] = $whitelist;
96
                }
97
98
                # Merge global and src specific attributewhitelists: cannot use array_unique for multi-dim.
99
                if (isset($source['attributewhitelist'])) {
100
                    $source['attributewhitelist'] = array_merge($source['attributewhitelist'], $attributewhitelist);
101
                } else {
102
                    $source['attributewhitelist'] = $attributewhitelist;
103
                }
104
105
                // Let src specific conditionalGET override global one
106
                if (!isset($source['conditionalGET'])) {
107
                    $source['conditionalGET'] = $conditionalGET;
108
                }
109
110
                Logger::debug('cron [metarefresh]: In set [' . $setkey . '] loading source [' . $source['src'] . ']');
111
                $metaloader->loadSource($source);
112
            }
113
114
            // Write state information back to disk
115
            $metaloader->writeState();
116
117
            switch ($outputFormat) {
118
                case 'flatfile':
119
                    $metaloader->writeMetadataFiles($outputDir);
120
                    break;
121
                case 'serialize':
122
                    $metaloader->writeMetadataSerialize($outputDir);
123
                    break;
124
            }
125
126
            if ($set->hasValue('arp')) {
127
                $arpconfig = Configuration::loadFromArray($set->getValue('arp'));
0 ignored issues
show
Bug introduced by
It seems like $set->getValue('arp') can also be of type null; however, parameter $config of SimpleSAML\Configuration::loadFromArray() does only seem to accept array, 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

127
                $arpconfig = Configuration::loadFromArray(/** @scrutinizer ignore-type */ $set->getValue('arp'));
Loading history...
128
                $metaloader->writeARPfile($arpconfig);
129
            }
130
        }
131
    } catch (\Exception $e) {
132
        $croninfo['summary'][] = 'Error during metarefresh: ' . $e->getMessage();
133
    }
134
}
135