Aggregator   A
last analyzed

Complexity

Total Complexity 8

Size/Duplication

Total Lines 91
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 39
dl 0
loc 91
rs 10
c 1
b 0
f 0
wmc 8

3 Methods

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