Passed
Pull Request — master (#19)
by Thijs
07:55
created

MetaRefresh::runRefresh()   F

Complexity

Conditions 15
Paths 400

Size

Total Lines 108
Code Lines 65

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 15
eloc 65
c 1
b 0
f 0
nc 400
nop 1
dl 0
loc 108
rs 2.5833

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
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\Module;
12
13
class MetaRefresh
14
{
15
    /**
16
     * @var \SimpleSAML\Configuration
17
     */
18
    private Configuration $config;
19
20
    /**
21
     * @var \SimpleSAML\Configuration
22
     */
23
    private Configuration $modconfig;
24
25
    public function __construct()
26
    {
27
        $this->config = Configuration::getInstance();
28
        $this->modconfig = Configuration::getOptionalConfig('module_metarefresh.php');
29
    }
30
31
    /**
32
     * @param string $crontag Only refresh sets which allow this crontag
33
     */
34
    public function runRefresh(string $crontag = null): void
35
    {
36
        $sets = $this->modconfig->getArray('sets');
37
        /** @var string $datadir */
38
        $datadir = $this->config->getPathValue('datadir', 'data/');
39
        $stateFile = $datadir . 'metarefresh-state.php';
40
41
        foreach ($sets as $setkey => $set) {
42
            $set = Configuration::loadFromArray($set);
43
44
            // Only process sets where cron matches the current cron tag
45
            $cronTags = $set->getArray('cron');
46
            if ($crontag !== null && !in_array($crontag, $cronTags, true)) {
47
                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...
48
                continue;
49
            }
50
51
            Logger::info('[metarefresh]: Executing set [' . $setkey . ']');
52
53
            $expireAfter = $set->getInteger('expireAfter', null);
0 ignored issues
show
Unused Code introduced by
The call to SimpleSAML\Configuration::getInteger() has too many arguments starting with null. ( Ignorable by Annotation )

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

53
            /** @scrutinizer ignore-call */ 
54
            $expireAfter = $set->getInteger('expireAfter', null);

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...
54
            if ($expireAfter !== null) {
55
                $expire = time() + $expireAfter;
56
            } else {
57
                $expire = null;
58
            }
59
60
            $outputDir = $set->getString('outputDir');
61
            $outputDir = $this->config->resolvePath($outputDir);
62
            if ($outputDir === null) {
63
                throw new \Exception("Invalid outputDir specified.");
64
            }
65
66
            $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

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