Passed
Pull Request — master (#12)
by Tim
03:54
created

MetaRefresh::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 3
c 1
b 0
f 0
nc 1
nop 2
dl 0
loc 7
rs 10
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
        $this->module_config = Configuration::getOptionalConfig('config-metarefresh.php');
62
    }
63
64
65
    /**
66
     * Inject the \SimpleSAML\Utils\Auth dependency.
67
     *
68
     * @param \SimpleSAML\Utils\Auth $authUtils
69
     */
70
    public function setAuthUtils(Utils\Auth $authUtils): void
71
    {
72
        $this->authUtils = $authUtils;
73
    }
74
75
76
    /**
77
     * @param \Symfony\Component\HttpFoundation\Request $request
78
     * @return \SimpleSAML\XHTML\Template
79
     */
80
    public function main(Request $request): Template
0 ignored issues
show
Unused Code introduced by
The parameter $request is not used and could be removed. ( Ignorable by Annotation )

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

80
    public function main(/** @scrutinizer ignore-unused */ Request $request): Template

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
81
    {
82
        $this->authUtils::requireAdmin();
83
84
        Logger::setCaptureLog(true);
85
        $sets = $this->module_config->getArray('sets', []);
86
87
        foreach ($sets as $setkey => $set) {
88
            $set = Configuration::loadFromArray($set);
89
90
            Logger::info('[metarefresh]: Executing set [' . $setkey . ']');
91
92
            try {
93
                $expireAfter = $set->getInteger('expireAfter', null);
94
                if ($expireAfter !== null) {
95
                    $expire = time() + $expireAfter;
96
                } else {
97
                    $expire = null;
98
                }
99
                $metaloader = new MetaLoader($expire);
100
101
                // Get global black/whitelists
102
                $blacklist = $mconfig->getArray('blacklist', []);
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $mconfig seems to be never defined.
Loading history...
103
                $whitelist = $mconfig->getArray('whitelist', []);
104
105
                // get global type filters
106
                $available_types = [
107
                    'saml20-idp-remote',
108
                    'saml20-sp-remote',
109
                    'attributeauthority-remote'
110
                ];
111
                $set_types = $set->getArrayize('types', $available_types);
112
113
                foreach ($set->getArray('sources') as $source) {
114
                    // filter metadata by type of entity
115
                    if (isset($source['types'])) {
116
                        $metaloader->setTypes($source['types']);
117
                    } else {
118
                        $metaloader->setTypes($set_types);
119
                    }
120
121
                    // Merge global and src specific blacklists
122
                    if (isset($source['blacklist'])) {
123
                        $source['blacklist'] = array_unique(array_merge($source['blacklist'], $blacklist));
124
                    } else {
125
                        $source['blacklist'] = $blacklist;
126
                    }
127
128
                    // Merge global and src specific whitelists
129
                    if (isset($source['whitelist'])) {
130
                        $source['whitelist'] = array_unique(array_merge($source['whitelist'], $whitelist));
131
                    } else {
132
                        $source['whitelist'] = $whitelist;
133
                    }
134
135
                    Logger::debug(
136
                        '[metarefresh]: In set [' . $setkey . '] loading source [' . $source['src'] . ']'
137
                    );
138
                    $metaloader->loadSource($source);
139
                }
140
141
                $outputDir = $set->getString('outputDir');
142
                $outputDir = Utils\System::resolvePath($outputDir);
143
144
                $outputFormat = $set->getValueValidate('outputFormat', ['flatfile', 'serialize'], 'flatfile');
145
                switch ($outputFormat) {
146
                    case 'flatfile':
147
                        $metaloader->writeMetadataFiles($outputDir);
148
                        break;
149
                    case 'serialize':
150
                        $metaloader->writeMetadataSerialize($outputDir);
151
                        break;
152
                }
153
            } catch (Exception $e) {
154
                $e = Error\Exception::fromException($e);
155
                $e->logWarning();
156
            }
157
        }
158
159
        $logentries = Logger::getCapturedLog();
160
161
        $t = new Template($this->config, 'metarefresh:fetch.twig');
162
        $t->data['logentries'] = $logentries;
163
        return $t;
164
    }
165
}
166