mirror of
https://github.com/elyby/accounts.git
synced 2024-11-09 23:12:20 +05:30
59 lines
1.3 KiB
PHP
59 lines
1.3 KiB
PHP
<?php
|
|
declare(strict_types=1);
|
|
|
|
namespace console\models;
|
|
|
|
use common\models\WebHook;
|
|
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 [
|
|
'account.edit',
|
|
'account.deletion',
|
|
];
|
|
}
|
|
|
|
}
|