Completed
Pull Request — master (#411)
by
unknown
02:03
created

Token::find()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
crap 1
1
<?php
2
3
/*
4
 * This file is part of the 2amigos/yii2-usuario project.
5
 *
6
 * (c) 2amigOS! <http://2amigos.us/>
7
 *
8
 * For the full copyright and license information, please view
9
 * the LICENSE file that was distributed with this source code.
10
 */
11
12
namespace Da\User\Model;
13
14
use Da\User\Helper\SecurityHelper;
15
use Da\User\Query\TokenQuery;
16
use Da\User\Traits\ContainerAwareTrait;
17
use Da\User\Traits\ModuleAwareTrait;
18
use RuntimeException;
19
use yii\base\InvalidConfigException;
20
use yii\base\InvalidParamException;
21
use yii\db\ActiveRecord;
22
use yii\helpers\Url;
23
24
/**
25
 * Token Active Record model.
26
 *
27
 * @property int $user_id
28
 * @property string $code
29
 * @property int $type
30
 * @property string $url
31
 * @property bool $isExpired
32
 * @property int $created_at
33
 * @property User $user
34
 */
35
class Token extends ActiveRecord
36
{
37
    use ModuleAwareTrait;
38
    use ContainerAwareTrait;
39
40
    const TYPE_CONFIRMATION = 0;
41
    const TYPE_RECOVERY = 1;
42
    const TYPE_CONFIRM_NEW_EMAIL = 2;
43
    const TYPE_CONFIRM_OLD_EMAIL = 3;
44
45
    protected $routes = [
46
        self::TYPE_CONFIRMATION => '/user/registration/confirm',
47
        self::TYPE_RECOVERY => '/user/recovery/reset',
48
        self::TYPE_CONFIRM_NEW_EMAIL => '/user/settings/confirm',
49
        self::TYPE_CONFIRM_OLD_EMAIL => '/user/settings/confirm',
50
    ];
51
52
    /**
53
     * {@inheritdoc}
54
     *
55
     * @throws InvalidParamException
56
     * @throws InvalidConfigException
57
     */
58 5
    public function beforeSave($insert)
59
    {
60 5
        if ($insert) {
61 5
            $this->setAttribute('code', $this->make(SecurityHelper::class)->generateRandomString());
62 5
            static::deleteAll(['user_id' => $this->user_id, 'type' => $this->type]);
63 5
            $this->setAttribute('created_at', time());
64
        }
65
66 5
        return parent::beforeSave($insert);
67
    }
68
69
    /**
70
     * {@inheritdoc}
71
     */
72 6
    public static function tableName()
73
    {
74 6
        return '{{%token}}';
75
    }
76
77
    /**
78
     * {@inheritdoc}
79
     */
80 2
    public static function primaryKey()
81
    {
82 2
        return ['user_id', 'code', 'type'];
83
    }
84
85
    /**
86
     * @return \yii\db\ActiveQuery
87
     */
88 1
    public function getUser()
89
    {
90 1
        return $this->hasOne($this->getClassMap()->get(User::class), ['id' => 'user_id']);
91
    }
92
93
    /**
94
     * @throws InvalidParamException
95
     * @return string
96
     */
97 5
    public function getUrl()
98
    {
99 5
        return Url::to([$this->routes[$this->type], 'id' => $this->user_id, 'code' => $this->code], true);
100
    }
101
102
    /**
103
     * @throws RuntimeException
104
     * @return bool             Whether token has expired
105
     */
106 2
    public function getIsExpired()
107
    {
108 2
        if ($this->type === static::TYPE_RECOVERY) {
109
            $expirationTime = $this->getModule()->tokenRecoveryLifespan;
110 2
        } elseif ($this->type >= static::TYPE_CONFIRMATION && $this->type <= static::TYPE_CONFIRM_OLD_EMAIL) {
111 2
            $expirationTime = $this->getModule()->tokenConfirmationLifespan;
112
        } else {
113
            throw new RuntimeException('Unknown Token type.');
114
        }
115
116 2
        return ($this->created_at + $expirationTime) < time();
117
    }
118
119
    /**
120
     * @return TokenQuery
121
     */
122 3
    public static function find()
123
    {
124 3
        return new TokenQuery(static::class);
125
    }
126
}
127