Passed
Pull Request — master (#11)
by Thijs
08:30
created

DiscoPower   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 61
Duplicated Lines 0 %

Importance

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

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 SimpleSAML\Configuration;
8
use SimpleSAML\Error;
9
use SimpleSAML\Session;
10
use SimpleSAML\XHTML\Template;
11
use Symfony\Component\HttpFoundation\Request;
12
use Symfony\Component\HttpFoundation\Response;
13
use Symfony\Component\HttpFoundation\JsonResponse;
14
15
class DiscoPower
16
{
17
    /**
18
     * @param \Symfony\Component\HttpFoundation\Request $request The current request.
19
     */
20
    public function main(Request $request): void
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

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

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