Updated GenerateShortUrlCommand to accept the findIfExists flag

This commit is contained in:
Alejandro Celaya 2019-02-03 12:09:56 +01:00
parent a918113ba0
commit 04d4d4a8d7
3 changed files with 16 additions and 12 deletions

View file

@ -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.

View file

@ -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);

View file

@ -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();