Files
shlink/module/Core/src/Service/ShortUrlService.php

73 lines
2.1 KiB
PHP
Raw Normal View History

<?php
2017-10-12 10:13:20 +02:00
declare(strict_types=1);
2016-07-19 18:01:39 +02:00
namespace Shlinkio\Shlink\Core\Service;
use Doctrine\ORM;
2016-07-19 17:38:41 +02:00
use Shlinkio\Shlink\Common\Paginator\Adapter\PaginableRepositoryAdapter;
2016-07-19 18:01:39 +02:00
use Shlinkio\Shlink\Core\Entity\ShortUrl;
use Shlinkio\Shlink\Core\Exception\InvalidShortCodeException;
use Shlinkio\Shlink\Core\Model\ShortUrlMeta;
2016-07-19 18:01:39 +02:00
use Shlinkio\Shlink\Core\Repository\ShortUrlRepository;
use Shlinkio\Shlink\Core\Service\ShortUrl\FindShortCodeTrait;
use Shlinkio\Shlink\Core\Util\TagManagerTrait;
use Zend\Paginator\Paginator;
class ShortUrlService implements ShortUrlServiceInterface
{
use FindShortCodeTrait;
use TagManagerTrait;
/** @var ORM\EntityManagerInterface */
private $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)
{
2016-07-04 09:15:50 +02:00
/** @var ShortUrlRepository $repo */
$repo = $this->em->getRepository(ShortUrl::class);
2016-10-22 23:02:12 +02:00
$paginator = new Paginator(new PaginableRepositoryAdapter($repo, $searchQuery, $tags, $orderBy));
2016-07-04 09:15:50 +02:00
$paginator->setItemCountPerPage(PaginableRepositoryAdapter::ITEMS_PER_PAGE)
->setCurrentPageNumber($page);
return $paginator;
}
/**
* @param string[] $tags
* @throws InvalidShortCodeException
*/
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 InvalidShortCodeException
*/
public function updateMetadataByShortCode(string $shortCode, ShortUrlMeta $shortUrlMeta): ShortUrl
{
$shortUrl = $this->findByShortCode($this->em, $shortCode);
$shortUrl->updateMeta($shortUrlMeta);
/** @var ORM\EntityManager $em */
$em = $this->em;
$em->flush($shortUrl);
return $shortUrl;
}
}