mirror of
https://github.com/shlinkio/shlink.git
synced 2026-03-06 15:23:12 +08:00
Created action to set the togas for a short url
This commit is contained in:
@@ -5,7 +5,7 @@ use Shlinkio\Shlink\Common\Exception\RuntimeException;
|
||||
|
||||
class InvalidShortCodeException extends RuntimeException
|
||||
{
|
||||
public static function fromShortCode($shortCode, $charSet, \Exception $previous = null)
|
||||
public static function fromCharset($shortCode, $charSet, \Exception $previous = null)
|
||||
{
|
||||
$code = isset($previous) ? $previous->getCode() : -1;
|
||||
return new static(
|
||||
@@ -14,4 +14,9 @@ class InvalidShortCodeException extends RuntimeException
|
||||
$previous
|
||||
);
|
||||
}
|
||||
|
||||
public static function fromNotFoundShortCode($shortCode)
|
||||
{
|
||||
return new static(sprintf('Provided short code "%s" does not belong to a short URL', $shortCode));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -5,11 +5,15 @@ use Acelaya\ZsmAnnotatedServices\Annotation\Inject;
|
||||
use Doctrine\ORM\EntityManagerInterface;
|
||||
use Shlinkio\Shlink\Common\Paginator\Adapter\PaginableRepositoryAdapter;
|
||||
use Shlinkio\Shlink\Core\Entity\ShortUrl;
|
||||
use Shlinkio\Shlink\Core\Exception\InvalidShortCodeException;
|
||||
use Shlinkio\Shlink\Core\Repository\ShortUrlRepository;
|
||||
use Shlinkio\Shlink\Core\Util\TagManagerTrait;
|
||||
use Zend\Paginator\Paginator;
|
||||
|
||||
class ShortUrlService implements ShortUrlServiceInterface
|
||||
{
|
||||
use TagManagerTrait;
|
||||
|
||||
/**
|
||||
* @var EntityManagerInterface
|
||||
*/
|
||||
@@ -40,4 +44,26 @@ class ShortUrlService implements ShortUrlServiceInterface
|
||||
|
||||
return $paginator;
|
||||
}
|
||||
|
||||
/**
|
||||
* @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,6 +2,7 @@
|
||||
namespace Shlinkio\Shlink\Core\Service;
|
||||
|
||||
use Shlinkio\Shlink\Core\Entity\ShortUrl;
|
||||
use Shlinkio\Shlink\Core\Exception\InvalidShortCodeException;
|
||||
use Zend\Paginator\Paginator;
|
||||
|
||||
interface ShortUrlServiceInterface
|
||||
@@ -11,4 +12,12 @@ interface ShortUrlServiceInterface
|
||||
* @return ShortUrl[]|Paginator
|
||||
*/
|
||||
public function listShortUrls($page = 1);
|
||||
|
||||
/**
|
||||
* @param string $shortCode
|
||||
* @param string[] $tags
|
||||
* @return ShortUrl
|
||||
* @throws InvalidShortCodeException
|
||||
*/
|
||||
public function setTagsByShortCode($shortCode, array $tags = []);
|
||||
}
|
||||
|
||||
@@ -161,7 +161,7 @@ class UrlShortener implements UrlShortenerInterface
|
||||
|
||||
// Validate short code format
|
||||
if (! preg_match('|[' . $this->chars . "]+|", $shortCode)) {
|
||||
throw InvalidShortCodeException::fromShortCode($shortCode, $this->chars);
|
||||
throw InvalidShortCodeException::fromCharset($shortCode, $this->chars);
|
||||
}
|
||||
|
||||
/** @var ShortUrl $shortUrl */
|
||||
|
||||
@@ -16,6 +16,7 @@ trait TagManagerTrait
|
||||
{
|
||||
$entities = [];
|
||||
foreach ($tags as $tagName) {
|
||||
$tagName = $this->normalizeTagName($tagName);
|
||||
$tag = $em->getRepository(Tag::class)->findOneBy(['name' => $tagName]) ?: (new Tag())->setName($tagName);
|
||||
$em->persist($tag);
|
||||
$entities[] = $tag;
|
||||
@@ -23,4 +24,15 @@ trait TagManagerTrait
|
||||
|
||||
return new Collections\ArrayCollection($entities);
|
||||
}
|
||||
|
||||
/**
|
||||
* Tag names are trimmed, lowercased and spaces are replaced by dashes
|
||||
*
|
||||
* @param string $tagName
|
||||
* @return string
|
||||
*/
|
||||
protected function normalizeTagName($tagName)
|
||||
{
|
||||
return str_replace(' ', '-', strtolower(trim($tagName)));
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user