ShortUrlService.php 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. <?php
  2. declare(strict_types=1);
  3. namespace Shlinkio\Shlink\Core\Service;
  4. use Doctrine\ORM;
  5. use Laminas\Paginator\Paginator;
  6. use Shlinkio\Shlink\Core\Entity\ShortUrl;
  7. use Shlinkio\Shlink\Core\Exception\InvalidUrlException;
  8. use Shlinkio\Shlink\Core\Exception\ShortUrlNotFoundException;
  9. use Shlinkio\Shlink\Core\Model\ShortUrlEdit;
  10. use Shlinkio\Shlink\Core\Model\ShortUrlIdentifier;
  11. use Shlinkio\Shlink\Core\Model\ShortUrlsParams;
  12. use Shlinkio\Shlink\Core\Paginator\Adapter\ShortUrlRepositoryAdapter;
  13. use Shlinkio\Shlink\Core\Repository\ShortUrlRepository;
  14. use Shlinkio\Shlink\Core\Service\ShortUrl\ShortUrlResolverInterface;
  15. use Shlinkio\Shlink\Core\Util\TagManagerTrait;
  16. use Shlinkio\Shlink\Core\Util\UrlValidatorInterface;
  17. use Shlinkio\Shlink\Rest\Entity\ApiKey;
  18. class ShortUrlService implements ShortUrlServiceInterface
  19. {
  20. use TagManagerTrait;
  21. private ORM\EntityManagerInterface $em;
  22. private ShortUrlResolverInterface $urlResolver;
  23. private UrlValidatorInterface $urlValidator;
  24. public function __construct(
  25. ORM\EntityManagerInterface $em,
  26. ShortUrlResolverInterface $urlResolver,
  27. UrlValidatorInterface $urlValidator
  28. ) {
  29. $this->em = $em;
  30. $this->urlResolver = $urlResolver;
  31. $this->urlValidator = $urlValidator;
  32. }
  33. /**
  34. * @return ShortUrl[]|Paginator
  35. */
  36. public function listShortUrls(ShortUrlsParams $params, ?ApiKey $apiKey = null): Paginator
  37. {
  38. /** @var ShortUrlRepository $repo */
  39. $repo = $this->em->getRepository(ShortUrl::class);
  40. $paginator = new Paginator(new ShortUrlRepositoryAdapter($repo, $params, $apiKey));
  41. $paginator->setItemCountPerPage($params->itemsPerPage())
  42. ->setCurrentPageNumber($params->page());
  43. return $paginator;
  44. }
  45. /**
  46. * @param string[] $tags
  47. * @throws ShortUrlNotFoundException
  48. */
  49. public function setTagsByShortCode(ShortUrlIdentifier $identifier, array $tags = []): ShortUrl
  50. {
  51. $shortUrl = $this->urlResolver->resolveShortUrl($identifier);
  52. $shortUrl->setTags($this->tagNamesToEntities($this->em, $tags));
  53. $this->em->flush();
  54. return $shortUrl;
  55. }
  56. /**
  57. * @throws ShortUrlNotFoundException
  58. * @throws InvalidUrlException
  59. */
  60. public function updateMetadataByShortCode(
  61. ShortUrlIdentifier $identifier,
  62. ShortUrlEdit $shortUrlEdit,
  63. ?ApiKey $apiKey = null
  64. ): ShortUrl {
  65. if ($shortUrlEdit->hasLongUrl()) {
  66. $this->urlValidator->validateUrl($shortUrlEdit->longUrl(), $shortUrlEdit->doValidateUrl());
  67. }
  68. $shortUrl = $this->urlResolver->resolveShortUrl($identifier, $apiKey);
  69. $shortUrl->update($shortUrlEdit);
  70. $this->em->flush();
  71. return $shortUrl;
  72. }
  73. }