Passed
Push — master ( e21463...a44c43 )
by Thijs
02:36
created

MetaRefresh   A

Complexity

Total Complexity 16

Size/Duplication

Total Lines 133
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 70
c 1
b 0
f 0
dl 0
loc 133
rs 10
wmc 16

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
F runRefresh() 0 108 15
1
<?php
2
3
declare(strict_types=1);
4
5
namespace SimpleSAML\Module\metarefresh;
6
7
use Exception;
8
use SimpleSAML\Assert\Assert;
9
use SimpleSAML\Configuration;
10
use SimpleSAML\Logger;
11
use SimpleSAML\Metadata\MetaDataStorageSource;
12
use SimpleSAML\Module;
13
14
class MetaRefresh
15
{
16
    /**
17
     * @var \SimpleSAML\Configuration
18
     */
19
    private Configuration $config;
20
21
    /**
22
     * @var \SimpleSAML\Configuration
23
     */
24
    private Configuration $modconfig;
25
26
    /**
27
     * @param \SimpleSAML\Configuration              $config The configuration to use by the module.
28
     * @param \SimpleSAML\Configuration              $modconfig The module-specific configuration to use by the module.
29
     */
30
    public function __construct(Configuration $config, Configuration $modconfig)
31
    {
32
        $this->config = $config;
33
        $this->modconfig = $modconfig;
34
    }
35
36
    /**
37
     * @param string $crontag Only refresh sets which allow this crontag
38
     */
39
    public function runRefresh(string $crontag = null): void
40
    {
41
        $sets = $this->modconfig->getArray('sets');
42
        /** @var string $datadir */
43
        $datadir = $this->config->getPathValue('datadir', 'data/');
44
        $stateFile = $datadir . 'metarefresh-state.php';
45
46
        foreach ($sets as $setkey => $set) {
47
            $set = Configuration::loadFromArray($set);
48
49
            // Only process sets where cron matches the current cron tag
50
            $cronTags = $set->getArray('cron');
51
            if ($crontag !== null && !in_array($crontag, $cronTags, true)) {
52
                Logger::debug('[metarefresh]: Skipping set [' . $setkey . '], not allowed for cron tag ' . $tag);
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $tag seems to be never defined.
Loading history...
53
                continue;
54
            }
55
56
            Logger::info('[metarefresh]: Executing set [' . $setkey . ']');
57
58
            $expireAfter = $set->getOptionalInteger('expireAfter', null);
59
            if ($expireAfter !== null) {
60
                $expire = time() + $expireAfter;
61
            } else {
62
                $expire = null;
63
            }
64
65
            $outputDir = $set->getString('outputDir');
66
            $outputDir = $this->config->resolvePath($outputDir);
67
            if ($outputDir === null) {
68
                throw new Exception("Invalid outputDir specified.");
69
            }
70
71
            $outputFormat = $set->getValueValidate('outputFormat', ['flatfile', 'serialize'], 'flatfile');
0 ignored issues
show
Unused Code introduced by
The call to SimpleSAML\Configuration::getValueValidate() has too many arguments starting with 'flatfile'. ( Ignorable by Annotation )

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

71
            /** @scrutinizer ignore-call */ 
72
            $outputFormat = $set->getValueValidate('outputFormat', ['flatfile', 'serialize'], 'flatfile');

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.

Loading history...
72
73
            $oldMetadataSrc = MetaDataStorageSource::getSource([
74
                'type' => $outputFormat,
75
                'directory' => $outputDir,
76
            ]);
77
78
            $metaloader = new MetaLoader($expire, $stateFile, $oldMetadataSrc);
79
80
            // Get global blacklist, whitelist, attributewhitelist and caching info
81
            $blacklist = $this->modconfig->getOptionalArray('blacklist', []);
82
            $whitelist = $this->modconfig->getOptionalArray('whitelist', []);
83
            $attributewhitelist = $this->modconfig->getOptionalArray('attributewhitelist', []);
84
            $conditionalGET = $this->modconfig->getOptionalBoolean('conditionalGET', false);
85
86
            // get global type filters
87
            $available_types = [
88
                'saml20-idp-remote',
89
                'saml20-sp-remote',
90
                'attributeauthority-remote'
91
            ];
92
            $set_types = $set->getOptionalArray('types', $available_types);
93
94
            foreach ($set->getArray('sources') as $source) {
95
                // filter metadata by type of entity
96
                if (isset($source['types'])) {
97
                    $metaloader->setTypes($source['types']);
98
                } else {
99
                    $metaloader->setTypes($set_types);
100
                }
101
102
                // Merge global and src specific blacklists
103
                if (isset($source['blacklist'])) {
104
                    $source['blacklist'] = array_unique(array_merge($source['blacklist'], $blacklist));
105
                } else {
106
                    $source['blacklist'] = $blacklist;
107
                }
108
109
                // Merge global and src specific whitelists
110
                if (isset($source['whitelist'])) {
111
                    $source['whitelist'] = array_unique(array_merge($source['whitelist'], $whitelist));
112
                } else {
113
                    $source['whitelist'] = $whitelist;
114
                }
115
116
                # Merge global and src specific attributewhitelists: cannot use array_unique for multi-dim.
117
                if (isset($source['attributewhitelist'])) {
118
                    $source['attributewhitelist'] = array_merge($source['attributewhitelist'], $attributewhitelist);
119
                } else {
120
                    $source['attributewhitelist'] = $attributewhitelist;
121
                }
122
123
                // Let src specific conditionalGET override global one
124
                if (!isset($source['conditionalGET'])) {
125
                    $source['conditionalGET'] = $conditionalGET;
126
                }
127
128
                Logger::debug('[metarefresh]: In set [' . $setkey . '] loading source [' . $source['src'] . ']');
129
                $metaloader->loadSource($source);
130
            }
131
132
            // Write state information back to disk
133
            $metaloader->writeState();
134
135
            switch ($outputFormat) {
136
                case 'flatfile':
137
                    $metaloader->writeMetadataFiles($outputDir);
138
                    break;
139
                case 'serialize':
140
                    $metaloader->writeMetadataSerialize($outputDir);
141
                    break;
142
            }
143
144
            if ($set->hasValue('arp')) {
145
                $arpconfig = Configuration::loadFromArray($set->getValue('arp'));
146
                $metaloader->writeARPfile($arpconfig);
147
            }
148
        }
149
    }
150
}
151