mirror of
				https://github.com/elyby/accounts.git
				synced 2025-05-31 14:11:46 +05:30 
			
		
		
		
	Rework the webhooks table, allow to update exists webhooks
This commit is contained in:
		@@ -7,15 +7,46 @@ use common\models\WebHook;
 | 
			
		||||
use console\models\WebHookForm;
 | 
			
		||||
use yii\console\Controller;
 | 
			
		||||
use yii\console\ExitCode;
 | 
			
		||||
use yii\helpers\Console;
 | 
			
		||||
use yii\console\widgets\Table;
 | 
			
		||||
use yii\helpers\Console as C;
 | 
			
		||||
 | 
			
		||||
class WebhooksController extends Controller {
 | 
			
		||||
 | 
			
		||||
    public function actionCreate(): int {
 | 
			
		||||
        $form = new WebHookForm(new WebHook());
 | 
			
		||||
    public $defaultAction = 'list';
 | 
			
		||||
 | 
			
		||||
        $url = Console::prompt('Enter webhook url:', [
 | 
			
		||||
    public function actionList(): void {
 | 
			
		||||
        $rows = [];
 | 
			
		||||
        /** @var WebHook $webHook */
 | 
			
		||||
        foreach (WebHook::find()->with('events')->all() as $webHook) {
 | 
			
		||||
            $rows[] = [$webHook->id, $webHook->url, $webHook->secret, implode(', ', $webHook->events)];
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        echo (new Table([
 | 
			
		||||
            'headers' => ['id', 'url', 'secret', 'events'],
 | 
			
		||||
            'rows' => $rows,
 | 
			
		||||
        ]))->run();
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    public function actionCreate(): int {
 | 
			
		||||
        return $this->runForm(new WebHookForm(new WebHook()));
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    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]), [
 | 
			
		||||
            'required' => true,
 | 
			
		||||
            'default' => $form->url,
 | 
			
		||||
            'validator' => function(string $input, ?string &$error) use ($form): bool {
 | 
			
		||||
                $form->url = $input;
 | 
			
		||||
                if (!$form->validate('url')) {
 | 
			
		||||
@@ -26,34 +57,48 @@ class WebhooksController extends Controller {
 | 
			
		||||
                return true;
 | 
			
		||||
            },
 | 
			
		||||
        ]);
 | 
			
		||||
        $secret = Console::prompt('Enter webhook secret (empty to no secret):');
 | 
			
		||||
 | 
			
		||||
        $options = $form::getEvents();
 | 
			
		||||
        $options[''] = 'Finish input'; // It's needed to allow finish input cycle
 | 
			
		||||
        $events = [];
 | 
			
		||||
 | 
			
		||||
        do {
 | 
			
		||||
            $availableOptions = array_diff($options, $events);
 | 
			
		||||
            $eventIndex = Console::select('Choose wanted events (submit no input to finish):', $availableOptions);
 | 
			
		||||
            if ($eventIndex !== '') {
 | 
			
		||||
                $events[] = $options[$eventIndex];
 | 
			
		||||
            }
 | 
			
		||||
        } while ($eventIndex !== '' || empty($events));
 | 
			
		||||
 | 
			
		||||
        $form->url = $url;
 | 
			
		||||
        $form->events = $events;
 | 
			
		||||
        $secret = C::prompt(C::ansiFormat('Enter webhook secret (empty to no secret):', [C::FG_GREY]), [
 | 
			
		||||
            'default' => $form->secret,
 | 
			
		||||
        ]);
 | 
			
		||||
        if ($secret !== '') {
 | 
			
		||||
            $form->secret = $secret;
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        $allEvents = WebHookForm::getEvents();
 | 
			
		||||
        do {
 | 
			
		||||
            $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;
 | 
			
		||||
                }
 | 
			
		||||
            }
 | 
			
		||||
 | 
			
		||||
            $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));
 | 
			
		||||
 | 
			
		||||
        if (!$form->save()) {
 | 
			
		||||
            Console::error('Unable to create new webhook. Check errors list below' . PHP_EOL . Console::errorSummary($form));
 | 
			
		||||
            C::error('Unable to create new webhook. Check errors list below' . PHP_EOL . C::errorSummary($form));
 | 
			
		||||
            return ExitCode::UNSPECIFIED_ERROR;
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        return ExitCode::OK;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    // TODO: add action to modify the webhook events
 | 
			
		||||
 | 
			
		||||
}
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user