Rework the webhooks table, allow to update exists webhooks

This commit is contained in:
ErickSkrauch
2020-06-14 01:20:31 +03:00
parent 17f1794a4e
commit fb452901b8
10 changed files with 131 additions and 104 deletions

View File

@@ -4,9 +4,7 @@ declare(strict_types=1);
namespace console\models;
use common\models\WebHook;
use common\models\WebHookEvent;
use Webmozart\Assert\Assert;
use Yii;
use yii\base\Model;
class WebHookForm extends Model {
@@ -22,6 +20,9 @@ class WebHookForm extends Model {
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 {
@@ -38,22 +39,12 @@ class WebHookForm extends Model {
return false;
}
$transaction = Yii::$app->db->beginTransaction();
$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.');
foreach ($this->events as $event) {
$eventModel = new WebHookEvent();
$eventModel->webhook_id = $webHook->id;
$eventModel->event_type = $event;
Assert::true($eventModel->save(), 'Cannot save webhook event.');
}
$transaction->commit();
return true;
}