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

Twitter::linkback()   B

Complexity

Conditions 10
Paths 9

Size

Total Lines 53
Code Lines 29

Duplication

Lines 0
Ratio 0 %

Importance

Changes 3
Bugs 0 Features 0
Metric Value
cc 10
eloc 29
c 3
b 0
f 0
nc 9
nop 1
dl 0
loc 53
rs 7.6666

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
declare(strict_types=1);
4
5
namespace SimpleSAML\Module\authtwitter\Controller;
6
7
use Exception;
8
use SimpleSAML\Auth;
9
use SimpleSAML\Configuration;
10
use SimpleSAML\Error;
11
use SimpleSAML\HTTP\RunnableResponse;
12
use SimpleSAML\Module\authtwitter\Auth\Source\Twitter as TwitterSource;
13
use SimpleSAML\Session;
14
use Symfony\Component\HttpFoundation\Request;
15
16
/**
17
 * Controller class for the authtwitter module.
18
 *
19
 * This class serves the different views available in the module.
20
 *
21
 * @package simplesamlphp/simplesamlphp-module-twitter
22
 */
23
class Twitter
24
{
25
    /** @var \SimpleSAML\Configuration */
26
    protected Configuration $config;
27
28
    /** @var \SimpleSAML\Session */
29
    protected Session $session;
30
31
32
    /**
33
     * Controller constructor.
34
     *
35
     * It initializes the global configuration and session for the controllers implemented here.
36
     *
37
     * @param \SimpleSAML\Configuration $config The configuration to use by the controllers.
38
     * @param \SimpleSAML\Session $session The session to use by the controllers.
39
     *
40
     * @throws \Exception
41
     */
42
    public function __construct(
43
        Configuration $config,
44
        Session $session
45
    ) {
46
        $this->config = $config;
47
        $this->session = $session;
48
    }
49
50
51
    /**
52
     * Linkback.
53
     *
54
     * @param \Symfony\Component\HttpFoundation\Request $request The current request.
55
     *
56
     */
57
    public function linkback(Request $request)
58
    {
59
        $authState = $request->get('AuthState');
60
        if ($authState === null) {
61
            throw new Error\BadRequest('Missing state parameter on twitter linkback endpoint.');
62
        }
63
64
        $state = Auth\State::loadState(base64_decode($authState), TwitterSource::STAGE_TEMP);
65
66
        // Find authentication source
67
        if (is_null($state) || !array_key_exists(TwitterSource::AUTHID, $state)) {
68
            throw new Error\BadRequest('No data in state for ' . TwitterSource::AUTHID);
69
        }
70
71
        $sourceId = $state[TwitterSource::AUTHID];
72
73
        /** @var \SimpleSAML\Module\authtwitter\Auth\Source\Twitter|null $source */
74
        $source = Auth\Source::getById($sourceId);
75
76
        if ($source === null) {
77
            throw new Error\BadRequest(
78
                'Could not find authentication source with id ' . var_export($sourceId, true)
79
            );
80
        }
81
82
        $requestToken = unserialize($state['authtwitter:authdata:requestToken']);
83
84
        $oauth_token = $request->get('oauth_token');
85
        if ($oauth_token === null) {
86
            throw new Error\BadRequest("Missing oauth_token parameter.");
87
        }
88
89
        if ($requestToken->getIdentifier() !== $oauth_token) {
90
            throw new Error\BadRequest("Invalid oauth_token parameter.");
91
        }
92
93
        $oauth_verifier = $request->get('oauth_verifier');
94
        if ($oauth_verifier === null) {
95
            throw new Error\BadRequest("Missing oauth_verifier parameter.");
96
        }
97
98
        try {
99
            $source->finalStep($requestToken, $oauth_token, $oauth_verifier);
0 ignored issues
show
Unused Code introduced by
The call to SimpleSAML\Module\authtw...ce\Twitter::finalStep() has too many arguments starting with $oauth_verifier. ( Ignorable by Annotation )

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

99
            $source->/** @scrutinizer ignore-call */ 
100
                     finalStep($requestToken, $oauth_token, $oauth_verifier);

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.

Loading history...
100
        } catch (Error\Exception $e) {
101
            Auth\State::throwException($state, $e);
102
        } catch (Exception $e) {
103
            Auth\State::throwException(
104
                $state,
105
                new Error\AuthSource($sourceId, 'Error on authtwitter linkback endpoint.', $e)
106
            );
107
        }
108
109
        return new RunnableResponse([Auth\Source::class, 'completeAuth'], [&$state]);
110
    }
111
}
112