From 0232f68b9170987d68222a9c5093a0995f5e31b9 Mon Sep 17 00:00:00 2001 From: Alejandro Celaya Date: Sat, 21 Oct 2017 12:24:53 +0200 Subject: [PATCH] Updated action and command to create short urls so that it accepts validity dates --- .../Shortcode/GenerateShortcodeCommand.php | 25 ++++++++++++++++--- module/Core/src/Service/UrlShortener.php | 14 ++++++++--- .../src/Service/UrlShortenerInterface.php | 9 ++++++- .../Rest/src/Action/CreateShortcodeAction.php | 11 ++++++-- .../test/Action/CreateShortcodeActionTest.php | 6 ++--- 5 files changed, 53 insertions(+), 12 deletions(-) diff --git a/module/CLI/src/Command/Shortcode/GenerateShortcodeCommand.php b/module/CLI/src/Command/Shortcode/GenerateShortcodeCommand.php index 9677c466..e84fee6c 100644 --- a/module/CLI/src/Command/Shortcode/GenerateShortcodeCommand.php +++ b/module/CLI/src/Command/Shortcode/GenerateShortcodeCommand.php @@ -51,9 +51,17 @@ class GenerateShortcodeCommand extends Command ->addOption( 'tags', 't', - InputOption::VALUE_IS_ARRAY | InputOption::VALUE_OPTIONAL, + InputOption::VALUE_IS_ARRAY | InputOption::VALUE_REQUIRED, $this->translator->translate('Tags to apply to the new short URL') - ); + ) + ->addOption('validSince', 's', InputOption::VALUE_REQUIRED, $this->translator->translate( + 'The date from which this short URL will be valid. ' + . 'If someone tries to access it before this date, it will not be found.' + )) + ->addOption('validUntil', 'u', InputOption::VALUE_REQUIRED, $this->translator->translate( + 'The date until which this short URL will be valid. ' + . 'If someone tries to access it after this date, it will not be found.' + )); } public function interact(InputInterface $input, OutputInterface $output) @@ -93,7 +101,12 @@ class GenerateShortcodeCommand extends Command return; } - $shortCode = $this->urlShortener->urlToShortCode(new Uri($longUrl), $tags); + $shortCode = $this->urlShortener->urlToShortCode( + new Uri($longUrl), + $tags, + $this->getOptionalDate($input, 'validSince'), + $this->getOptionalDate($input, 'validUntil') + ); $shortUrl = (new Uri())->withPath($shortCode) ->withScheme($this->domainConfig['schema']) ->withHost($this->domainConfig['hostname']); @@ -111,4 +124,10 @@ class GenerateShortcodeCommand extends Command )); } } + + private function getOptionalDate(InputInterface $input, string $fieldName) + { + $since = $input->getOption($fieldName); + return $since !== null ? new \DateTime($since) : null; + } } diff --git a/module/Core/src/Service/UrlShortener.php b/module/Core/src/Service/UrlShortener.php index a14665e4..eb5d585a 100644 --- a/module/Core/src/Service/UrlShortener.php +++ b/module/Core/src/Service/UrlShortener.php @@ -57,12 +57,18 @@ class UrlShortener implements UrlShortenerInterface * * @param UriInterface $url * @param string[] $tags + * @param \DateTime|null $validSince + * @param \DateTime|null $validUntil * @return string * @throws InvalidUrlException * @throws RuntimeException */ - public function urlToShortCode(UriInterface $url, array $tags = []): string - { + public function urlToShortCode( + UriInterface $url, + array $tags = [], + \DateTime $validSince = null, + \DateTime $validUntil = null + ): string { // If the url already exists in the database, just return its short code $shortUrl = $this->em->getRepository(ShortUrl::class)->findOneBy([ 'originalUrl' => $url, @@ -80,7 +86,9 @@ class UrlShortener implements UrlShortenerInterface // First, create the short URL with an empty short code $shortUrl = new ShortUrl(); - $shortUrl->setOriginalUrl((string) $url); + $shortUrl->setOriginalUrl((string) $url) + ->setValidSince($validSince) + ->setValidUntil($validUntil); $this->em->persist($shortUrl); $this->em->flush(); diff --git a/module/Core/src/Service/UrlShortenerInterface.php b/module/Core/src/Service/UrlShortenerInterface.php index 6fc0c24a..27b0574a 100644 --- a/module/Core/src/Service/UrlShortenerInterface.php +++ b/module/Core/src/Service/UrlShortenerInterface.php @@ -16,11 +16,18 @@ interface UrlShortenerInterface * * @param UriInterface $url * @param string[] $tags + * @param \DateTime|null $validSince + * @param \DateTime|null $validUntil * @return string * @throws InvalidUrlException * @throws RuntimeException */ - public function urlToShortCode(UriInterface $url, array $tags = []): string; + public function urlToShortCode( + UriInterface $url, + array $tags = [], + \DateTime $validSince = null, + \DateTime $validUntil = null + ): string; /** * Tries to find the mapped URL for provided short code. Returns null if not found diff --git a/module/Rest/src/Action/CreateShortcodeAction.php b/module/Rest/src/Action/CreateShortcodeAction.php index 9915a5ec..8969ee75 100644 --- a/module/Rest/src/Action/CreateShortcodeAction.php +++ b/module/Rest/src/Action/CreateShortcodeAction.php @@ -57,10 +57,12 @@ class CreateShortcodeAction extends AbstractRestAction ], self::STATUS_BAD_REQUEST); } $longUrl = $postData['longUrl']; - $tags = isset($postData['tags']) && is_array($postData['tags']) ? $postData['tags'] : []; + $tags = (array) ($postData['tags'] ?? []); + $validSince = $this->getOptionalDate($postData, 'validSince'); + $validUntil = $this->getOptionalDate($postData, 'validUntil'); try { - $shortCode = $this->urlShortener->urlToShortCode(new Uri($longUrl), $tags); + $shortCode = $this->urlShortener->urlToShortCode(new Uri($longUrl), $tags, $validSince, $validUntil); $shortUrl = (new Uri())->withPath($shortCode) ->withScheme($this->domainConfig['schema']) ->withHost($this->domainConfig['hostname']); @@ -87,4 +89,9 @@ class CreateShortcodeAction extends AbstractRestAction ], self::STATUS_INTERNAL_SERVER_ERROR); } } + + private function getOptionalDate(array $postData, string $fieldName) + { + return isset($postData[$fieldName]) ? new \DateTime($postData[$fieldName]) : null; + } } diff --git a/module/Rest/test/Action/CreateShortcodeActionTest.php b/module/Rest/test/Action/CreateShortcodeActionTest.php index a5590785..adb1ada4 100644 --- a/module/Rest/test/Action/CreateShortcodeActionTest.php +++ b/module/Rest/test/Action/CreateShortcodeActionTest.php @@ -52,7 +52,7 @@ class CreateShortcodeActionTest extends TestCase */ public function properShortcodeConversionReturnsData() { - $this->urlShortener->urlToShortCode(Argument::type(Uri::class), Argument::type('array')) + $this->urlShortener->urlToShortCode(Argument::type(Uri::class), Argument::type('array'), null, null) ->willReturn('abc123') ->shouldBeCalledTimes(1); @@ -69,7 +69,7 @@ class CreateShortcodeActionTest extends TestCase */ public function anInvalidUrlReturnsError() { - $this->urlShortener->urlToShortCode(Argument::type(Uri::class), Argument::type('array')) + $this->urlShortener->urlToShortCode(Argument::type(Uri::class), Argument::type('array'), null, null) ->willThrow(InvalidUrlException::class) ->shouldBeCalledTimes(1); @@ -86,7 +86,7 @@ class CreateShortcodeActionTest extends TestCase */ public function aGenericExceptionWillReturnError() { - $this->urlShortener->urlToShortCode(Argument::type(Uri::class), Argument::type('array')) + $this->urlShortener->urlToShortCode(Argument::type(Uri::class), Argument::type('array'), null, null) ->willThrow(\Exception::class) ->shouldBeCalledTimes(1);