Passed
Push — master ( c47978...017115 )
by Tim
06:46
created

Aggregator::__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\aggregator2\Controller;
6
7
use SimpleSAML\Configuration;
8
use SimpleSAML\Error;
9
use SimpleSAML\Module;
10
use SimpleSAML\Module\aggregator2\Aggregator as AttributeAggregator;
11
use SimpleSAML\Session;
12
use SimpleSAML\Utils;
13
use SimpleSAML\XHTML\Template;
14
use Symfony\Component\HttpFoundation\Request;
15
use Symfony\Component\HttpFoundation\Response;
16
17
/**
18
 * Controller class for the aggregator2 module.
19
 *
20
 * This class serves the different views available in the module.
21
 *
22
 * @package simplesamlphp/simplesamlphp-module-aggregator2
23
 */
24
class Aggregator
25
{
26
    /** @var \SimpleSAML\Configuration */
27
    protected Configuration $config;
28
29
    /** @var \SimpleSAML\Configuration */
30
    protected Configuration $moduleConfig;
31
32
    /** @var \SimpleSAML\Session */
33
    protected Session $session;
34
35
    /** @var string[] */
36
    private static array $allowedMimeTypes = [
37
        'text/plain',
38
        'application/samlmetadata-xml',
39
        'application/xml',
40
    ];
41
42
    /**
43
     * Controller constructor.
44
     *
45
     * It initializes the global configuration and session for the controllers implemented here.
46
     *
47
     * @param \SimpleSAML\Configuration $config The configuration to use by the controllers.
48
     * @param \SimpleSAML\Session $session The session to use by the controllers.
49
     *
50
     * @throws \Exception
51
     */
52
    public function __construct(
53
        Configuration $config,
54
        Session $session
55
    ) {
56
        $this->config = $config;
57
        $this->moduleConfig = Configuration::getConfig('module_aggregator2.php');
58
        $this->session = $session;
59
    }
60
61
62
    /**
63
     * @return \SimpleSAML\XHTML\Template
64
     */
65
    public function main(): Template
66
    {
67
        // get list of sources
68
        $names = array_keys($this->moduleConfig->toArray());
69
70
        $sources = [];
71
        foreach ($names as $name) {
72
            $encId = urlencode($name);
73
74
            $sources[$name] = [
75
                'name' => Module::getModuleURL(
76
                    'aggregator2/get.php',
77
                    ['id' => $encId]
78
                ),
79
                'text' => Module::getModuleURL(
80
                    'aggregator2/get.php',
81
                    ['id' => $encId, 'mimetype' => 'text/plain']
82
                ),
83
                'xml' => Module::getModuleURL(
84
                    'aggregator2/get.php',
85
                    ['id' => $encId, 'mimetype' => 'application/xml']
86
                ),
87
            ];
88
        }
89
90
        $t = new Template($this->config, 'aggregator2:list.twig');
91
        $t->data['sources'] = $sources;
92
93
        return $t;
94
    }
95
96
97
    /**
98
     * @param \Symfony\Component\HttpFoundation\Request $request The current request.
99
     *
100
     * @return \Symfony\Component\HttpFoundation\Response
101
     */
102
    public function get(Request $request): Response
103
    {
104
        $id = $request->get('id');
105
        if ($id === null) {
106
            throw new Error\BadRequest('Missing required parameter "id".');
107
        }
108
        $id = strval($id);
109
110
        $sets = [];
111
        $set = $request->get('set');
112
        if ($set !== null) {
113
            $sets = explode(',', $set);
114
        }
115
116
        $excluded_entities = [];
117
        $exclude = $request->get('exclude');
118
        if ($exclude !== null) {
119
            $excluded_entities = explode(',', $exclude);
120
        }
121
122
        $aggregator = AttributeAggregator::getAggregator($id);
123
        $aggregator->setFilters($sets);
124
        $aggregator->excludeEntities($excluded_entities);
125
        $xml = $aggregator->getMetadata();
126
127
        $mimeType = $request->get('mimetype');
128
        if (in_array($mimeType, self::$allowedMimeTypes)) {
129
            $mime = $mimeType;
130
131
            if ($mime === 'text/plain') {
132
                $xmlUtils = new Utils\XML();
133
                $xml = $xmlUtils->formatXMLString($xml);
134
            }
135
        } else {
136
            $mime = 'application/samlmetadata+xml';
137
        }
138
139
        $response = new Response();
140
        $response->headers->set('Content-Type', $mime);
141
        $response->headers->set('Content-Length', strval(strlen($xml)));
142
        $response->headers->set('Content-Disposition', 'filename=' . $id . '.xml');
143
        $response->setContent($xml);
144
145
        return $response;
146
    }
147
}
148