Completed
Push — master ( 394d43...52d8c5 )
by Tim
15s queued 11s
created

MetaRefresh::main()   C

Complexity

Conditions 10
Paths 295

Size

Total Lines 84
Code Lines 53

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 10
eloc 53
c 1
b 0
f 0
nc 295
nop 0
dl 0
loc 84
rs 5.542

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\Controller;
6
7
use Exception;
8
use SimpleSAML\Auth;
9
use SimpleSAML\Configuration;
10
use SimpleSAML\Error;
11
use SimpleSAML\Logger;
12
use SimpleSAML\Module\metarefresh\MetaLoader;
13
use SimpleSAML\Session;
14
use SimpleSAML\Utils;
15
use SimpleSAML\XHTML\Template;
16
use Symfony\Component\HttpFoundation\Response;
17
use Symfony\Component\HttpFoundation\Request;
18
19
/**
20
 * Controller class for the metarefresh module.
21
 *
22
 * This class serves the different views available in the module.
23
 *
24
 * @package SimpleSAML\Module\metarefresh
25
 */
26
27
class MetaRefresh
28
{
29
    /** @var \SimpleSAML\Configuration */
30
    protected $config;
31
32
    /** @var \SimpleSAML\Session */
33
    protected $session;
34
35
    /** @var \SimpleSAML\Configuration */
36
    protected $module_config;
37
38
    /**
39
     * @var \SimpleSAML\Utils\Auth|string
40
     * @psalm-var \SimpleSAML\Utils\Auth|class-string
41
     */
42
    protected $authUtils = Utils\Auth::class;
43
44
45
    /**
46
     * Controller constructor.
47
     *
48
     * It initializes the global configuration and auth source configuration for the controllers implemented here.
49
     *
50
     * @param \SimpleSAML\Configuration              $config The configuration to use by the controllers.
51
     * @param \SimpleSAML\Session                    $session The session to use by the controllers.
52
     *
53
     * @throws \Exception
54
     */
55
    public function __construct(
56
        Configuration $config,
57
        Session $session
58
    ) {
59
        $this->config = $config;
60
        $this->session = $session;
61
    }
62
63
64
    /**
65
     * Inject the \SimpleSAML\Utils\Auth dependency.
66
     *
67
     * @param \SimpleSAML\Utils\Auth $authUtils
68
     */
69
    public function setAuthUtils(Utils\Auth $authUtils): void
70
    {
71
        $this->authUtils = $authUtils;
72
    }
73
74
75
    /**
76
     * Inject the \SimpleSAML\Configuration dependency.
77
     *
78
     * @param \SimpleSAML\Configuration $module_config
79
     */
80
    public function setModuleConfig(Configuration $module_config): void
81
    {
82
        $this->module_config = $module_config;
83
    }
84
85
86
    /**
87
     * @return \SimpleSAML\XHTML\Template
88
     */
89
    public function main(): Template
90
    {
91
        $this->authUtils::requireAdmin();
92
93
        Logger::setCaptureLog(true);
94
        $sets = $this->module_config->getArray('sets', []);
95
96
        foreach ($sets as $setkey => $set) {
97
            $set = Configuration::loadFromArray($set);
98
99
            Logger::info('[metarefresh]: Executing set [' . $setkey . ']');
100
101
            try {
102
                $expireAfter = $set->getInteger('expireAfter', null);
103
                if ($expireAfter !== null) {
104
                    $expire = time() + $expireAfter;
105
                } else {
106
                    $expire = null;
107
                }
108
                $metaloader = new MetaLoader($expire);
109
110
                // Get global black/whitelists
111
                $blacklist = $this->module_config->getArray('blacklist', []);
112
                $whitelist = $this->module_config->getArray('whitelist', []);
113
114
                // get global type filters
115
                $available_types = [
116
                    'saml20-idp-remote',
117
                    'saml20-sp-remote',
118
                    'attributeauthority-remote'
119
                ];
120
                $set_types = $set->getArrayize('types', $available_types);
121
122
                foreach ($set->getArray('sources') as $source) {
123
                    // filter metadata by type of entity
124
                    if (isset($source['types'])) {
125
                        $metaloader->setTypes($source['types']);
126
                    } else {
127
                        $metaloader->setTypes($set_types);
128
                    }
129
130
                    // Merge global and src specific blacklists
131
                    if (isset($source['blacklist'])) {
132
                        $source['blacklist'] = array_unique(array_merge($source['blacklist'], $blacklist));
133
                    } else {
134
                        $source['blacklist'] = $blacklist;
135
                    }
136
137
                    // Merge global and src specific whitelists
138
                    if (isset($source['whitelist'])) {
139
                        $source['whitelist'] = array_unique(array_merge($source['whitelist'], $whitelist));
140
                    } else {
141
                        $source['whitelist'] = $whitelist;
142
                    }
143
144
                    Logger::debug(
145
                        '[metarefresh]: In set [' . $setkey . '] loading source [' . $source['src'] . ']'
146
                    );
147
                    $metaloader->loadSource($source);
148
                }
149
150
                $outputDir = $set->getString('outputDir');
151
                $outputDir = Utils\System::resolvePath($outputDir);
152
153
                $outputFormat = $set->getValueValidate('outputFormat', ['flatfile', 'serialize'], 'flatfile');
154
                switch ($outputFormat) {
155
                    case 'flatfile':
156
                        $metaloader->writeMetadataFiles($outputDir);
157
                        break;
158
                    case 'serialize':
159
                        $metaloader->writeMetadataSerialize($outputDir);
160
                        break;
161
                }
162
            } catch (Exception $e) {
163
                $e = Error\Exception::fromException($e);
164
                $e->logWarning();
165
            }
166
        }
167
168
        $logentries = Logger::getCapturedLog();
169
170
        $t = new Template($this->config, 'metarefresh:fetch.twig');
171
        $t->data['logentries'] = $logentries;
172
        return $t;
173
    }
174
}
175