From cb23d38b38031584685a5e11705de36e8e7d6bab Mon Sep 17 00:00:00 2001 From: Alejandro Celaya Date: Sun, 22 Oct 2017 09:15:37 +0200 Subject: [PATCH] Used maxVisits field when creating or fetching a ShortUrl --- .../src/Command/Shortcode/GenerateShortcodeCommand.php | 7 ++++++- module/Core/src/Entity/ShortUrl.php | 2 +- module/Core/src/Repository/ShortUrlRepository.php | 4 +++- module/Core/src/Service/UrlShortener.php | 9 ++++++--- module/Core/src/Service/UrlShortenerInterface.php | 4 +++- module/Rest/src/Action/CreateShortcodeAction.php | 3 ++- 6 files changed, 21 insertions(+), 8 deletions(-) diff --git a/module/CLI/src/Command/Shortcode/GenerateShortcodeCommand.php b/module/CLI/src/Command/Shortcode/GenerateShortcodeCommand.php index 9df42f20..e685e876 100644 --- a/module/CLI/src/Command/Shortcode/GenerateShortcodeCommand.php +++ b/module/CLI/src/Command/Shortcode/GenerateShortcodeCommand.php @@ -65,6 +65,9 @@ class GenerateShortcodeCommand extends Command )) ->addOption('customSlug', 'c', InputOption::VALUE_REQUIRED, $this->translator->translate( 'If provided, this slug will be used instead of generating a short code' + )) + ->addOption('maxVisits', 'm', InputOption::VALUE_REQUIRED, $this->translator->translate( + 'This will limit the number of visits for this short URL.' )); } @@ -99,6 +102,7 @@ class GenerateShortcodeCommand extends Command } $tags = $processedTags; $customSlug = $input->getOption('customSlug'); + $maxVisits = $input->getOption('maxVisits'); try { if (! isset($longUrl)) { @@ -111,7 +115,8 @@ class GenerateShortcodeCommand extends Command $tags, $this->getOptionalDate($input, 'validSince'), $this->getOptionalDate($input, 'validUntil'), - $customSlug + $customSlug, + $maxVisits !== null ? (int) $maxVisits : null ); $shortUrl = (new Uri())->withPath($shortCode) ->withScheme($this->domainConfig['schema']) diff --git a/module/Core/src/Entity/ShortUrl.php b/module/Core/src/Entity/ShortUrl.php index 6c9b5189..1431f465 100644 --- a/module/Core/src/Entity/ShortUrl.php +++ b/module/Core/src/Entity/ShortUrl.php @@ -224,7 +224,7 @@ class ShortUrl extends AbstractEntity implements \JsonSerializable public function maxVisitsReached(): bool { - return $this->maxVisits !== null && $this->maxVisits >= $this->getVisitsCount(); + return $this->maxVisits !== null && $this->getVisitsCount() >= $this->maxVisits; } /** diff --git a/module/Core/src/Repository/ShortUrlRepository.php b/module/Core/src/Repository/ShortUrlRepository.php index 84d07511..7223b188 100644 --- a/module/Core/src/Repository/ShortUrlRepository.php +++ b/module/Core/src/Repository/ShortUrlRepository.php @@ -145,6 +145,8 @@ class ShortUrlRepository extends EntityRepository implements ShortUrlRepositoryI ->setParameter('now', $now) ->setMaxResults(1); - return $qb->getQuery()->getOneOrNullResult(); + /** @var ShortUrl|null $result */ + $result = $qb->getQuery()->getOneOrNullResult(); + return $result === null || $result->maxVisitsReached() ? null : $result; } } diff --git a/module/Core/src/Service/UrlShortener.php b/module/Core/src/Service/UrlShortener.php index e6cdf947..e294fe70 100644 --- a/module/Core/src/Service/UrlShortener.php +++ b/module/Core/src/Service/UrlShortener.php @@ -69,6 +69,7 @@ class UrlShortener implements UrlShortenerInterface * @param \DateTime|null $validSince * @param \DateTime|null $validUntil * @param string|null $customSlug + * @param int|null $maxVisits * @return string * @throws NonUniqueSlugException * @throws InvalidUrlException @@ -79,7 +80,8 @@ class UrlShortener implements UrlShortenerInterface array $tags = [], \DateTime $validSince = null, \DateTime $validUntil = null, - string $customSlug = null + string $customSlug = null, + int $maxVisits = null ): string { // If the url already exists in the database, just return its short code $shortUrl = $this->em->getRepository(ShortUrl::class)->findOneBy([ @@ -101,7 +103,8 @@ class UrlShortener implements UrlShortenerInterface $shortUrl = new ShortUrl(); $shortUrl->setOriginalUrl((string) $url) ->setValidSince($validSince) - ->setValidUntil($validUntil); + ->setValidUntil($validUntil) + ->setMaxVisits($maxVisits); $this->em->persist($shortUrl); $this->em->flush(); @@ -146,7 +149,7 @@ class UrlShortener implements UrlShortenerInterface * @param int $id * @return string */ - private function convertAutoincrementIdToShortCode($id) + private function convertAutoincrementIdToShortCode($id): string { $id = ((int) $id) + 200000; // Increment the Id so that the generated shortcode is not too short $length = strlen($this->chars); diff --git a/module/Core/src/Service/UrlShortenerInterface.php b/module/Core/src/Service/UrlShortenerInterface.php index aa346756..17859a81 100644 --- a/module/Core/src/Service/UrlShortenerInterface.php +++ b/module/Core/src/Service/UrlShortenerInterface.php @@ -20,6 +20,7 @@ interface UrlShortenerInterface * @param \DateTime|null $validSince * @param \DateTime|null $validUntil * @param string|null $customSlug + * @param int|null $maxVisits * @return string * @throws NonUniqueSlugException * @throws InvalidUrlException @@ -30,7 +31,8 @@ interface UrlShortenerInterface array $tags = [], \DateTime $validSince = null, \DateTime $validUntil = null, - string $customSlug = null + string $customSlug = null, + int $maxVisits = null ): string; /** diff --git a/module/Rest/src/Action/CreateShortcodeAction.php b/module/Rest/src/Action/CreateShortcodeAction.php index 790c361b..8edcd499 100644 --- a/module/Rest/src/Action/CreateShortcodeAction.php +++ b/module/Rest/src/Action/CreateShortcodeAction.php @@ -66,7 +66,8 @@ class CreateShortcodeAction extends AbstractRestAction (array) ($postData['tags'] ?? []), $this->getOptionalDate($postData, 'validSince'), $this->getOptionalDate($postData, 'validUntil'), - $customSlug + $customSlug, + isset($postData['maxVisits']) ? (int) $postData['maxVisits'] : null ); $shortUrl = (new Uri())->withPath($shortCode) ->withScheme($this->domainConfig['schema'])