1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Wearesho\Yii\Models; |
6
|
|
|
|
7
|
|
|
use yii\behaviors\AttributeBehavior; |
8
|
|
|
use Wearesho\Yii\Traits\TokenText; |
9
|
|
|
use Horat1us\Yii\CarbonBehavior; |
10
|
|
|
use yii\db; |
11
|
|
|
|
12
|
|
|
use Wearesho\Yii\Interfaces\TokenRecordInterface; |
13
|
|
|
use Wearesho\Yii\Interfaces\TokenQueryInterface; |
14
|
|
|
use Wearesho\Yii\Queries\TokenQuery; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* @property-read int $id |
18
|
|
|
* |
19
|
|
|
* @property string $type |
20
|
|
|
* @property string $token |
21
|
|
|
* @property string $recipient |
22
|
|
|
* @property array $data |
23
|
|
|
* |
24
|
|
|
* @property int $delivery_count |
25
|
|
|
* @property int $verify_count |
26
|
|
|
* |
27
|
|
|
* @property string $created_at |
28
|
|
|
*/ |
29
|
|
|
final class Token extends db\ActiveRecord implements TokenRecordInterface |
30
|
|
|
{ |
31
|
|
|
use TokenText; |
32
|
|
|
|
33
|
|
|
public static function tableName(): string |
34
|
|
|
{ |
35
|
|
|
return 'token'; |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
public static function find(): TokenQueryInterface |
39
|
25 |
|
{ |
40
|
|
|
return new TokenQuery(get_called_class()); |
41
|
25 |
|
} |
42
|
|
|
|
43
|
|
|
public function behaviors(): array |
44
|
|
|
{ |
45
|
|
|
return [ |
46
|
|
|
'timestamp' => [ |
47
|
13 |
|
'class' => CarbonBehavior::class, |
48
|
|
|
'updatedAtAttribute' => null, |
49
|
13 |
|
], |
50
|
|
|
]; |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
public function rules(): array |
54
|
|
|
{ |
55
|
25 |
|
return [ |
56
|
|
|
[['token', 'recipient', 'type',], 'required'], |
57
|
|
|
[['token', 'recipient',], 'safe'], |
58
|
25 |
|
[['verify_count', 'delivery_count'], 'integer', 'min' => 0,], |
59
|
|
|
['verify_count', 'default', 'value' => 0,], |
60
|
|
|
['delivery_count', 'default', 'value' => 0,], |
61
|
25 |
|
['data', 'safe',], |
62
|
15 |
|
['type', 'string', 'min' => 1,], |
63
|
25 |
|
]; |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
public function getType(): string |
67
|
|
|
{ |
68
|
25 |
|
return $this->type; |
69
|
25 |
|
} |
70
|
|
|
|
71
|
|
|
public function getRecipient(): string |
72
|
25 |
|
{ |
73
|
|
|
return $this->recipient; |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
public function getToken(): string |
77
|
|
|
{ |
78
|
|
|
return $this->token; |
79
|
|
|
} |
80
|
15 |
|
|
81
|
|
|
public function getData(): array |
82
|
|
|
{ |
83
|
15 |
|
return $this->data; |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* Count of sent attempts |
88
|
|
|
*/ |
89
|
|
|
public function getDeliveryCount(): int |
90
|
|
|
{ |
91
|
|
|
return $this->delivery_count ?? 0; |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* Count of verify attempts |
96
|
|
|
*/ |
97
|
|
|
public function getVerifyCount(): int |
98
|
|
|
{ |
99
|
|
|
return $this->verify_count ?? 0; |
100
|
6 |
|
} |
101
|
|
|
|
102
|
6 |
|
public function setToken(string $token): TokenRecordInterface |
103
|
|
|
{ |
104
|
|
|
$this->token = $token; |
105
|
|
|
return $this; |
106
|
|
|
} |
107
|
|
|
|
108
|
9 |
|
public function setRecipient(string $recipient): TokenRecordInterface |
109
|
|
|
{ |
110
|
9 |
|
$this->recipient = $recipient; |
111
|
|
|
return $this; |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
public function setData(array $data): TokenRecordInterface |
115
|
|
|
{ |
116
|
4 |
|
$this->data = $data; |
117
|
|
|
return $this; |
118
|
4 |
|
} |
119
|
|
|
|
120
|
|
|
public function increaseVerifyCount(): TokenRecordInterface |
121
|
|
|
{ |
122
|
|
|
$this->verify_count++; |
123
|
|
|
return $this; |
124
|
|
|
} |
125
|
|
|
|
126
|
4 |
|
public function increaseDeliveryCount(): TokenRecordInterface |
127
|
|
|
{ |
128
|
4 |
|
$this->delivery_count++; |
129
|
|
|
return $this; |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
public function __toString(): string |
133
|
|
|
{ |
134
|
|
|
return $this->token; |
135
|
|
|
} |
136
|
|
|
} |
137
|
|
|
|