Tokenizer   A
last analyzed

Complexity

Total Complexity 3

Size/Duplication

Total Lines 29
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 1

Importance

Changes 0
Metric Value
wmc 3
lcom 0
cbo 1
dl 0
loc 29
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A generate() 0 4 1
A validate() 0 12 2
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