Passed
Push — master ( 12ce73...9dd436 )
by Thijs
02:15
created

DiscoPower::main()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 17
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 3
eloc 10
c 1
b 0
f 0
nc 3
nop 1
dl 0
loc 17
rs 9.9332
1
<?php
2
3
declare(strict_types=1);
4
5
namespace SimpleSAML\Module\discopower\Controller;
6
7
use Exception;
8
use SimpleSAML\Configuration;
9
use SimpleSAML\Error;
10
use SimpleSAML\HTTP\RunnableResponse;
11
use SimpleSAML\Module\discopower\PowerIdPDisco;
12
use SimpleSAML\Session;
13
use SimpleSAML\XHTML\Template;
14
use Symfony\Component\HttpFoundation\Request;
15
use Symfony\Component\HttpFoundation\Response;
16
use Symfony\Component\HttpFoundation\JsonResponse;
17
18
class DiscoPower
19
{
20
    /**
21
     * @param \Symfony\Component\HttpFoundation\Request $request The current request.
22
     * @return \SimpleSAML\HTTP\RunnableResponse
23
     */
24
    public function main(Request $request): RunnableResponse
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

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

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