1 | <?php |
||
42 | class Provider extends ActiveRecordWithId |
||
43 | { |
||
44 | use HandleRulesTrait, |
||
45 | StateAttributeTrait; |
||
46 | |||
47 | /** |
||
48 | * The table alias |
||
49 | */ |
||
50 | const TABLE_ALIAS = 'patron_providers'; |
||
51 | |||
52 | const CLIENT_ID_LENGTH = 100; |
||
53 | const CLIENT_SECRET_LENGTH = 255; |
||
54 | |||
55 | protected $getterPriorityAttributes = ['settings']; |
||
56 | |||
57 | /** |
||
58 | * @inheritdoc |
||
59 | * @return ProviderActiveQuery |
||
60 | * @throws \yii\base\InvalidConfigException |
||
61 | */ |
||
62 | public static function find() |
||
63 | { |
||
64 | /** @noinspection PhpIncompatibleReturnTypeInspection */ |
||
65 | return Craft::createObject(ProviderActiveQuery::class, [get_called_class()]); |
||
66 | } |
||
67 | |||
68 | /** |
||
69 | * @inheritdoc |
||
70 | */ |
||
71 | protected static function findByCondition($condition) |
||
72 | { |
||
73 | if (!is_numeric($condition) && is_string($condition)) { |
||
74 | $condition = ['handle' => $condition]; |
||
75 | } |
||
76 | |||
77 | /** @noinspection PhpInternalEntityUsedInspection */ |
||
78 | return parent::findByCondition($condition); |
||
79 | } |
||
80 | |||
81 | /** |
||
82 | * An array of additional information about the provider. As an example: |
||
83 | * |
||
84 | * ``` |
||
85 | * [ |
||
86 | * 'name' => 'Provider Name', |
||
87 | * 'icon' => '/path/to/icon.svg' |
||
88 | * ] |
||
89 | * ``` |
||
90 | * @return array |
||
91 | * @throws \ReflectionException |
||
92 | */ |
||
93 | public function getInfo(): array |
||
94 | { |
||
95 | if ($this->class === null) { |
||
96 | return []; |
||
97 | } |
||
98 | |||
99 | $info = Patron::getInstance()->getCp()->getProviderInfo(); |
||
100 | |||
101 | return $info[$this->class] ?? [ |
||
102 | 'name' => ProviderHelper::displayName($this->class) |
||
103 | ]; |
||
104 | } |
||
105 | |||
106 | /** |
||
107 | * @return SettingsInterface |
||
108 | * @throws \yii\base\InvalidConfigException |
||
109 | */ |
||
110 | public function getSettings(): SettingsInterface |
||
111 | { |
||
112 | $settings = $this->getAttribute('settings'); |
||
113 | if (!$settings instanceof SettingsInterface) { |
||
114 | $settings = Patron::getInstance()->providerSettings( |
||
115 | $this->class, |
||
116 | $this->getAttribute('settings') |
||
117 | ); |
||
118 | |||
119 | $this->setAttribute('settings', $settings); |
||
120 | } |
||
121 | |||
122 | return $settings; |
||
123 | } |
||
124 | |||
125 | |||
126 | /** |
||
127 | * @param $record |
||
128 | * @param $row |
||
129 | */ |
||
130 | public static function populateRecord($record, $row) |
||
131 | { |
||
132 | // Apply override settings |
||
133 | if (null !== ($handle = $row['handle'] ?? null)) { |
||
134 | $row = array_merge( |
||
135 | $row, |
||
136 | Patron::getInstance()->getSettings()->getProvider($handle) |
||
137 | ); |
||
138 | } |
||
139 | |||
140 | parent::populateRecord($record, $row); |
||
141 | } |
||
142 | |||
143 | /** |
||
144 | * @inheritdoc |
||
145 | */ |
||
146 | public function rules() |
||
147 | { |
||
148 | return array_merge( |
||
149 | parent::rules(), |
||
150 | $this->handleRules(), |
||
151 | $this->stateRules(), |
||
152 | [ |
||
153 | [ |
||
154 | [ |
||
155 | 'clientId' |
||
156 | ], |
||
157 | 'string', |
||
158 | 'max' => static::CLIENT_ID_LENGTH |
||
159 | ], |
||
160 | [ |
||
161 | [ |
||
162 | 'clientSecret' |
||
163 | ], |
||
164 | 'string', |
||
165 | 'max' => static::CLIENT_SECRET_LENGTH |
||
166 | ], |
||
167 | [ |
||
168 | [ |
||
169 | 'class' |
||
170 | ], |
||
171 | ProviderValidator::class |
||
172 | ], |
||
173 | [ |
||
174 | [ |
||
175 | 'settings' |
||
176 | ], |
||
177 | ProviderSettingsValidator::class |
||
178 | ], |
||
179 | [ |
||
180 | [ |
||
181 | 'class', |
||
182 | 'clientId' |
||
183 | ], |
||
184 | 'required' |
||
185 | ], |
||
186 | [ |
||
187 | [ |
||
188 | 'class', |
||
189 | 'clientId', |
||
190 | 'clientSecret', |
||
191 | 'settings', |
||
192 | ], |
||
193 | 'safe', |
||
194 | 'on' => [ |
||
195 | self::SCENARIO_DEFAULT |
||
196 | ] |
||
197 | ] |
||
198 | ] |
||
199 | ); |
||
200 | } |
||
201 | |||
202 | |||
203 | /******************************************* |
||
204 | * RELATIONS |
||
205 | *******************************************/ |
||
206 | |||
207 | /** |
||
208 | * Get all of the associated tokens. |
||
209 | * |
||
210 | * @param array $config |
||
211 | * @return \yii\db\ActiveQuery |
||
212 | */ |
||
213 | public function getTokens(array $config = []) |
||
214 | { |
||
215 | $query = $this->hasMany( |
||
216 | Token::class, |
||
217 | ['providerId' => 'id'] |
||
218 | ); |
||
219 | |||
220 | if (!empty($config)) { |
||
221 | QueryHelper::configure( |
||
222 | $query, |
||
223 | $config |
||
224 | ); |
||
225 | } |
||
226 | |||
227 | return $query; |
||
228 | } |
||
229 | |||
230 | /** |
||
231 | * Get all of the associated tokens. |
||
232 | * |
||
233 | * @param array $config |
||
234 | * @return \yii\db\ActiveQuery |
||
235 | */ |
||
236 | public function getLocks(array $config = []) |
||
237 | { |
||
238 | $query = $this->hasMany( |
||
239 | ProviderLock::class, |
||
240 | ['providerId' => 'id'] |
||
241 | ); |
||
242 | |||
243 | if (!empty($config)) { |
||
244 | QueryHelper::configure( |
||
245 | $query, |
||
246 | $config |
||
247 | ); |
||
248 | } |
||
249 | |||
250 | return $query; |
||
251 | } |
||
252 | |||
253 | /******************************************* |
||
254 | * SAVE |
||
255 | *******************************************/ |
||
256 | |||
257 | /** |
||
258 | * @param PluginInterface $plugin |
||
259 | * @param bool $runValidation |
||
260 | * @param null $attributeNames |
||
261 | * @return bool |
||
262 | */ |
||
263 | public function saveAndLock(PluginInterface $plugin, $runValidation = true, $attributeNames = null): bool |
||
271 | |||
272 | |||
273 | /******************************************* |
||
274 | * LOCK |
||
275 | *******************************************/ |
||
276 | |||
277 | /** |
||
278 | * @param PluginInterface $plugin |
||
279 | * @return bool |
||
280 | */ |
||
281 | public function addLock(PluginInterface $plugin): bool |
||
282 | { |
||
283 | if (null === ($pluginId = $this->getPluginId($plugin))) { |
||
284 | return false; |
||
285 | } |
||
286 | |||
287 | $record = ProviderLock::findOne([ |
||
288 | 'providerId' => $this->getId(), |
||
289 | 'pluginId' => $pluginId |
||
290 | ]); |
||
291 | |||
292 | if (!empty($record)) { |
||
293 | return true; |
||
294 | } |
||
295 | |||
296 | $record = new ProviderLock(); |
||
297 | |||
298 | $record->setAttributes([ |
||
299 | 'providerId' => $this->getId(), |
||
300 | 'pluginId' => $pluginId |
||
301 | ]); |
||
302 | |||
303 | return (bool)$record->save(); |
||
304 | } |
||
305 | |||
306 | /** |
||
307 | * @param PluginInterface $plugin |
||
308 | * @return bool |
||
309 | * @throws \Throwable |
||
310 | */ |
||
311 | public function removeLock(PluginInterface $plugin): bool |
||
312 | { |
||
313 | if (null === ($pluginId = $this->getPluginId($plugin))) { |
||
314 | return false; |
||
315 | } |
||
316 | |||
317 | if (null === ($record = ProviderLock::findOne([ |
||
318 | 'providerId' => $this->getId() ?: 0, |
||
319 | 'pluginId' => $pluginId |
||
320 | ]))) { |
||
321 | return true; |
||
322 | } |
||
323 | |||
324 | return (bool)$record->delete(); |
||
325 | } |
||
326 | |||
327 | /** |
||
328 | * @return bool |
||
329 | */ |
||
330 | public function isLocked(): bool |
||
334 | |||
335 | /** |
||
336 | * @param PluginInterface $plugin |
||
337 | * @return int|null |
||
338 | */ |
||
339 | protected function getPluginId(PluginInterface $plugin) |
||
340 | { |
||
341 | $id = (new Query()) |
||
342 | ->select([ |
||
353 | |||
354 | /** |
||
355 | * @param PluginInterface $plugin |
||
356 | * @return int|null |
||
357 | */ |
||
358 | protected function getPluginName(PluginInterface $plugin) |
||
372 | |||
373 | |||
374 | /******************************************* |
||
375 | * DELETE |
||
376 | *******************************************/ |
||
377 | |||
378 | /** |
||
379 | * @param PluginInterface|null $plugin |
||
380 | * @return bool|false|int |
||
381 | * @throws \Throwable |
||
382 | * @throws \yii\db\StaleObjectException |
||
383 | */ |
||
384 | public function delete(PluginInterface $plugin = null) |
||
388 | |||
389 | /** |
||
390 | * @param PluginInterface|null $plugin |
||
391 | * @return bool |
||
392 | * @throws \craft\errors\InvalidPluginException |
||
393 | */ |
||
394 | protected function canDelete(PluginInterface $plugin = null) |
||
438 | |||
439 | |||
440 | /******************************************* |
||
441 | * PROJECT CONFIG |
||
442 | *******************************************/ |
||
443 | |||
444 | /** |
||
445 | * Return an array suitable for Craft's Project config |
||
446 | */ |
||
447 | public function toProjectConfig(): array |
||
459 | } |
||
460 |