RefreshToken::attributes()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 5
nc 1
nop 0
dl 0
loc 7
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace yrc\models\redis;
4
5
use Base32\Base32;
6
use Yii;
7
8
/**
9
 * Represents a temporary single use consumable token
10
 * @property integer $id
11
 * @property string $token
12
 * @property integer $expires_at
13
 */
14
abstract class RefreshToken extends \yrc\redis\ActiveRecord
15
{
16
    /**
17
     * This is our default token lifespan
18
     * @const TOKEN_EXPIRATION_TIME
19
     */
20
    const TOKEN_EXPIRATION_TIME = '+30 days';
21
22
    /**
23
     * @inheritdoc
24
     */
25
    public function attributes()
26
    {
27
        return [
28
            'id',
29
            'user_id',
30
            'token',
31
            'expires_at'
32
        ];
33
    }
34
35
    /**
36
     * Creates a new refresh token that operated independently of the access token
37
     * @param User $user
0 ignored issues
show
Bug introduced by
The type yrc\models\redis\User was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
38
     * @return string
39
     */
40
    public static function create($user)
41
    {
42
        $model = new static;
43
        $model->setAttributes([
44
            'user_id' => $user->id,
45
            'token' => \str_replace('=', '', Base32::encode(\random_bytes(64))),
46
            'expires_at' => \strtotime(static::TOKEN_EXPIRATION_TIME)
47
        ], false);
48
49
        if ($model->save()) {
50
            return $model->token;
51
        }
52
53
        throw new \yii\base\Exception(Yii::t('yrc', 'Refresh token failed to generate.'));
54
    }
55
}
56