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\ember\helpers\ModelHelper; |
13
|
|
|
use flipbox\ember\records\ActiveRecordWithId; |
14
|
|
|
use flipbox\patron\db\ProviderInstanceActiveQuery; |
15
|
|
|
use flipbox\patron\helpers\ProviderHelper; |
16
|
|
|
use flipbox\patron\Patron; |
17
|
|
|
use flipbox\patron\providers\SettingsInterface; |
18
|
|
|
use flipbox\patron\validators\ProviderSettings as ProviderSettingsValidator; |
19
|
|
|
use yii\db\ActiveQueryInterface; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* @author Flipbox Factory <[email protected]> |
23
|
|
|
* @since 1.0.0 |
24
|
|
|
* |
25
|
|
|
* @property string $clientId |
26
|
|
|
* @property string $clientSecret |
27
|
|
|
* @property array $settings |
28
|
|
|
* @property int $providerId |
29
|
|
|
* @property Provider $provider |
30
|
|
|
* @property ProviderEnvironment[] $environments |
31
|
|
|
*/ |
32
|
|
|
class ProviderInstance extends ActiveRecordWithId |
33
|
|
|
{ |
34
|
|
|
use traits\ProviderAttribute, |
35
|
|
|
traits\RelatedEnvironmentsAttribute; |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* The table alias |
39
|
|
|
*/ |
40
|
|
|
const TABLE_ALIAS = 'patron_provider_instances'; |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* The length of the identifier |
44
|
|
|
*/ |
45
|
|
|
const CLIENT_ID_LENGTH = 100; |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* The length of the secret |
49
|
|
|
*/ |
50
|
|
|
const CLIENT_SECRET_LENGTH = 255; |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* @var SettingsInterface |
54
|
|
|
*/ |
55
|
|
|
private $providerSettings; |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* @inheritdoc |
59
|
|
|
*/ |
60
|
|
|
protected $getterPriorityAttributes = [ |
61
|
|
|
'providerId' |
62
|
|
|
]; |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* @inheritdoc |
66
|
|
|
* @return ProviderInstanceActiveQuery |
67
|
|
|
* @throws \yii\base\InvalidConfigException |
68
|
|
|
*/ |
69
|
|
|
public static function find() |
70
|
|
|
{ |
71
|
|
|
/** @noinspection PhpIncompatibleReturnTypeInspection */ |
72
|
|
|
return Craft::createObject(ProviderInstanceActiveQuery::class, [get_called_class()]); |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* @inheritdoc |
77
|
|
|
*/ |
78
|
|
|
public function rules() |
79
|
|
|
{ |
80
|
|
|
return array_merge( |
81
|
|
|
parent::rules(), |
82
|
|
|
$this->providerRules(), |
83
|
|
|
[ |
84
|
|
|
[ |
85
|
|
|
[ |
86
|
|
|
'clientId' |
87
|
|
|
], |
88
|
|
|
'string', |
89
|
|
|
'max' => static::CLIENT_ID_LENGTH |
90
|
|
|
], |
91
|
|
|
[ |
92
|
|
|
[ |
93
|
|
|
'clientSecret' |
94
|
|
|
], |
95
|
|
|
'string', |
96
|
|
|
'max' => static::CLIENT_SECRET_LENGTH |
97
|
|
|
], |
98
|
|
|
[ |
99
|
|
|
[ |
100
|
|
|
'providerId', |
101
|
|
|
'clientId' |
102
|
|
|
], |
103
|
|
|
'required' |
104
|
|
|
], |
105
|
|
|
[ |
106
|
|
|
[ |
107
|
|
|
'settings' |
108
|
|
|
], |
109
|
|
|
ProviderSettingsValidator::class |
110
|
|
|
], |
111
|
|
|
[ |
112
|
|
|
[ |
113
|
|
|
'clientId', |
114
|
|
|
'clientSecret', |
115
|
|
|
'settings' |
116
|
|
|
], |
117
|
|
|
'safe', |
118
|
|
|
'on' => [ |
119
|
|
|
ModelHelper::SCENARIO_DEFAULT |
120
|
|
|
] |
121
|
|
|
] |
122
|
|
|
] |
123
|
|
|
); |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
|
127
|
|
|
/******************************************* |
128
|
|
|
* EVENTS |
129
|
|
|
*******************************************/ |
130
|
|
|
|
131
|
|
|
/** |
132
|
|
|
* @inheritdoc |
133
|
|
|
*/ |
134
|
|
|
public function afterFind() |
135
|
|
|
{ |
136
|
|
|
if ($this->clientSecret) { |
137
|
|
|
$this->clientSecret = ProviderHelper::decryptClientSecret($this->clientSecret); |
138
|
|
|
} |
139
|
|
|
|
140
|
|
|
parent::afterFind(); |
141
|
|
|
} |
142
|
|
|
|
143
|
|
|
/** |
144
|
|
|
* @inheritdoc |
145
|
|
|
*/ |
146
|
|
|
public function beforeSave($insert) |
147
|
|
|
{ |
148
|
|
|
if ($this->clientSecret) { |
149
|
|
|
$this->clientSecret = ProviderHelper::encryptClientSecret($this->clientSecret); |
150
|
|
|
} |
151
|
|
|
|
152
|
|
|
if (!parent::beforeSave($insert)) { |
153
|
|
|
return false; |
154
|
|
|
} |
155
|
|
|
|
156
|
|
|
return $this->beforeSaveEnvironments($insert); |
157
|
|
|
} |
158
|
|
|
|
159
|
|
|
/** |
160
|
|
|
* @inheritdoc |
161
|
|
|
* @throws \Throwable |
162
|
|
|
*/ |
163
|
|
|
public function afterSave($insert, $changedAttributes) |
164
|
|
|
{ |
165
|
|
|
if ($this->clientSecret) { |
166
|
|
|
$this->clientSecret = ProviderHelper::decryptClientSecret($this->clientSecret); |
167
|
|
|
} |
168
|
|
|
|
169
|
|
|
parent::afterSave($insert, $changedAttributes); |
170
|
|
|
} |
171
|
|
|
|
172
|
|
|
|
173
|
|
|
/******************************************* |
174
|
|
|
* UPDATE / INSERT |
175
|
|
|
*******************************************/ |
176
|
|
|
|
177
|
|
|
/** |
178
|
|
|
* @inheritdoc |
179
|
|
|
* @throws \Throwable |
180
|
|
|
*/ |
181
|
|
|
protected function insertInternal($attributes = null) |
182
|
|
|
{ |
183
|
|
|
if (!parent::insertInternal($attributes)) { |
184
|
|
|
return false; |
185
|
|
|
} |
186
|
|
|
|
187
|
|
|
return $this->insertInternalEnvironments($attributes); |
188
|
|
|
} |
189
|
|
|
|
190
|
|
|
/** |
191
|
|
|
* @inheritdoc |
192
|
|
|
* @throws \Throwable |
193
|
|
|
*/ |
194
|
|
|
protected function updateInternal($attributes = null) |
195
|
|
|
{ |
196
|
|
|
if (false === ($response = parent::updateInternal($attributes))) { |
197
|
|
|
return false; |
198
|
|
|
} |
199
|
|
|
|
200
|
|
|
return $this->upsertEnvironmentsInternal($attributes) ? $response : false; |
201
|
|
|
} |
202
|
|
|
|
203
|
|
|
|
204
|
|
|
/******************************************* |
205
|
|
|
* ENVIRONMENTS |
206
|
|
|
*******************************************/ |
207
|
|
|
|
208
|
|
|
/** |
209
|
|
|
* @inheritdoc |
210
|
|
|
*/ |
211
|
|
|
protected static function environmentRecordClass(): string |
212
|
|
|
{ |
213
|
|
|
return ProviderEnvironment::class; |
214
|
|
|
} |
215
|
|
|
|
216
|
|
|
/** |
217
|
|
|
* @inheritdoc |
218
|
|
|
*/ |
219
|
|
|
protected function prepareEnvironmentRecordConfig(array $config = []): array |
220
|
|
|
{ |
221
|
|
|
$config['instance'] = $this; |
222
|
|
|
return $config; |
223
|
|
|
} |
224
|
|
|
|
225
|
|
|
/** |
226
|
|
|
* @inheritdoc |
227
|
|
|
*/ |
228
|
|
|
protected function environmentRelationshipQuery(): ActiveQueryInterface |
229
|
|
|
{ |
230
|
|
|
return $this->hasMany( |
231
|
|
|
static::environmentRecordClass(), |
232
|
|
|
['instanceId' => 'id'] |
233
|
|
|
); |
234
|
|
|
} |
235
|
|
|
|
236
|
|
|
/** |
237
|
|
|
* @return string |
238
|
|
|
* @throws \yii\base\InvalidConfigException |
239
|
|
|
*/ |
240
|
|
|
public function getHtml(): string |
241
|
|
|
{ |
242
|
|
|
return $this->getProviderSettings()->inputHtml(); |
243
|
|
|
} |
244
|
|
|
|
245
|
|
|
/** |
246
|
|
|
* @return SettingsInterface |
247
|
|
|
* @throws \yii\base\InvalidConfigException |
248
|
|
|
*/ |
249
|
|
|
public function getProviderSettings(): SettingsInterface |
250
|
|
|
{ |
251
|
|
|
if (!$this->providerSettings instanceof SettingsInterface) { |
252
|
|
|
$this->providerSettings = Patron::getInstance()->getProviderSettings()->resolveSettings( |
253
|
|
|
$this->getProvider(), |
254
|
|
|
$this->settings |
255
|
|
|
); |
256
|
|
|
} |
257
|
|
|
|
258
|
|
|
return $this->providerSettings; |
259
|
|
|
} |
260
|
|
|
} |
261
|
|
|
|