Completed
Push — master ( 8ea8de...59eb2e )
by Charles
01:52
created

RefreshAction::post()   B

Complexity

Conditions 4
Paths 4

Size

Total Lines 23
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 23
rs 8.7972
c 0
b 0
f 0
cc 4
eloc 11
nc 4
nop 1
1
<?php
2
3
namespace yrc\actions;
4
5
use yrc\api\actions\AuthenticationAction;
0 ignored issues
show
Bug introduced by
This use statement conflicts with another class in this namespace, yrc\actions\AuthenticationAction.

Let’s assume that you have a directory layout like this:

.
|-- OtherDir
|   |-- Bar.php
|   `-- Foo.php
`-- SomeDir
    `-- Foo.php

and let’s assume the following content of Bar.php:

// Bar.php
namespace OtherDir;

use SomeDir\Foo; // This now conflicts the class OtherDir\Foo

If both files OtherDir/Foo.php and SomeDir/Foo.php are loaded in the same runtime, you will see a PHP error such as the following:

PHP Fatal error:  Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.php

However, as OtherDir/Foo.php does not necessarily have to be loaded and the error is only triggered if it is loaded before OtherDir/Bar.php, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias:

// Bar.php
namespace OtherDir;

use SomeDir\Foo as SomeDirFoo; // There is no conflict anymore.
Loading history...
6
use app\models\Token;
7
use yrc\models\TokenKeyPair;
8
use yrc\web\Json25519Parser;
9
use yrc\rest\Action as RestAction;
10
use yii\helpers\ArrayHelper;
11
use Yii;
12
13
/**
14
 * @class RefreshAction
15
 * Handles token refresh
16
 */
17
class RefreshAction extends RestAction
18
{
19
    public $extraAttributes = [];
20
21
    public $identityAttributes = [];
22
23
    /**
24
     * Refreshes the user's token
25
     * @return bool
26
     */
27
    public function post($params)
0 ignored issues
show
Unused Code introduced by
The parameter $params is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
28
    {
29
        // Get the token
30
        $token = AuthenticationAction::getAccessTokenFromHeader();
31
        
32
        $refreshToken = Yii::$app->request->post('refresh_token', false);
33
        if ($refreshToken !== $token->refresh_token) {
34
            return false;
35
        }
36
37
        // If we can delete the token, send a newly generated token out
38
        if ($token->delete()) {
39
            // Merge any extra attributes with the generated tokens
40
            $tokens = ArrayHelper::merge($this->extraAttributes, Token::generate(Yii::$app->user->id)->getAuthResponse());
41
            // Merge the identity attributes
42
            foreach ($this->identityAttributes as $attr) {
43
                $tokens[$attr] = Yii::$app->user->getIdentity()->$attr;
44
            }
45
            return $tokens;
46
        }
47
        // Return false for any other reasons
48
        return false;
49
    }
50
}
51