DiscoPower   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 62
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 7
eloc 28
c 1
b 0
f 0
dl 0
loc 62
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A main() 0 17 3
A tablist() 0 30 4
1
<?php
2
3
declare(strict_types=1);
4
5
namespace SimpleSAML\Module\discopower\Controller;
6
7
use Exception;
8
use SimpleSAML\Error;
9
use SimpleSAML\Module\discopower\PowerIdPDisco;
10
use SimpleSAML\Session;
11
use Symfony\Component\HttpFoundation\{JsonResponse, Request, Response, StreamedResponse};
12
13
class DiscoPower
14
{
15
    /**
16
     * @param \Symfony\Component\HttpFoundation\Request $request The current request.
17
     * @return \Symfony\Component\HttpFoundation\StreamedResponse
18
     */
19
    public function main(Request $request): StreamedResponse
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

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

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...
20
    {
21
        try {
22
            $discoHandler = new PowerIdPDisco(
23
                ['saml20-idp-remote'],
24
                'poweridpdisco',
25
            );
26
        } catch (Exception $exception) {
27
            // An error here should be caused by invalid query parameters
28
            throw new Error\Error('DISCOPARAMS', $exception);
29
        }
30
31
        try {
32
            return new StreamedResponse([$discoHandler, 'handleRequest']);
33
        } catch (Exception $exception) {
34
            // An error here should be caused by metadata
35
            throw new Error\Error('METADATA', $exception);
36
        }
37
    }
38
39
    /**
40
     * An AJAX handler to retrieve a list of disco tabs from the session.
41
     * This allows us to dynamically update the tab list without inline javascript.
42
     *
43
     * @param \Symfony\Component\HttpFoundation\Request $request The current request.
44
     */
45
    public function tablist(Request $request): Response
46
    {
47
        $session = Session::getSessionFromRequest();
48
        $tabs = $session->getData('discopower:tabList', 'tabs');
49
        $faventry = $session->getData('discopower:tabList', 'faventry');
50
        $defaulttab = $session->getData('discopower:tabList', 'defaulttab');
51
52
        if (!is_array($tabs)) {
53
            throw new Error\Exception('Could not get tab list from session');
54
        }
55
56
        $response = new JsonResponse();
57
58
        // handle JSONP requests
59
        if ($request->query->has('callback')) {
60
            $callback = $request->query->get('callback');
61
            if (!preg_match('/^[a-z0-9_]+$/i', $callback)) {
62
                throw new Error\Exception('Unsafe JSONP callback function name ' . var_export($callback, true));
63
            }
64
            $response->setCallback($callback);
65
        }
66
67
        $response->setData(
68
            [
69
                'faventry' => $faventry,
70
                'default' => $defaulttab,
71
                'tabs' => $tabs,
72
            ],
73
        );
74
        return $response;
75
    }
76
}
77