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 ( 924952...661904 )
by François
02:04
created

Controller::handleGet()   C

Complexity

Conditions 7
Paths 7

Size

Total Lines 30
Code Lines 20

Duplication

Lines 14
Ratio 46.67 %

Importance

Changes 0
Metric Value
dl 14
loc 30
rs 6.7272
c 0
b 0
f 0
cc 7
eloc 20
nc 7
nop 1
1
<?php
2
3
namespace fkooman\RemoteStorage;
4
5
use DateTime;
6
use fkooman\RemoteStorage\Http\Exception\HttpException;
7
use fkooman\RemoteStorage\Http\FormAuthentication;
8
use fkooman\RemoteStorage\Http\Request;
9
use fkooman\RemoteStorage\Http\Response;
10
use fkooman\RemoteStorage\Http\SessionInterface;
11
use fkooman\RemoteStorage\OAuth\BearerAuthentication;
12
use fkooman\RemoteStorage\OAuth\OAuthModule;
13
use fkooman\RemoteStorage\OAuth\TokenStorage;
14
use PDO;
15
16
class Controller
17
{
18
    /** @var TwigTpl */
19
    private $templateManager;
20
21
    /** @var ApiModule */
22
    private $apiModule;
23
24
    /** @var UiModule */
25
    private $uiModule;
26
27
    /** @var WebfingerModule */
28
    private $webfingerModule;
29
30
    /** @var OAuthModule */
31
    private $oauthModule;
32
33
    /** @var array */
34
    private $auth = [];
35
36
    /**
37
     * @param string $configFile
0 ignored issues
show
Bug introduced by
There is no parameter named $configFile. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
38
     * @param string $storageRoot
0 ignored issues
show
Bug introduced by
There is no parameter named $storageRoot. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
39
     * @param string $dbDsn
0 ignored issues
show
Bug introduced by
There is no parameter named $dbDsn. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
40
     * @param array  $templateFolders
0 ignored issues
show
Bug introduced by
There is no parameter named $templateFolders. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
41
     */
42
    public function __construct($appDir, SessionInterface $session, RandomInterface $random, DateTime $dateTime)
43
    {
44
        $config = Config::fromFile(sprintf('%s/config/server.yaml', $appDir));
45
        $serverMode = $config->serverMode;
0 ignored issues
show
Bug introduced by
The property serverMode does not seem to exist in fkooman\RemoteStorage\Config.

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
46
47
        $this->templateManager = new TwigTpl(
48
            [
49
                sprintf('%s/views', $appDir),
50
                sprintf('%s/config/views', $appDir),
51
            ],
52
            'development' !== $serverMode ? sprintf('%s/data/tpl', $appDir) : null
53
        );
54
        $this->templateManager->setDefault(
55
            [
56
                'serverMode' => $serverMode,
57
            ]
58
        );
59
60
        $db = new PDO(sprintf('sqlite:%s/data/rs.sqlite', $appDir));
61
        $metaDataStorage = new MetadataStorage($db);
62
        $metaDataStorage->init();
63
64
        $tokenStorage = new TokenStorage($db);
65
        $tokenStorage->init();
66
67
        $remoteStorage = new RemoteStorage(
68
            $metaDataStorage,
69
            new DocumentStorage(sprintf('%s/data/storage', $appDir))
70
        );
71
72
        $this->apiModule = new ApiModule($remoteStorage, $config->serverMode);
0 ignored issues
show
Documentation introduced by
The property serverMode does not exist on object<fkooman\RemoteStorage\Config>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
73
        $this->uiModule = new UiModule($remoteStorage, $this->templateManager, $tokenStorage);
74
        $this->webfingerModule = new WebfingerModule($config->serverMode);
0 ignored issues
show
Documentation introduced by
The property serverMode does not exist on object<fkooman\RemoteStorage\Config>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
75
        $this->oauthModule = new OAuthModule($this->templateManager, $tokenStorage, $random, $dateTime);
76
77
        $session->setSecureOnly('development' !== $serverMode);
78
        $this->auth['form'] = new FormAuthentication($session, $this->templateManager, $config->Users->asArray());
0 ignored issues
show
Documentation introduced by
The property Users does not exist on object<fkooman\RemoteStorage\Config>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
79
        $this->auth['bearer'] = new BearerAuthentication($tokenStorage);
80
    }
81
82
    public function run(Request $request)
83
    {
84
        try {
85
            switch ($request->getRequestMethod()) {
86
                case 'GET':
87
                    return $this->handleGet($request);
88
                case 'POST':
89
                    return $this->handlePost($request);
90 View Code Duplication
                case 'PUT':
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
91
                    $tokenInfo = $this->auth['bearer']->requireAuth($request);
92
93
                    return $this->apiModule->put($request, $tokenInfo);
94 View Code Duplication
                case 'DELETE':
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
95
                    $tokenInfo = $this->auth['bearer']->requireAuth($request);
96
97
                    return $this->apiModule->delete($request, $tokenInfo);
98
                case 'OPTIONS':
99
                    return $this->apiModule->options($request);
100 View Code Duplication
                case 'HEAD':
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
101
                    $tokenInfo = $this->auth['bearer']->optionalAuth($request);
102
103
                    return $this->apiModule->head($request, $tokenInfo);
104
                default:
105
                    throw new HttpException('method not allowed', 405);
106
           }
107
        } catch (HttpException $e) {
108
            if ($request->isBrowser()) {
109
                $response = new Response($e->getCode(), 'text/html');
110
                $response->setBody(
111
                    $this->templateManager->render(
112
                        'errorPage',
113
                        [
114
                            'code' => $e->getCode(),
115
                            'message' => $e->getMessage(),
116
                        ]
117
                    )
118
                );
119
            } else {
120
                // not a browser
121
                $response = new Response($e->getCode(), 'application/json');
122
                $response->setBody(json_encode(['error' => $e->getMessage()]));
123
            }
124
125
            foreach ($e->getResponseHeaders() as $key => $value) {
126
                $response->addHeader($key, $value);
127
            }
128
129
            return $response;
130
        }
131
    }
132
133
    private function handleGet(Request $request)
134
    {
135
        switch ($request->getPathInfo()) {
136
            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...
137
138
                return $this->webfingerModule->getWebfinger($request);
139 View Code Duplication
            case '/authorize':
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
140
                $userId = $this->auth['form']->requireAuth($request);
141
                if ($userId instanceof Response) {
142
                    return $userId;
143
                }
144
145
                return $this->oauthModule->getAuthorize($request, $userId);
146
            case '/':
147
                $userId = $this->auth['form']->optionalAuth($request);
148
149
                return $this->uiModule->getRootPage($request, $userId);
150 View Code Duplication
            case '/account':
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
151
                $userId = $this->auth['form']->requireAuth($request);
152
                if ($userId instanceof Response) {
153
                    return $userId;
154
                }
155
156
                return $this->uiModule->getAccountPage($request, $userId);
157
            default:
158
                $tokenInfo = $this->auth['bearer']->optionalAuth($request);
159
160
                return $this->apiModule->get($request, $tokenInfo);
161
        }
162
    }
163
164
    private function handlePost(Request $request)
165
    {
166
        switch ($request->getPathInfo()) {
167
            case '/account':
168
                $userId = $this->auth['form']->requireAuth($request);
169
170
                return $this->uiModule->postAccountPage($request, $userId);
171
            case '/authorize':
172
                $userId = $this->auth['form']->requireAuth($request);
173
174
                return $this->oauthModule->postAuthorize($request, $userId);
175
            case '/authenticate':
176
                return $this->auth['form']->verifyAuth($request);
177
            case '/logout':
178
                return $this->auth['form']->logout($request);
179
            default:
180
                throw new HttpException('not found', 404);
181
        }
182
    }
183
}
184