diff --git a/module/CLI/config/cli.config.php b/module/CLI/config/cli.config.php index 2ee33a1d..e60bb2e1 100644 --- a/module/CLI/config/cli.config.php +++ b/module/CLI/config/cli.config.php @@ -9,6 +9,7 @@ return [ 'cli' => [ 'commands' => [ Command\ShortUrl\CreateShortUrlCommand::NAME => Command\ShortUrl\CreateShortUrlCommand::class, + Command\ShortUrl\EditShortUrlCommand::NAME => Command\ShortUrl\EditShortUrlCommand::class, Command\ShortUrl\ResolveUrlCommand::NAME => Command\ShortUrl\ResolveUrlCommand::class, Command\ShortUrl\ListShortUrlsCommand::NAME => Command\ShortUrl\ListShortUrlsCommand::class, Command\ShortUrl\GetShortUrlVisitsCommand::NAME => Command\ShortUrl\GetShortUrlVisitsCommand::class, diff --git a/module/CLI/config/dependencies.config.php b/module/CLI/config/dependencies.config.php index f9b90dac..f9bb9654 100644 --- a/module/CLI/config/dependencies.config.php +++ b/module/CLI/config/dependencies.config.php @@ -41,6 +41,7 @@ return [ ApiKey\RoleResolver::class => ConfigAbstractFactory::class, Command\ShortUrl\CreateShortUrlCommand::class => ConfigAbstractFactory::class, + Command\ShortUrl\EditShortUrlCommand::class => ConfigAbstractFactory::class, Command\ShortUrl\ResolveUrlCommand::class => ConfigAbstractFactory::class, Command\ShortUrl\ListShortUrlsCommand::class => ConfigAbstractFactory::class, Command\ShortUrl\GetShortUrlVisitsCommand::class => ConfigAbstractFactory::class, @@ -92,6 +93,7 @@ return [ ShortUrlStringifier::class, UrlShortenerOptions::class, ], + Command\ShortUrl\EditShortUrlCommand::class => [ShortUrl\ShortUrlService::class], Command\ShortUrl\ResolveUrlCommand::class => [ShortUrl\ShortUrlResolver::class], Command\ShortUrl\ListShortUrlsCommand::class => [ ShortUrl\ShortUrlListService::class, diff --git a/module/CLI/src/Command/ShortUrl/CreateShortUrlCommand.php b/module/CLI/src/Command/ShortUrl/CreateShortUrlCommand.php index fa7a2bea..d47c30b9 100644 --- a/module/CLI/src/Command/ShortUrl/CreateShortUrlCommand.php +++ b/module/CLI/src/Command/ShortUrl/CreateShortUrlCommand.php @@ -137,6 +137,6 @@ class CreateShortUrlCommand extends Command private function getIO(InputInterface $input, OutputInterface $output): SymfonyStyle { - return $this->io ?? ($this->io = new SymfonyStyle($input, $output)); + return $this->io ??= new SymfonyStyle($input, $output); } } diff --git a/module/CLI/src/Command/ShortUrl/EditShortUrlCommand.php b/module/CLI/src/Command/ShortUrl/EditShortUrlCommand.php new file mode 100644 index 00000000..b3fd0bd4 --- /dev/null +++ b/module/CLI/src/Command/ShortUrl/EditShortUrlCommand.php @@ -0,0 +1,60 @@ +shortUrlDataInput = new ShortUrlDataInput($this, longUrlAsOption: true); + $this->shortUrlIdentifierInput = new ShortUrlIdentifierInput( + $this, + shortCodeDesc: 'The short code to edit', + domainDesc: 'The domain to which the short URL is attached.', + ); + } + + protected function configure(): void + { + $this + ->setName(self::NAME) + ->setDescription('Edit an existing short URL'); + } + + protected function execute(InputInterface $input, OutputInterface $output): int + { + $io = new SymfonyStyle($input, $output); + + try { + $shortUrl = $this->shortUrlService->updateShortUrl( + $this->shortUrlIdentifierInput->toShortUrlIdentifier($input), + $this->shortUrlDataInput->toShortUrlEdition($input), + ); + + // TODO Print success + return ExitCode::EXIT_SUCCESS; + } catch (ShortUrlNotFoundException) { + // TODO Print error + return ExitCode::EXIT_FAILURE; + } + } +} diff --git a/module/CLI/src/Input/ShortUrlDataInput.php b/module/CLI/src/Input/ShortUrlDataInput.php index 5ba3126f..46c5b1ce 100644 --- a/module/CLI/src/Input/ShortUrlDataInput.php +++ b/module/CLI/src/Input/ShortUrlDataInput.php @@ -4,6 +4,8 @@ declare(strict_types=1); namespace Shlinkio\Shlink\CLI\Input; +use Shlinkio\Shlink\Core\ShortUrl\Model\ShortUrlEdition; +use Shlinkio\Shlink\Core\ShortUrl\Model\Validation\ShortUrlInputFilter; use Symfony\Component\Console\Command\Command; use Symfony\Component\Console\Input\InputArgument; use Symfony\Component\Console\Input\InputInterface; @@ -103,4 +105,21 @@ readonly final class ShortUrlDataInput { return $input->getOption('no-forward-query'); } + + public function toShortUrlEdition(InputInterface $input): ShortUrlEdition + { + return ShortUrlEdition::fromRawData([ + ShortUrlInputFilter::LONG_URL => $this->longUrl($input), + ShortUrlInputFilter::VALID_SINCE => $this->validSince($input), + ShortUrlInputFilter::VALID_UNTIL => $this->validUntil($input), + ShortUrlInputFilter::MAX_VISITS => $this->maxVisits($input), + ShortUrlInputFilter::TAGS => $this->tags($input), + ShortUrlInputFilter::CRAWLABLE => $this->crawlable($input), + ShortUrlInputFilter::FORWARD_QUERY => !$this->noForwardQuery($input), +// ShortUrlInputFilter::TITLE => TODO, + ]); + } + + // TODO + // public function toShortUrlCreation(InputInterface $input) }