Passed
Pull Request — master (#2)
by Tim
02:20
created

Twitter   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 74
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 27
c 1
b 0
f 0
dl 0
loc 74
rs 10
wmc 9

2 Methods

Rating   Name   Duplication   Size   Complexity  
B linkback() 0 40 8
A __construct() 0 6 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace SimpleSAML\Module\authtwitter\Controller;
6
7
use SimpleSAML\Auth;
8
use SimpleSAML\Configuration;
9
use SimpleSAML\Error;
10
use SimpleSAML\HTTP\RunnableResponse;
11
use SimpleSAML\Module\authtwitter\Auth\Source\Twitter as TwitterSource;
12
use SimpleSAML\Session;
13
use Symfony\Component\HttpFoundation\Request;
14
15
/**
16
 * Controller class for the authtwitter module.
17
 *
18
 * This class serves the different views available in the module.
19
 *
20
 * @package simplesamlphp/simplesamlphp-module-twitter
21
 */
22
class Twitter
23
{
24
    /** @var \SimpleSAML\Configuration */
25
    protected Configuration $config;
26
27
    /** @var \SimpleSAML\Session */
28
    protected Session $session;
29
30
31
    /**
32
     * Controller constructor.
33
     *
34
     * It initializes the global configuration and session for the controllers implemented here.
35
     *
36
     * @param \SimpleSAML\Configuration $config The configuration to use by the controllers.
37
     * @param \SimpleSAML\Session $session The session to use by the controllers.
38
     *
39
     * @throws \Exception
40
     */
41
    public function __construct(
42
        Configuration $config,
43
        Session $session
44
    ) {
45
        $this->config = $config;
46
        $this->session = $session;
47
    }
48
49
50
    /**
51
     * Linkback.
52
     *
53
     * @param \Symfony\Component\HttpFoundation\Request $request The current request.
54
     *
55
     */
56
    public function linkback(Request $request)
57
    {
58
        $authState = $request->get('AuthState');
59
        if ($authState === null) {
60
            throw new Error\BadRequest('Missing state parameter on twitter linkback endpoint.');
61
        }
62
63
        $state = Auth\State::loadState($authState, TwitterSource::STAGE_INIT);
64
65
        // Find authentication source
66
        if (is_null($state) || !array_key_exists(TwitterSource::AUTHID, $state)) {
67
            throw new Error\BadRequest('No data in state for ' . TwitterSource::AUTHID);
68
        }
69
70
        $sourceId = $state[TwitterSource::AUTHID];
71
72
        /** @var \SimpleSAML\Module\authtwitter\Auth\Source\Twitter|null $source */
73
        $source = Auth\Source::getById($sourceId);
74
75
        if ($source === null) {
76
            throw new Error\BadRequest(
77
                'Could not find authentication source with id ' . var_export($sourceId, true)
78
            );
79
        }
80
81
        try {
82
            if ($request->request->has('denied')) {
83
                throw new Error\UserAborted();
84
            }
85
            $source->finalStep($state, $request);
86
        } catch (Error\Exception $e) {
87
            Auth\State::throwException($state, $e);
88
        } catch (Exception $e) {
0 ignored issues
show
Bug introduced by
The type SimpleSAML\Module\authtwitter\Controller\Exception was not found. Did you mean Exception? If so, make sure to prefix the type with \.
Loading history...
89
            Auth\State::throwException(
90
                $state,
91
                new Error\AuthSource($sourceId, 'Error on authtwitter linkback endpoint.', $e)
92
            );
93
        }
94
95
        return RunnableResponse([Auth\Source::class, 'completeAuth'], [$state]);
0 ignored issues
show
Bug introduced by
The function RunnableResponse was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

95
        return /** @scrutinizer ignore-call */ RunnableResponse([Auth\Source::class, 'completeAuth'], [$state]);
Loading history...
96
    }
97
}
98