GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.
Completed
Push — master ( cf153f...f10585 )
by François
02:09
created

Controller::handlePost()   B

Complexity

Conditions 5
Paths 5

Size

Total Lines 19
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 19
rs 8.8571
c 0
b 0
f 0
cc 5
eloc 14
nc 5
nop 1
1
<?php
2
3
namespace fkooman\RemoteStorage\Http;
4
5
use fkooman\RemoteStorage\ApiModule;
6
use fkooman\RemoteStorage\Http\Exception\HttpException;
7
use fkooman\RemoteStorage\OAuth\BearerAuthentication;
8
use fkooman\RemoteStorage\OAuth\OAuthModule;
9
use fkooman\RemoteStorage\OAuth\TokenStorage;
10
use fkooman\RemoteStorage\RandomInterface;
11
use fkooman\RemoteStorage\RemoteStorage;
12
use fkooman\RemoteStorage\TplInterface;
13
use fkooman\RemoteStorage\UiModule;
14
use fkooman\RemoteStorage\WebfingerModule;
15
16
class Controller
17
{
18
    private $tpl;
19
    private $formAuth;
20
    private $bearerAuth;
21
    private $oauthModule;
22
    private $apiModule;
23
    private $uiModule;
24
    private $webfingerModule;
25
26
    public function __construct(TplInterface $tpl, SessionInterface $session, TokenStorage $tokenStorage, RandomInterface $random, RemoteStorage $remoteStorage, array $userPass)
27
    {
28
        $this->tpl = $tpl;
29
        $serverMode = 'development';
30
        $this->formAuth = new FormAuthentication($session, $tpl, $userPass);
31
        $this->bearerAuth = new BearerAuthentication($tokenStorage);
32
        $this->oauthModule = new OAuthModule($tpl, $random, $tokenStorage);
33
        $this->apiModule = new ApiModule($remoteStorage, $serverMode);
34
        $this->uiModule = new UiModule($remoteStorage, $tpl, $tokenStorage);
35
        $this->webfingerModule = new WebfingerModule($serverMode);
36
    }
37
38
    public function run(Request $request)
39
    {
40
        try {
41
            switch ($request->getRequestMethod()) {
42
                case 'GET':
43
                    return $this->handleGet($request);
44
                case 'POST':
45
                    return $this->handlePost($request);
46
                case 'PUT':
47
                    $tokenInfo = $this->bearerAuth->requireAuth($request);
48
49
                    return $this->apiModule->put($request, $tokenInfo);
50
                case 'DELETE':
51
                    $tokenInfo = $this->bearerAuth->requireAuth($request);
52
53
                    return $this->apiModule->delete($request, $tokenInfo);
54
                case 'OPTIONS':
55
                    return $this->apiModule->options($request);
56
                case 'HEAD':
57
                    $tokenInfo = $this->bearerAuth->optionalAuth($request);
58
59
                    return $this->apiModule->head($request, $tokenInfo);
60
                default:
61
                    throw new HttpException('method not allowed', 405);
62
           }
63
        } catch (HttpException $e) {
64
            if ($request->isBrowser()) {
65
                $response = new Response($e->getCode(), 'text/html');
66
                $response->setBody(
67
                    $this->tpl->render(
68
                        'errorPage',
69
                        [
70
                            'code' => $e->getCode(),
71
                            'message' => $e->getMessage(),
72
                        ]
73
                    )
74
                );
75
            } else {
76
                // not a browser
77
                $response = new Response($e->getCode(), 'application/json');
78
                $response->setBody(json_encode(['error' => $e->getMessage()]));
79
            }
80
81
            foreach ($e->getResponseHeaders() as $key => $value) {
82
                $response->addHeader($key, $value);
83
            }
84
85
            return $response;
86
        }
87
    }
88
89
    private function handleGet(Request $request)
90
    {
91
        switch ($request->getPathInfo()) {
92
            case '/.well-known/webfinger':
0 ignored issues
show
Coding Style introduced by
The case body in a switch statement must start on the line following the statement.

According to the PSR-2, the body of a case statement must start on the line immediately following the case statement.

switch ($expr) {
case "A":
    doSomething(); //right
    break;
case "B":

    doSomethingElse(); //wrong
    break;

}

To learn more about the PSR-2 coding standard, please refer to the PHP-Fig.

Loading history...
93
94
                return $this->webfingerModule->getWebfinger($request);
95
            case '/_oauth/authorize':
96
                $userId = $this->formAuth->requireAuth($request);
97
                if ($userId instanceof Response) {
98
                    return $userId;
99
                }
100
101
                return $this->oauthModule->getAuthorize($request, $userId);
102
            case '/':
103
                $userId = $this->formAuth->optionalAuth($request);
104
105
                return $this->uiModule->getRootPage($request, $userId);
106
            case '/account':
107
                $userId = $this->formAuth->requireAuth($request);
108
                if ($userId instanceof Response) {
109
                    return $userId;
110
                }
111
112
                return $this->uiModule->getAccountPage($request, $userId);
113
            default:
114
                $tokenInfo = $this->bearerAuth->optionalAuth($request);
115
116
                return $this->apiModule->get($request, $tokenInfo);
0 ignored issues
show
Bug introduced by
It seems like $tokenInfo defined by $this->bearerAuth->optionalAuth($request) on line 114 can also be of type object<fkooman\RemoteStorage\OAuth\TokenInfo>; however, fkooman\RemoteStorage\ApiModule::get() does only seem to accept string|false, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
117
        }
118
    }
119
120
    private function handlePost(Request $request)
121
    {
122
        switch ($request->getPathInfo()) {
123
            case '/account':
124
                $userId = $this->formAuth->requireAuth($request);
125
126
                return $this->uiModule->postAccountPage($request, $userId);
127
            case '/_oauth/authorize':
128
                $userId = $this->formAuth->requireAuth($request);
129
130
                return $this->oauthModule->postAuthorize($request, $userId);
131
            case '/_auth/form/verify':
132
                return $this->formAuth->verifyAuth($request);
133
            case '/_auth/form/logout':
134
                return $this->formAuth->logout($request);
135
            default:
136
                throw new HttpException('not found', 404);
137
        }
138
    }
139
}
140