2018-07-07 17:31:18 +05:30
|
|
|
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
|
|
|
|
namespace console\models;
|
|
|
|
|
|
|
|
use common\models\WebHook;
|
2020-10-01 04:10:28 +05:30
|
|
|
use common\notifications;
|
2019-12-13 23:18:13 +05:30
|
|
|
use Webmozart\Assert\Assert;
|
2018-07-07 17:31:18 +05:30
|
|
|
use yii\base\Model;
|
|
|
|
|
|
|
|
class WebHookForm extends Model {
|
|
|
|
|
2024-12-02 15:40:55 +05:30
|
|
|
public string $url;
|
2018-07-07 17:31:18 +05:30
|
|
|
|
2024-12-02 15:40:55 +05:30
|
|
|
public string $secret;
|
2018-07-07 17:31:18 +05:30
|
|
|
|
2024-12-02 15:40:55 +05:30
|
|
|
/**
|
|
|
|
* @var string[]
|
|
|
|
*/
|
|
|
|
public array $events = [];
|
2018-07-07 17:31:18 +05:30
|
|
|
|
2024-12-02 15:40:55 +05:30
|
|
|
public function __construct(
|
|
|
|
private WebHook $webHook,
|
|
|
|
array $config = [],
|
|
|
|
) {
|
2018-07-07 17:31:18 +05:30
|
|
|
parent::__construct($config);
|
2024-12-02 15:40:55 +05:30
|
|
|
$this->url = $this->webHook->url;
|
|
|
|
$this->secret = $this->webHook->secret;
|
|
|
|
$this->events = $this->webHook->events;
|
2018-07-07 17:31:18 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
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;
|
2020-06-14 03:50:31 +05:30
|
|
|
$webHook->events = array_values($this->events); // Drop the keys order
|
2019-12-13 23:18:13 +05:30
|
|
|
Assert::true($webHook->save(), 'Cannot save webhook.');
|
2018-07-07 17:31:18 +05:30
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
public static function getEvents(): array {
|
|
|
|
return [
|
2020-10-01 04:10:28 +05:30
|
|
|
notifications\AccountEditNotification::getType(),
|
|
|
|
notifications\AccountDeletedNotification::getType(),
|
|
|
|
notifications\OAuthSessionRevokedNotification::getType(),
|
2018-07-07 17:31:18 +05:30
|
|
|
];
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|