From 04d4d4a8d71de9520be3bf1555d50131c7d3b775 Mon Sep 17 00:00:00 2001 From: Alejandro Celaya Date: Sun, 3 Feb 2019 12:09:56 +0100 Subject: [PATCH] Updated GenerateShortUrlCommand to accept the findIfExists flag --- CHANGELOG.md | 2 +- .../ShortUrl/GenerateShortUrlCommand.php | 24 +++++++++++-------- .../ShortUrl/GenerateShortUrlCommandTest.php | 2 +- 3 files changed, 16 insertions(+), 12 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 79412b1f..308790a6 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -29,7 +29,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com), and this The status code can be `200 OK` in case of success or `503 Service Unavailable` in case of error, while the `status` property will be one of `pass` or `fail`, as defined in the [Health check RFC](https://inadarei.github.io/rfc-healthcheck/). -* [#279](https://github.com/shlinkio/shlink/issues/279) Added new `findIfExists` flag to the `[POST /short-url]` endpoint which will be used to return existing short URLs if they exist, instead of creating new ones. +* [#279](https://github.com/shlinkio/shlink/issues/279) Added new `findIfExists` flag to the `[POST /short-url]` REST endpoint and the `short-urls:generate` CLI command. It can be used to return existing short URLs when found, instead of creating new ones. Thanks to this flag you won't need to remember if you created a short URL for a long one. It will just create it if needed or return the existing one if possible. diff --git a/module/CLI/src/Command/ShortUrl/GenerateShortUrlCommand.php b/module/CLI/src/Command/ShortUrl/GenerateShortUrlCommand.php index b187b97c..fcb4d305 100644 --- a/module/CLI/src/Command/ShortUrl/GenerateShortUrlCommand.php +++ b/module/CLI/src/Command/ShortUrl/GenerateShortUrlCommand.php @@ -16,8 +16,10 @@ use Symfony\Component\Console\Input\InputOption; use Symfony\Component\Console\Output\OutputInterface; use Symfony\Component\Console\Style\SymfonyStyle; use Zend\Diactoros\Uri; -use function array_merge; -use function explode; +use function array_map; +use function Functional\curry; +use function Functional\flatten; +use function Functional\unique; use function sprintf; class GenerateShortUrlCommand extends Command @@ -77,6 +79,12 @@ class GenerateShortUrlCommand extends Command 'm', InputOption::VALUE_REQUIRED, 'This will limit the number of visits for this short URL.' + ) + ->addOption( + 'findIfExists', + 'f', + InputOption::VALUE_NONE, + 'This will force existing matching URL to be returned if found, instead of creating a new one.' ); } @@ -103,13 +111,8 @@ class GenerateShortUrlCommand extends Command return; } - $tags = $input->getOption('tags'); - $processedTags = []; - foreach ($tags as $key => $tag) { - $explodedTags = explode(',', $tag); - $processedTags = array_merge($processedTags, $explodedTags); - } - $tags = $processedTags; + $explodeWithComma = curry('explode')(','); + $tags = unique(flatten(array_map($explodeWithComma, $input->getOption('tags')))); $customSlug = $input->getOption('customSlug'); $maxVisits = $input->getOption('maxVisits'); @@ -121,7 +124,8 @@ class GenerateShortUrlCommand extends Command $this->getOptionalDate($input, 'validSince'), $this->getOptionalDate($input, 'validUntil'), $customSlug, - $maxVisits !== null ? (int) $maxVisits : null + $maxVisits !== null ? (int) $maxVisits : null, + $input->getOption('findIfExists') ) )->getShortCode(); $shortUrl = $this->buildShortUrl($this->domainConfig, $shortCode); diff --git a/module/CLI/test/Command/ShortUrl/GenerateShortUrlCommandTest.php b/module/CLI/test/Command/ShortUrl/GenerateShortUrlCommandTest.php index 4698ed48..03db0d61 100644 --- a/module/CLI/test/Command/ShortUrl/GenerateShortUrlCommandTest.php +++ b/module/CLI/test/Command/ShortUrl/GenerateShortUrlCommandTest.php @@ -90,7 +90,7 @@ class GenerateShortUrlCommandTest extends TestCase $this->commandTester->execute([ 'command' => 'shortcode:generate', 'longUrl' => 'http://domain.com/foo/bar', - '--tags' => ['foo,bar', 'baz', 'boo,zar'], + '--tags' => ['foo,bar', 'baz', 'boo,zar,baz'], ]); $output = $this->commandTester->getDisplay();