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\Template; |
13
|
|
|
use flipbox\ember\helpers\ModelHelper; |
14
|
|
|
use flipbox\ember\helpers\ObjectHelper; |
15
|
|
|
use flipbox\ember\helpers\QueryHelper; |
16
|
|
|
use flipbox\ember\records\ActiveRecordWithId; |
17
|
|
|
use flipbox\ember\records\traits\StateAttribute; |
18
|
|
|
use flipbox\ember\traits\HandleRules; |
19
|
|
|
use flipbox\patron\db\ProviderActiveQuery; |
20
|
|
|
use flipbox\patron\helpers\ProviderHelper; |
21
|
|
|
use flipbox\patron\Patron; |
22
|
|
|
use flipbox\patron\providers\SettingsInterface; |
23
|
|
|
use flipbox\patron\validators\ProviderValidator; |
24
|
|
|
use Twig_Markup; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* @author Flipbox Factory <[email protected]> |
28
|
|
|
* @since 1.0.0 |
29
|
|
|
* |
30
|
|
|
* @property string $clientId |
31
|
|
|
* @property string $clientSecret |
32
|
|
|
* @property string $class |
33
|
|
|
* @property array $settings |
34
|
|
|
* @property Token[] tokens |
35
|
|
|
* @property ProviderEnvironment[] $environments |
36
|
|
|
*/ |
37
|
|
|
class Provider extends ActiveRecordWithId |
38
|
|
|
{ |
39
|
|
|
use HandleRules, |
40
|
|
|
StateAttribute; |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* The table alias |
44
|
|
|
*/ |
45
|
|
|
const TABLE_ALIAS = 'patron_providers'; |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* @var SettingsInterface |
49
|
|
|
*/ |
50
|
|
|
private $settingsModel; |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* The length of the identifier |
54
|
|
|
*/ |
55
|
|
|
const CLIENT_ID_LENGTH = 100; |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* The length of the secret |
59
|
|
|
*/ |
60
|
|
|
const CLIENT_SECRET_LENGTH = 100; |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* @return string|null |
64
|
|
|
*/ |
65
|
|
|
public function getIcon() |
66
|
|
|
{ |
67
|
|
|
if ($this->class === null) { |
68
|
|
|
return null; |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
return Patron::getInstance()->getCp()->getProviderIcon( |
72
|
|
|
$this->class |
73
|
|
|
); |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* @inheritdoc |
78
|
|
|
* @return ProviderActiveQuery |
79
|
|
|
* @throws \yii\base\InvalidConfigException |
80
|
|
|
*/ |
81
|
|
|
public static function find() |
82
|
|
|
{ |
83
|
|
|
/** @noinspection PhpIncompatibleReturnTypeInspection */ |
84
|
|
|
return Craft::createObject(ProviderActiveQuery::class, [get_called_class()]); |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* @inheritdoc |
89
|
|
|
*/ |
90
|
|
|
public function rules() |
91
|
|
|
{ |
92
|
|
|
return array_merge( |
93
|
|
|
parent::rules(), |
94
|
|
|
$this->handleRules(), |
95
|
|
|
$this->stateRules(), |
96
|
|
|
[ |
97
|
|
|
[ |
98
|
|
|
[ |
99
|
|
|
'clientId' |
100
|
|
|
], |
101
|
|
|
'string', |
102
|
|
|
'max' => static::CLIENT_ID_LENGTH |
103
|
|
|
], |
104
|
|
|
[ |
105
|
|
|
[ |
106
|
|
|
'clientSecret' |
107
|
|
|
], |
108
|
|
|
'string', |
109
|
|
|
'max' => static::CLIENT_SECRET_LENGTH |
110
|
|
|
], |
111
|
|
|
[ |
112
|
|
|
[ |
113
|
|
|
'class' |
114
|
|
|
], |
115
|
|
|
ProviderValidator::class |
116
|
|
|
], |
117
|
|
|
[ |
118
|
|
|
[ |
119
|
|
|
'clientId', |
120
|
|
|
'class' |
121
|
|
|
], |
122
|
|
|
'required' |
123
|
|
|
], |
124
|
|
|
[ |
125
|
|
|
[ |
126
|
|
|
'clientId', |
127
|
|
|
'clientSecret', |
128
|
|
|
'class', |
129
|
|
|
'settings' |
130
|
|
|
], |
131
|
|
|
'safe', |
132
|
|
|
'on' => [ |
133
|
|
|
ModelHelper::SCENARIO_DEFAULT |
134
|
|
|
] |
135
|
|
|
] |
136
|
|
|
] |
137
|
|
|
); |
138
|
|
|
} |
139
|
|
|
|
140
|
|
|
/** |
141
|
|
|
* Get all of the associated tokens. |
142
|
|
|
* |
143
|
|
|
* @param array $config |
144
|
|
|
* @return \yii\db\ActiveQuery |
145
|
|
|
*/ |
146
|
|
|
public function getTokens(array $config = []) |
147
|
|
|
{ |
148
|
|
|
$query = $this->hasMany( |
149
|
|
|
Token::class, |
150
|
|
|
['providerId' => 'id'] |
151
|
|
|
); |
152
|
|
|
|
153
|
|
|
if (!empty($config)) { |
154
|
|
|
QueryHelper::configure( |
155
|
|
|
$query, |
156
|
|
|
$config |
157
|
|
|
); |
158
|
|
|
} |
159
|
|
|
|
160
|
|
|
return $query; |
161
|
|
|
} |
162
|
|
|
|
163
|
|
|
/** |
164
|
|
|
* Get all of the associated environments. |
165
|
|
|
* |
166
|
|
|
* @param array $config |
167
|
|
|
* @return \yii\db\ActiveQuery |
168
|
|
|
*/ |
169
|
|
|
public function getEnvironments(array $config = []) |
170
|
|
|
{ |
171
|
|
|
$query = $this->hasMany( |
172
|
|
|
ProviderEnvironment::class, |
173
|
|
|
['providerId' => 'id'] |
174
|
|
|
)->indexBy('environment'); |
175
|
|
|
|
176
|
|
|
if (!empty($config)) { |
177
|
|
|
QueryHelper::configure( |
178
|
|
|
$query, |
179
|
|
|
$config |
180
|
|
|
); |
181
|
|
|
} |
182
|
|
|
|
183
|
|
|
return $query; |
184
|
|
|
} |
185
|
|
|
|
186
|
|
|
/** |
187
|
|
|
* @param array $environments |
188
|
|
|
* @return $this |
189
|
|
|
*/ |
190
|
|
|
public function setEnvironments(array $environments = []) |
191
|
|
|
{ |
192
|
|
|
$records = []; |
193
|
|
|
foreach (array_filter($environments) as $key => $environment) { |
194
|
|
|
$records[] = $this->resolveEnvironment($key, $environment); |
195
|
|
|
} |
196
|
|
|
|
197
|
|
|
$this->populateRelation('environments', $records); |
198
|
|
|
return $this; |
199
|
|
|
} |
200
|
|
|
|
201
|
|
|
/** |
202
|
|
|
* @param string $key |
203
|
|
|
* @param $environment |
204
|
|
|
* @return ProviderEnvironment |
205
|
|
|
*/ |
206
|
|
|
protected function resolveEnvironment(string $key, $environment): ProviderEnvironment |
207
|
|
|
{ |
208
|
|
|
if (!$record = $this->environments[$key] ?? null) { |
209
|
|
|
$record = new ProviderEnvironment(); |
210
|
|
|
} |
211
|
|
|
|
212
|
|
|
if (!is_array($environment)) { |
213
|
|
|
$environment = ['environment' => $environment]; |
214
|
|
|
} |
215
|
|
|
|
216
|
|
|
/** @noinspection PhpIncompatibleReturnTypeInspection */ |
217
|
|
|
return ObjectHelper::populate( |
218
|
|
|
$record, |
219
|
|
|
$environment |
220
|
|
|
); |
221
|
|
|
} |
222
|
|
|
|
223
|
|
|
/******************************************* |
224
|
|
|
* EVENTS |
225
|
|
|
*******************************************/ |
226
|
|
|
|
227
|
|
|
|
228
|
|
|
/** |
229
|
|
|
* @inheritdoc |
230
|
|
|
* @throws \Throwable |
231
|
|
|
*/ |
232
|
|
|
public function afterSave($insert, $changedAttributes) |
233
|
|
|
{ |
234
|
|
|
Patron::getInstance()->manageProviders()->saveEnvironments($this); |
235
|
|
|
parent::afterSave($insert, $changedAttributes); |
236
|
|
|
} |
237
|
|
|
|
238
|
|
|
/** |
239
|
|
|
* @return string |
240
|
|
|
*/ |
241
|
|
|
public function getDisplayName(): string |
242
|
|
|
{ |
243
|
|
|
return ProviderHelper::displayName( |
244
|
|
|
$this->class |
245
|
|
|
); |
246
|
|
|
} |
247
|
|
|
|
248
|
|
|
/** |
249
|
|
|
* @return Twig_Markup |
250
|
|
|
* @throws \yii\base\InvalidConfigException |
251
|
|
|
*/ |
252
|
|
|
public function getSettingsHtml(): Twig_Markup |
253
|
|
|
{ |
254
|
|
|
return Template::raw( |
255
|
|
|
$this->getSettingsModel()->inputHtml() |
256
|
|
|
); |
257
|
|
|
} |
258
|
|
|
|
259
|
|
|
/** |
260
|
|
|
* @return SettingsInterface |
261
|
|
|
* @throws \yii\base\InvalidConfigException |
262
|
|
|
*/ |
263
|
|
|
protected function getSettingsModel(): SettingsInterface |
264
|
|
|
{ |
265
|
|
|
if (!$this->settingsModel instanceof SettingsInterface) { |
266
|
|
|
$this->settingsModel = Patron::getInstance()->manageProviders()->resolveSettings($this, $this->settings); |
267
|
|
|
} |
268
|
|
|
|
269
|
|
|
return $this->settingsModel; |
270
|
|
|
} |
271
|
|
|
} |
272
|
|
|
|