feat: cli command to un-/register MailJet webhook url

This commit is contained in:
Björn Fromme
2026-04-29 10:36:04 +02:00
parent daf89f9189
commit a2bcedafc4
5 changed files with 349 additions and 1 deletions
@@ -0,0 +1,146 @@
<?php
declare(strict_types=1);
namespace App\Command;
use App\Exception\NewsletterProviderException;
use App\Service\MailjetApiClient;
use Psr\Log\LoggerInterface;
use Symfony\Component\Console\Attribute\AsCommand;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Style\SymfonyStyle;
use Symfony\Component\DependencyInjection\Attribute\Autowire;
#[AsCommand(
name: 'app:mailjet:newsletter-webhook',
description: 'Creates or deletes the Mailjet newsletter webhook',
)]
final class MailjetNewsletterWebhookCommand extends Command
{
private const string ACTION_REGISTER = 'register';
private const string ACTION_REMOVE = 'remove';
private const string ACTION_DEACTIVATE = 'deactivate';
private const string EVENT_TYPE_UNSUBSCRIBE = 'unsub';
private const string WEBHOOK_PATH = '/webhooks/mailjet/newsletter';
private const int WEBHOOK_VERSION = 2;
public function __construct(
private readonly MailjetApiClient $mailjetApiClient,
#[Autowire('%env(APP_BASE_URL)%')]
private readonly string $defaultBaseUrl,
private readonly LoggerInterface $logger,
) {
parent::__construct();
}
protected function configure(): void
{
$this
->addArgument('action', InputArgument::REQUIRED, 'One of: register, remove, deactivate')
->addArgument('callback_id', InputArgument::OPTIONAL, 'Callback ID required for remove/deactivate')
->addOption('url', null, InputOption::VALUE_REQUIRED, 'Override the webhook URL to register');
}
protected function execute(InputInterface $input, OutputInterface $output): int
{
$io = new SymfonyStyle($input, $output);
$action = strtolower(trim((string) $input->getArgument('action')));
try {
return match ($action) {
self::ACTION_REGISTER => $this->register($io, $input->getOption('url')),
self::ACTION_REMOVE, self::ACTION_DEACTIVATE => $this->remove($io, $input->getArgument('callback_id')),
default => $this->invalidAction($io, $action),
};
} catch (NewsletterProviderException|\InvalidArgumentException $exception) {
$this->logger->error('Mailjet newsletter webhook command failed', [
'action' => $action,
'error' => $exception->getMessage(),
]);
$io->error($exception->getMessage());
return Command::FAILURE;
}
}
private function register(SymfonyStyle $io, mixed $overrideUrl): int
{
$webhookUrl = $this->resolveWebhookUrl($overrideUrl);
$payload = $this->mailjetApiClient->createEventCallbackUrl(self::EVENT_TYPE_UNSUBSCRIBE, $webhookUrl, self::WEBHOOK_VERSION);
$io->success(sprintf('Created Mailjet newsletter webhook at %s.', $webhookUrl));
$io->writeln($this->formatPayload($payload));
return Command::SUCCESS;
}
private function remove(SymfonyStyle $io, mixed $callbackId): int
{
$resolvedCallbackId = $this->resolveCallbackId($callbackId);
$this->mailjetApiClient->deleteEventCallbackUrl($resolvedCallbackId);
$io->success(sprintf('Deleted Mailjet newsletter webhook callback ID %d.', $resolvedCallbackId));
return Command::SUCCESS;
}
private function invalidAction(SymfonyStyle $io, string $action): int
{
$io->error(sprintf('Unknown action "%s". Use register, remove, or deactivate.', $action));
return Command::FAILURE;
}
private function resolveWebhookUrl(mixed $override): string
{
if (true === is_string($override) && '' !== trim($override)) {
$url = trim($override);
if (false === filter_var($url, FILTER_VALIDATE_URL)) {
throw new \InvalidArgumentException('The provided webhook URL is not valid.');
}
return rtrim($url, '/');
}
if ('' === trim($this->defaultBaseUrl)) {
throw new \InvalidArgumentException('APP_BASE_URL is not configured.');
}
return rtrim(trim($this->defaultBaseUrl), '/').self::WEBHOOK_PATH;
}
private function resolveCallbackId(mixed $callbackId): int
{
if (true === is_int($callbackId) && $callbackId > 0) {
return $callbackId;
}
if (true === is_string($callbackId) && true === ctype_digit(trim($callbackId))) {
$resolvedCallbackId = (int) trim($callbackId);
if ($resolvedCallbackId > 0) {
return $resolvedCallbackId;
}
}
throw new \InvalidArgumentException('A numeric callback ID is required for remove/deactivate.');
}
/**
* @param array<string, mixed> $payload
*/
private function formatPayload(array $payload): string
{
$encoded = json_encode($payload, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE);
if (false !== $encoded) {
return $encoded;
}
return var_export($payload, true);
}
}