Tokenizer::validate()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 12
rs 9.8666
c 0
b 0
f 0
cc 2
nc 2
nop 1
1
<?php
2
3
namespace app\modules\auth\services;
4
5
use Yii;
6
7
class Tokenizer
8
{
9
    /**
10
     * Generate a new token
11
     * @return string
12
     */
13
    public function generate(): string
14
    {
15
        return Yii::$app->security->generateRandomString() . '_' . time();
16
    }
17
18
    /**
19
     * Validate token
20
     * @param string $token
21
     * @return bool
22
     */
23
    public function validate(string $token = null): bool
24
    {
25
        if (empty($token)) {
26
            return false;
27
        }
28
29
        $expire = Yii::$app->params['user.tokenExpire'];
30
        $parts = explode('_', $token);
31
        $timestamp = (int) end($parts);
32
33
        return $timestamp + $expire >= time();
34
    }
35
}
36