|
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 flipbox\craft\ember\helpers\ObjectHelper; |
|
13
|
|
|
use flipbox\craft\ember\records\ActiveRecordTrait; |
|
14
|
|
|
use yii\db\ActiveQueryInterface; |
|
15
|
|
|
|
|
16
|
|
|
/** |
|
17
|
|
|
* @property int|null $tokenId |
|
18
|
|
|
* @property Token|null $token |
|
19
|
|
|
* @property ActiveQueryInterface $tokenRecord |
|
20
|
|
|
* |
|
21
|
|
|
* @author Flipbox Factory <[email protected]> |
|
22
|
|
|
* @since 1.0.0 |
|
23
|
|
|
*/ |
|
24
|
|
|
trait TokenMutatorTrait |
|
25
|
|
|
{ |
|
26
|
|
|
use ActiveRecordTrait; |
|
27
|
|
|
|
|
28
|
|
|
/** |
|
29
|
|
|
* @var Token|null |
|
30
|
|
|
*/ |
|
31
|
|
|
private $token; |
|
32
|
|
|
|
|
33
|
|
|
/** |
|
34
|
|
|
* Set associated tokenId |
|
35
|
|
|
* |
|
36
|
|
|
* @param $id |
|
37
|
|
|
* @return $this |
|
38
|
|
|
*/ |
|
39
|
|
|
public function setTokenId(int $id) |
|
40
|
|
|
{ |
|
41
|
|
|
$this->tokenId = $id; |
|
42
|
|
|
return $this; |
|
43
|
|
|
} |
|
44
|
|
|
|
|
45
|
|
|
/** |
|
46
|
|
|
* Get associated tokenId |
|
47
|
|
|
* |
|
48
|
|
|
* @return int|null |
|
49
|
|
|
*/ |
|
50
|
|
|
public function getTokenId() |
|
51
|
|
|
{ |
|
52
|
|
|
$id = $this->getAttribute('tokenId'); |
|
53
|
|
|
if (null === $id && null !== $this->token) { |
|
54
|
|
|
$id = $this->token->id; |
|
55
|
|
|
$this->setAttribute('tokenId', $id); |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
return $id; |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
/** |
|
62
|
|
|
* Associate a token |
|
63
|
|
|
* |
|
64
|
|
|
* @param mixed $token |
|
65
|
|
|
* @return $this |
|
66
|
|
|
*/ |
|
67
|
|
|
public function setToken($token = null) |
|
68
|
|
|
{ |
|
69
|
|
|
$this->token = null; |
|
70
|
|
|
|
|
71
|
|
|
if (null === ($token = $this->internalResolveToken($token))) { |
|
72
|
|
|
$this->token = $this->tokenId = null; |
|
73
|
|
|
} else { |
|
74
|
|
|
$this->tokenId = $token->id; |
|
75
|
|
|
$this->token = $token; |
|
|
|
|
|
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
|
|
return $this; |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
|
|
/** |
|
82
|
|
|
* @return Token|null |
|
83
|
|
|
*/ |
|
84
|
|
|
public function getToken() |
|
85
|
|
|
{ |
|
86
|
|
|
if ($this->token === null) { |
|
87
|
|
|
$token = $this->resolveToken(); |
|
88
|
|
|
$this->setToken($token); |
|
89
|
|
|
return $token; |
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
|
|
$tokenId = $this->tokenId; |
|
93
|
|
|
if ($tokenId !== null && |
|
94
|
|
|
$tokenId !== $this->token->id |
|
95
|
|
|
) { |
|
96
|
|
|
$this->token = null; |
|
97
|
|
|
return $this->getToken(); |
|
98
|
|
|
} |
|
99
|
|
|
|
|
100
|
|
|
return $this->token; |
|
101
|
|
|
} |
|
102
|
|
|
|
|
103
|
|
|
/** |
|
104
|
|
|
* @return Token|null |
|
105
|
|
|
*/ |
|
106
|
|
|
protected function resolveToken() |
|
107
|
|
|
{ |
|
108
|
|
|
if (null !== ($model = $this->resolveTokenFromRelation())) { |
|
109
|
|
|
return $model; |
|
110
|
|
|
} |
|
111
|
|
|
|
|
112
|
|
|
if (null !== ($model = $this->resolveTokenFromId())) { |
|
113
|
|
|
return $model; |
|
114
|
|
|
} |
|
115
|
|
|
|
|
116
|
|
|
return null; |
|
117
|
|
|
} |
|
118
|
|
|
|
|
119
|
|
|
/** |
|
120
|
|
|
* @return Token|null |
|
121
|
|
|
*/ |
|
122
|
|
|
private function resolveTokenFromRelation() |
|
123
|
|
|
{ |
|
124
|
|
|
if (false === $this->isRelationPopulated('tokenRecord')) { |
|
125
|
|
|
return null; |
|
126
|
|
|
} |
|
127
|
|
|
|
|
128
|
|
|
/** @var Token $record */ |
|
129
|
|
|
if (null === ($record = $this->getRelation('tokenRecord'))) { |
|
130
|
|
|
return null; |
|
131
|
|
|
} |
|
132
|
|
|
|
|
133
|
|
|
return $record; |
|
134
|
|
|
} |
|
135
|
|
|
|
|
136
|
|
|
/** |
|
137
|
|
|
* @return Token|null |
|
138
|
|
|
*/ |
|
139
|
|
|
private function resolveTokenFromId() |
|
140
|
|
|
{ |
|
141
|
|
|
if (null === $this->tokenId) { |
|
142
|
|
|
return null; |
|
143
|
|
|
} |
|
144
|
|
|
|
|
145
|
|
|
return Token::findOne([ |
|
146
|
|
|
'id' => $this->tokenId |
|
147
|
|
|
]); |
|
148
|
|
|
} |
|
149
|
|
|
|
|
150
|
|
|
/** |
|
151
|
|
|
* @param $token |
|
152
|
|
|
* @return Token|null |
|
153
|
|
|
*/ |
|
154
|
|
|
protected function internalResolveToken($token = null) |
|
155
|
|
|
{ |
|
156
|
|
|
if ($token instanceof Token) { |
|
157
|
|
|
return $token; |
|
158
|
|
|
} |
|
159
|
|
|
|
|
160
|
|
|
if (is_numeric($token) || is_string($token)) { |
|
161
|
|
|
return Token::findOne([ |
|
162
|
|
|
is_numeric($token) ? 'id' : 'accessToken' => $token |
|
163
|
|
|
]); |
|
164
|
|
|
} |
|
165
|
|
|
|
|
166
|
|
|
try { |
|
167
|
|
|
$object = Craft::createObject(Token::class, [$token]); |
|
168
|
|
|
} catch (\Exception $e) { |
|
169
|
|
|
$object = new Token(); |
|
170
|
|
|
ObjectHelper::populate( |
|
171
|
|
|
$object, |
|
172
|
|
|
$token |
|
173
|
|
|
); |
|
174
|
|
|
} |
|
175
|
|
|
|
|
176
|
|
|
/** @var Token $object */ |
|
177
|
|
|
return $object; |
|
178
|
|
|
} |
|
179
|
|
|
|
|
180
|
|
|
/** |
|
181
|
|
|
* Returns the associated token record. |
|
182
|
|
|
* |
|
183
|
|
|
* @return ActiveQueryInterface |
|
184
|
|
|
*/ |
|
185
|
|
|
protected function getTokenRecord(): ActiveQueryInterface |
|
186
|
|
|
{ |
|
187
|
|
|
return $this->hasOne( |
|
188
|
|
|
Token::class, |
|
189
|
|
|
['id' => 'tokenId'] |
|
190
|
|
|
); |
|
191
|
|
|
} |
|
192
|
|
|
} |
|
193
|
|
|
|
Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.
Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..