1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677 |
- <?php
- declare(strict_types=1);
- namespace Shlinkio\Shlink\Core\Service;
- use Doctrine\ORM;
- use Laminas\Paginator\Paginator;
- use Shlinkio\Shlink\Common\Util\DateRange;
- use Shlinkio\Shlink\Core\Entity\ShortUrl;
- use Shlinkio\Shlink\Core\Exception\ShortUrlNotFoundException;
- use Shlinkio\Shlink\Core\Model\ShortUrlMeta;
- use Shlinkio\Shlink\Core\Paginator\Adapter\ShortUrlRepositoryAdapter;
- use Shlinkio\Shlink\Core\Repository\ShortUrlRepository;
- use Shlinkio\Shlink\Core\Service\ShortUrl\FindShortCodeTrait;
- use Shlinkio\Shlink\Core\Util\TagManagerTrait;
- class ShortUrlService implements ShortUrlServiceInterface
- {
- use FindShortCodeTrait;
- use TagManagerTrait;
- private ORM\EntityManagerInterface $em;
- public function __construct(ORM\EntityManagerInterface $em)
- {
- $this->em = $em;
- }
- /**
- * @param string[] $tags
- * @param array|string|null $orderBy
- *
- * @return ShortUrl[]|Paginator
- */
- public function listShortUrls(
- int $page = 1,
- ?string $searchQuery = null,
- array $tags = [],
- $orderBy = null,
- ?DateRange $dateRange = null
- ) {
- /** @var ShortUrlRepository $repo */
- $repo = $this->em->getRepository(ShortUrl::class);
- $paginator = new Paginator(new ShortUrlRepositoryAdapter($repo, $searchQuery, $tags, $orderBy, $dateRange));
- $paginator->setItemCountPerPage(ShortUrlRepositoryAdapter::ITEMS_PER_PAGE)
- ->setCurrentPageNumber($page);
- return $paginator;
- }
- /**
- * @param string[] $tags
- * @throws ShortUrlNotFoundException
- */
- public function setTagsByShortCode(string $shortCode, array $tags = []): ShortUrl
- {
- $shortUrl = $this->findByShortCode($this->em, $shortCode);
- $shortUrl->setTags($this->tagNamesToEntities($this->em, $tags));
- $this->em->flush();
- return $shortUrl;
- }
- /**
- * @throws ShortUrlNotFoundException
- */
- public function updateMetadataByShortCode(string $shortCode, ShortUrlMeta $shortUrlMeta): ShortUrl
- {
- $shortUrl = $this->findByShortCode($this->em, $shortCode);
- $shortUrl->updateMeta($shortUrlMeta);
- $this->em->flush();
- return $shortUrl;
- }
- }
|