示例#1
0
 /**
  * @param string $shortCode
  * @param string[] $tags
  * @return ShortUrl
  * @throws InvalidShortCodeException
  */
 public function setTagsByShortCode($shortCode, array $tags = [])
 {
     /** @var ShortUrl $shortUrl */
     $shortUrl = $this->em->getRepository(ShortUrl::class)->findOneBy(['shortCode' => $shortCode]);
     if (!isset($shortUrl)) {
         throw InvalidShortCodeException::fromNotFoundShortCode($shortCode);
     }
     $shortUrl->setTags($this->tagNamesToEntities($this->em, $tags));
     $this->em->flush();
     return $shortUrl;
 }
示例#2
0
 /**
  * Tries to find the mapped URL for provided short code. Returns null if not found
  *
  * @param string $shortCode
  * @return string|null
  * @throws InvalidShortCodeException
  */
 public function shortCodeToUrl($shortCode)
 {
     $cacheKey = sprintf('%s_longUrl', $shortCode);
     // Check if the short code => URL map is already cached
     if ($this->cache->contains($cacheKey)) {
         return $this->cache->fetch($cacheKey);
     }
     // Validate short code format
     if (!preg_match('|[' . $this->chars . "]+|", $shortCode)) {
         throw InvalidShortCodeException::fromCharset($shortCode, $this->chars);
     }
     /** @var ShortUrl $shortUrl */
     $shortUrl = $this->em->getRepository(ShortUrl::class)->findOneBy(['shortCode' => $shortCode]);
     // Cache the shortcode
     if (isset($shortUrl)) {
         $url = $shortUrl->getOriginalUrl();
         $this->cache->save($cacheKey, $url);
         return $url;
     }
     return null;
 }