mirror of
https://github.com/elyby/accounts.git
synced 2024-12-24 14:19:49 +05:30
5fc97fdd7a
Reworked webhooks notifications constructors
61 lines
1.5 KiB
PHP
61 lines
1.5 KiB
PHP
<?php
|
|
declare(strict_types=1);
|
|
|
|
namespace console\models;
|
|
|
|
use common\models\WebHook;
|
|
use common\notifications;
|
|
use Webmozart\Assert\Assert;
|
|
use yii\base\Model;
|
|
|
|
class WebHookForm extends Model {
|
|
|
|
public $url;
|
|
|
|
public $secret;
|
|
|
|
public $events = [];
|
|
|
|
private $webHook;
|
|
|
|
public function __construct(WebHook $webHook, array $config = []) {
|
|
parent::__construct($config);
|
|
$this->webHook = $webHook;
|
|
$this->url = $webHook->url;
|
|
$this->secret = $webHook->secret;
|
|
$this->events = (array)$webHook->events;
|
|
}
|
|
|
|
public function rules(): array {
|
|
return [
|
|
[['url'], 'required'],
|
|
[['url'], 'url'],
|
|
[['secret'], 'string'],
|
|
[['events'], 'in', 'range' => static::getEvents(), 'allowArray' => true],
|
|
];
|
|
}
|
|
|
|
public function save(): bool {
|
|
if (!$this->validate()) {
|
|
return false;
|
|
}
|
|
|
|
$webHook = $this->webHook;
|
|
$webHook->url = $this->url;
|
|
$webHook->secret = $this->secret;
|
|
$webHook->events = array_values($this->events); // Drop the keys order
|
|
Assert::true($webHook->save(), 'Cannot save webhook.');
|
|
|
|
return true;
|
|
}
|
|
|
|
public static function getEvents(): array {
|
|
return [
|
|
notifications\AccountEditNotification::getType(),
|
|
notifications\AccountDeletedNotification::getType(),
|
|
notifications\OAuthSessionRevokedNotification::getType(),
|
|
];
|
|
}
|
|
|
|
}
|