2018-07-07 17:31:18 +05:30
|
|
|
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
|
|
|
|
namespace console\controllers;
|
|
|
|
|
|
|
|
use common\models\WebHook;
|
|
|
|
use console\models\WebHookForm;
|
|
|
|
use yii\console\Controller;
|
|
|
|
use yii\console\ExitCode;
|
2020-06-14 03:50:31 +05:30
|
|
|
use yii\console\widgets\Table;
|
|
|
|
use yii\helpers\Console as C;
|
2018-07-07 17:31:18 +05:30
|
|
|
|
|
|
|
class WebhooksController extends Controller {
|
|
|
|
|
2020-06-14 03:50:31 +05:30
|
|
|
public $defaultAction = 'list';
|
|
|
|
|
|
|
|
public function actionList(): void {
|
|
|
|
$rows = [];
|
|
|
|
/** @var WebHook $webHook */
|
2020-06-26 00:01:32 +05:30
|
|
|
foreach (WebHook::find()->all() as $webHook) {
|
2020-06-14 03:50:31 +05:30
|
|
|
$rows[] = [$webHook->id, $webHook->url, $webHook->secret, implode(', ', $webHook->events)];
|
|
|
|
}
|
|
|
|
|
|
|
|
echo (new Table([
|
|
|
|
'headers' => ['id', 'url', 'secret', 'events'],
|
|
|
|
'rows' => $rows,
|
|
|
|
]))->run();
|
|
|
|
}
|
|
|
|
|
2018-07-07 17:31:18 +05:30
|
|
|
public function actionCreate(): int {
|
2020-06-14 03:50:31 +05:30
|
|
|
return $this->runForm(new WebHookForm(new WebHook()));
|
|
|
|
}
|
2018-07-07 17:31:18 +05:30
|
|
|
|
2020-06-14 03:50:31 +05:30
|
|
|
public function actionUpdate(int $id): int {
|
|
|
|
/** @var WebHook|null $webHook */
|
|
|
|
$webHook = WebHook::findOne(['id' => $id]);
|
|
|
|
if ($webHook === null) {
|
|
|
|
C::error("Entity with id {$id} isn't found.");
|
|
|
|
|
|
|
|
return ExitCode::DATAERR;
|
|
|
|
}
|
|
|
|
|
|
|
|
return $this->runForm(new WebHookForm($webHook));
|
|
|
|
}
|
|
|
|
|
|
|
|
private function runForm(WebHookForm $form): int {
|
|
|
|
C::prompt(C::ansiFormat('Enter webhook url:', [C::FG_GREY]), [
|
2018-07-07 17:31:18 +05:30
|
|
|
'required' => true,
|
2020-06-14 03:50:31 +05:30
|
|
|
'default' => $form->url,
|
2018-07-07 17:31:18 +05:30
|
|
|
'validator' => function(string $input, ?string &$error) use ($form): bool {
|
|
|
|
$form->url = $input;
|
|
|
|
if (!$form->validate('url')) {
|
|
|
|
$error = $form->getFirstError('url');
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
},
|
|
|
|
]);
|
|
|
|
|
2020-06-14 03:50:31 +05:30
|
|
|
$secret = C::prompt(C::ansiFormat('Enter webhook secret (empty to no secret):', [C::FG_GREY]), [
|
|
|
|
'default' => $form->secret,
|
|
|
|
]);
|
|
|
|
if ($secret !== '') {
|
|
|
|
$form->secret = $secret;
|
|
|
|
}
|
2018-07-07 17:31:18 +05:30
|
|
|
|
2020-06-14 03:50:31 +05:30
|
|
|
$allEvents = WebHookForm::getEvents();
|
2018-07-07 17:31:18 +05:30
|
|
|
do {
|
2020-06-14 03:50:31 +05:30
|
|
|
$options = [];
|
|
|
|
foreach ($allEvents as $id => $option) {
|
|
|
|
if (in_array($option, $form->events, true)) {
|
|
|
|
$options["-{$id}"] = $option; // Cast to string to create "-0" index
|
|
|
|
} else {
|
|
|
|
$options[$id] = $option;
|
|
|
|
}
|
2018-07-07 17:31:18 +05:30
|
|
|
}
|
|
|
|
|
2020-06-14 03:50:31 +05:30
|
|
|
$options[''] = 'Finish input'; // This needed to allow finish input cycle
|
|
|
|
|
|
|
|
$eventIndex = C::select(
|
|
|
|
C::ansiFormat('Choose wanted events (submit no input to finish):', [C::FG_GREY]),
|
|
|
|
$options,
|
|
|
|
);
|
|
|
|
if ($eventIndex === '') {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($eventIndex[0] === '-') {
|
|
|
|
unset($form->events[array_search($options[$eventIndex], $form->events, true)]);
|
|
|
|
} else {
|
|
|
|
$form->events[] = $options[$eventIndex];
|
|
|
|
}
|
|
|
|
} while ($eventIndex !== '' || empty($form->events));
|
2018-07-07 17:31:18 +05:30
|
|
|
|
|
|
|
if (!$form->save()) {
|
2020-06-14 03:50:31 +05:30
|
|
|
C::error('Unable to create new webhook. Check errors list below' . PHP_EOL . C::errorSummary($form));
|
2018-07-07 17:31:18 +05:30
|
|
|
return ExitCode::UNSPECIFIED_ERROR;
|
|
|
|
}
|
|
|
|
|
|
|
|
return ExitCode::OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|