2016-06-18 09:43:29 +02:00
|
|
|
<?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;
|
2016-06-18 09:43:29 +02:00
|
|
|
|
2018-01-07 19:51:25 +01:00
|
|
|
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;
|
2016-08-21 16:52:26 +02:00
|
|
|
use Shlinkio\Shlink\Core\Exception\InvalidShortCodeException;
|
2018-01-07 20:00:21 +01:00
|
|
|
use Shlinkio\Shlink\Core\Model\ShortUrlMeta;
|
2016-07-19 18:01:39 +02:00
|
|
|
use Shlinkio\Shlink\Core\Repository\ShortUrlRepository;
|
2018-09-15 11:54:58 +02:00
|
|
|
use Shlinkio\Shlink\Core\Service\ShortUrl\FindShortCodeTrait;
|
2016-08-21 16:52:26 +02:00
|
|
|
use Shlinkio\Shlink\Core\Util\TagManagerTrait;
|
2016-07-04 08:57:37 +02:00
|
|
|
use Zend\Paginator\Paginator;
|
2016-06-18 09:43:29 +02:00
|
|
|
|
|
|
|
|
class ShortUrlService implements ShortUrlServiceInterface
|
|
|
|
|
{
|
2018-09-15 11:54:58 +02:00
|
|
|
use FindShortCodeTrait;
|
2016-08-21 16:52:26 +02:00
|
|
|
use TagManagerTrait;
|
|
|
|
|
|
2018-11-20 19:30:27 +01:00
|
|
|
/** @var ORM\EntityManagerInterface */
|
2016-06-18 09:43:29 +02:00
|
|
|
private $em;
|
|
|
|
|
|
2018-01-07 19:51:25 +01:00
|
|
|
public function __construct(ORM\EntityManagerInterface $em)
|
2016-06-18 09:43:29 +02:00
|
|
|
{
|
|
|
|
|
$this->em = $em;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2018-09-15 11:01:23 +02:00
|
|
|
* @param string[] $tags
|
|
|
|
|
* @param array|string|null $orderBy
|
2016-10-22 12:40:51 +02:00
|
|
|
* @return ShortUrl[]|Paginator
|
2016-06-18 09:43:29 +02:00
|
|
|
*/
|
2019-08-01 19:49:54 +02:00
|
|
|
public function listShortUrls(int $page = 1, ?string $searchQuery = null, array $tags = [], $orderBy = null)
|
2016-06-18 09:43:29 +02:00
|
|
|
{
|
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;
|
2016-06-18 09:43:29 +02:00
|
|
|
}
|
2016-08-21 16:52:26 +02:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @param string[] $tags
|
|
|
|
|
* @throws InvalidShortCodeException
|
|
|
|
|
*/
|
2018-01-07 19:51:25 +01:00
|
|
|
public function setTagsByShortCode(string $shortCode, array $tags = []): ShortUrl
|
|
|
|
|
{
|
2018-09-15 11:54:58 +02:00
|
|
|
$shortUrl = $this->findByShortCode($this->em, $shortCode);
|
2018-01-07 19:51:25 +01:00
|
|
|
$shortUrl->setTags($this->tagNamesToEntities($this->em, $tags));
|
|
|
|
|
$this->em->flush();
|
|
|
|
|
|
|
|
|
|
return $shortUrl;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @throws InvalidShortCodeException
|
|
|
|
|
*/
|
2018-10-28 16:00:54 +01:00
|
|
|
public function updateMetadataByShortCode(string $shortCode, ShortUrlMeta $shortUrlMeta): ShortUrl
|
2018-01-07 19:51:25 +01:00
|
|
|
{
|
2018-09-15 11:54:58 +02:00
|
|
|
$shortUrl = $this->findByShortCode($this->em, $shortCode);
|
2018-10-28 16:00:54 +01:00
|
|
|
$shortUrl->updateMeta($shortUrlMeta);
|
2018-01-07 19:51:25 +01:00
|
|
|
|
|
|
|
|
/** @var ORM\EntityManager $em */
|
|
|
|
|
$em = $this->em;
|
|
|
|
|
$em->flush($shortUrl);
|
2018-09-15 11:01:23 +02:00
|
|
|
|
2018-01-07 19:51:25 +01:00
|
|
|
return $shortUrl;
|
|
|
|
|
}
|
2016-06-18 09:43:29 +02:00
|
|
|
}
|