Used maxVisits field when creating or fetching a ShortUrl

This commit is contained in:
Alejandro Celaya 2017-10-22 09:15:37 +02:00
parent af7c11665c
commit cb23d38b38
6 changed files with 21 additions and 8 deletions

View File

@ -65,6 +65,9 @@ class GenerateShortcodeCommand extends Command
)) ))
->addOption('customSlug', 'c', InputOption::VALUE_REQUIRED, $this->translator->translate( ->addOption('customSlug', 'c', InputOption::VALUE_REQUIRED, $this->translator->translate(
'If provided, this slug will be used instead of generating a short code' '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; $tags = $processedTags;
$customSlug = $input->getOption('customSlug'); $customSlug = $input->getOption('customSlug');
$maxVisits = $input->getOption('maxVisits');
try { try {
if (! isset($longUrl)) { if (! isset($longUrl)) {
@ -111,7 +115,8 @@ class GenerateShortcodeCommand extends Command
$tags, $tags,
$this->getOptionalDate($input, 'validSince'), $this->getOptionalDate($input, 'validSince'),
$this->getOptionalDate($input, 'validUntil'), $this->getOptionalDate($input, 'validUntil'),
$customSlug $customSlug,
$maxVisits !== null ? (int) $maxVisits : null
); );
$shortUrl = (new Uri())->withPath($shortCode) $shortUrl = (new Uri())->withPath($shortCode)
->withScheme($this->domainConfig['schema']) ->withScheme($this->domainConfig['schema'])

View File

@ -224,7 +224,7 @@ class ShortUrl extends AbstractEntity implements \JsonSerializable
public function maxVisitsReached(): bool public function maxVisitsReached(): bool
{ {
return $this->maxVisits !== null && $this->maxVisits >= $this->getVisitsCount(); return $this->maxVisits !== null && $this->getVisitsCount() >= $this->maxVisits;
} }
/** /**

View File

@ -145,6 +145,8 @@ class ShortUrlRepository extends EntityRepository implements ShortUrlRepositoryI
->setParameter('now', $now) ->setParameter('now', $now)
->setMaxResults(1); ->setMaxResults(1);
return $qb->getQuery()->getOneOrNullResult(); /** @var ShortUrl|null $result */
$result = $qb->getQuery()->getOneOrNullResult();
return $result === null || $result->maxVisitsReached() ? null : $result;
} }
} }

View File

@ -69,6 +69,7 @@ class UrlShortener implements UrlShortenerInterface
* @param \DateTime|null $validSince * @param \DateTime|null $validSince
* @param \DateTime|null $validUntil * @param \DateTime|null $validUntil
* @param string|null $customSlug * @param string|null $customSlug
* @param int|null $maxVisits
* @return string * @return string
* @throws NonUniqueSlugException * @throws NonUniqueSlugException
* @throws InvalidUrlException * @throws InvalidUrlException
@ -79,7 +80,8 @@ class UrlShortener implements UrlShortenerInterface
array $tags = [], array $tags = [],
\DateTime $validSince = null, \DateTime $validSince = null,
\DateTime $validUntil = null, \DateTime $validUntil = null,
string $customSlug = null string $customSlug = null,
int $maxVisits = null
): string { ): string {
// If the url already exists in the database, just return its short code // If the url already exists in the database, just return its short code
$shortUrl = $this->em->getRepository(ShortUrl::class)->findOneBy([ $shortUrl = $this->em->getRepository(ShortUrl::class)->findOneBy([
@ -101,7 +103,8 @@ class UrlShortener implements UrlShortenerInterface
$shortUrl = new ShortUrl(); $shortUrl = new ShortUrl();
$shortUrl->setOriginalUrl((string) $url) $shortUrl->setOriginalUrl((string) $url)
->setValidSince($validSince) ->setValidSince($validSince)
->setValidUntil($validUntil); ->setValidUntil($validUntil)
->setMaxVisits($maxVisits);
$this->em->persist($shortUrl); $this->em->persist($shortUrl);
$this->em->flush(); $this->em->flush();
@ -146,7 +149,7 @@ class UrlShortener implements UrlShortenerInterface
* @param int $id * @param int $id
* @return string * @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 $id = ((int) $id) + 200000; // Increment the Id so that the generated shortcode is not too short
$length = strlen($this->chars); $length = strlen($this->chars);

View File

@ -20,6 +20,7 @@ interface UrlShortenerInterface
* @param \DateTime|null $validSince * @param \DateTime|null $validSince
* @param \DateTime|null $validUntil * @param \DateTime|null $validUntil
* @param string|null $customSlug * @param string|null $customSlug
* @param int|null $maxVisits
* @return string * @return string
* @throws NonUniqueSlugException * @throws NonUniqueSlugException
* @throws InvalidUrlException * @throws InvalidUrlException
@ -30,7 +31,8 @@ interface UrlShortenerInterface
array $tags = [], array $tags = [],
\DateTime $validSince = null, \DateTime $validSince = null,
\DateTime $validUntil = null, \DateTime $validUntil = null,
string $customSlug = null string $customSlug = null,
int $maxVisits = null
): string; ): string;
/** /**

View File

@ -66,7 +66,8 @@ class CreateShortcodeAction extends AbstractRestAction
(array) ($postData['tags'] ?? []), (array) ($postData['tags'] ?? []),
$this->getOptionalDate($postData, 'validSince'), $this->getOptionalDate($postData, 'validSince'),
$this->getOptionalDate($postData, 'validUntil'), $this->getOptionalDate($postData, 'validUntil'),
$customSlug $customSlug,
isset($postData['maxVisits']) ? (int) $postData['maxVisits'] : null
); );
$shortUrl = (new Uri())->withPath($shortCode) $shortUrl = (new Uri())->withPath($shortCode)
->withScheme($this->domainConfig['schema']) ->withScheme($this->domainConfig['schema'])