1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* @copyright Copyright (c) Flipbox Digital Limited |
5
|
|
|
* @license https://flipboxfactory.com/software/patron/license |
6
|
|
|
* @link https://www.flipboxfactory.com/software/patron/ |
7
|
|
|
*/ |
8
|
|
|
|
9
|
|
|
namespace flipbox\patron\records; |
10
|
|
|
|
11
|
|
|
use Craft; |
12
|
|
|
use craft\helpers\DateTimeHelper; |
13
|
|
|
use craft\validators\DateTimeValidator; |
14
|
|
|
use DateTime; |
15
|
|
|
use flipbox\craft\ember\helpers\ModelHelper; |
16
|
|
|
use flipbox\craft\ember\records\ActiveRecordWithId; |
17
|
|
|
use flipbox\craft\ember\records\StateAttributeTrait; |
18
|
|
|
use flipbox\patron\queries\TokenActiveQuery; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* @author Flipbox Factory <[email protected]> |
22
|
|
|
* @since 1.0.0 |
23
|
|
|
* |
24
|
|
|
* @property string $accessToken |
25
|
|
|
* @property string $refreshToken |
26
|
|
|
* @property DateTime|null $dateExpires |
27
|
|
|
* @property array $values |
28
|
|
|
*/ |
29
|
|
|
class Token extends ActiveRecordWithId |
30
|
|
|
{ |
31
|
|
|
use StateAttributeTrait, |
32
|
|
|
ProviderAttributeTrait; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* The table alias |
36
|
|
|
*/ |
37
|
|
|
const TABLE_ALIAS = 'patron_tokens'; |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* @inheritdoc |
41
|
|
|
*/ |
42
|
|
|
protected $getterPriorityAttributes = [ |
43
|
|
|
'providerId' |
44
|
|
|
]; |
45
|
|
|
|
46
|
|
|
/******************************************* |
47
|
|
|
* QUERY |
48
|
|
|
*******************************************/ |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* @inheritdoc |
52
|
|
|
* @return TokenActiveQuery |
53
|
|
|
* @throws \yii\base\InvalidConfigException |
54
|
|
|
*/ |
55
|
|
|
public static function find() |
56
|
|
|
{ |
57
|
|
|
/** @noinspection PhpIncompatibleReturnTypeInspection */ |
58
|
|
|
return Craft::createObject(TokenActiveQuery::class, [get_called_class()]); |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* @inheritdoc |
63
|
|
|
*/ |
64
|
|
|
public function rules() |
65
|
|
|
{ |
66
|
|
|
return array_merge( |
67
|
|
|
parent::rules(), |
68
|
|
|
$this->stateRules(), |
69
|
|
|
$this->providerRules(), |
70
|
|
|
[ |
71
|
|
|
[ |
72
|
|
|
[ |
73
|
|
|
'accessToken', |
74
|
|
|
'refreshToken' |
75
|
|
|
], |
76
|
|
|
'unique' |
77
|
|
|
], |
78
|
|
|
[ |
79
|
|
|
[ |
80
|
|
|
'dateExpires' |
81
|
|
|
], |
82
|
|
|
DateTimeValidator::class |
83
|
|
|
], |
84
|
|
|
[ |
85
|
|
|
[ |
86
|
|
|
'providerId', |
87
|
|
|
'accessToken' |
88
|
|
|
], |
89
|
|
|
'required' |
90
|
|
|
], |
91
|
|
|
[ |
92
|
|
|
[ |
93
|
|
|
'accessToken', |
94
|
|
|
'values', |
95
|
|
|
'dateExpires' |
96
|
|
|
], |
97
|
|
|
'safe', |
98
|
|
|
'on' => [ |
99
|
|
|
ModelHelper::SCENARIO_DEFAULT |
100
|
|
|
] |
101
|
|
|
] |
102
|
|
|
] |
103
|
|
|
); |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
/** |
107
|
|
|
* @return bool |
108
|
|
|
*/ |
109
|
|
|
public function isActive(): bool |
110
|
|
|
{ |
111
|
|
|
return $this->isEnabled() && !$this->hasExpired(); |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
/** |
115
|
|
|
* @return bool |
116
|
|
|
*/ |
117
|
|
|
public function hasExpired(): bool |
118
|
|
|
{ |
119
|
|
|
$dateExpires = $this->dateExpires ?: new DateTime('now'); |
120
|
|
|
return DateTimeHelper::isInThePast($dateExpires); |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
|
124
|
|
|
/******************************************* |
125
|
|
|
* PROJECT CONFIG |
126
|
|
|
*******************************************/ |
127
|
|
|
|
128
|
|
|
/** |
129
|
|
|
* Return an array suitable for Craft's Project config |
130
|
|
|
*/ |
131
|
|
|
public function toProjectConfig(): array |
132
|
|
|
{ |
133
|
|
|
return $this->toArray([ |
134
|
|
|
'accessToken', |
135
|
|
|
'refreshToken', |
136
|
|
|
'providerId', |
137
|
|
|
'values', |
138
|
|
|
'enabled' |
139
|
|
|
]); |
140
|
|
|
} |
141
|
|
|
} |
142
|
|
|
|