|
1
|
|
|
<?php |
|
2
|
|
|
declare(strict_types=1); |
|
3
|
|
|
|
|
4
|
|
|
namespace Shlinkio\Shlink\Core\Service; |
|
5
|
|
|
|
|
6
|
|
|
use Doctrine\ORM; |
|
7
|
|
|
use Shlinkio\Shlink\Common\Paginator\Adapter\PaginableRepositoryAdapter; |
|
8
|
|
|
use Shlinkio\Shlink\Core\Entity\ShortUrl; |
|
9
|
|
|
use Shlinkio\Shlink\Core\Exception\InvalidShortCodeException; |
|
10
|
|
|
use Shlinkio\Shlink\Core\Model\ShortUrlMeta; |
|
11
|
|
|
use Shlinkio\Shlink\Core\Repository\ShortUrlRepository; |
|
12
|
|
|
use Shlinkio\Shlink\Core\Service\ShortUrl\FindShortCodeTrait; |
|
13
|
|
|
use Shlinkio\Shlink\Core\Util\TagManagerTrait; |
|
14
|
|
|
use Zend\Paginator\Paginator; |
|
15
|
|
|
|
|
16
|
|
|
class ShortUrlService implements ShortUrlServiceInterface |
|
17
|
|
|
{ |
|
18
|
|
|
use FindShortCodeTrait; |
|
19
|
|
|
use TagManagerTrait; |
|
20
|
|
|
|
|
21
|
|
|
/** |
|
22
|
|
|
* @var ORM\EntityManagerInterface |
|
23
|
|
|
*/ |
|
24
|
|
|
private $em; |
|
25
|
|
|
|
|
26
|
4 |
|
public function __construct(ORM\EntityManagerInterface $em) |
|
27
|
|
|
{ |
|
28
|
4 |
|
$this->em = $em; |
|
29
|
4 |
|
} |
|
30
|
|
|
|
|
31
|
|
|
/** |
|
32
|
|
|
* @param string[] $tags |
|
33
|
|
|
* @param array|string|null $orderBy |
|
34
|
|
|
* @return ShortUrl[]|Paginator |
|
35
|
|
|
*/ |
|
36
|
1 |
|
public function listShortUrls(int $page = 1, string $searchQuery = null, array $tags = [], $orderBy = null) |
|
37
|
|
|
{ |
|
38
|
|
|
/** @var ShortUrlRepository $repo */ |
|
39
|
1 |
|
$repo = $this->em->getRepository(ShortUrl::class); |
|
40
|
1 |
|
$paginator = new Paginator(new PaginableRepositoryAdapter($repo, $searchQuery, $tags, $orderBy)); |
|
41
|
1 |
|
$paginator->setItemCountPerPage(PaginableRepositoryAdapter::ITEMS_PER_PAGE) |
|
42
|
1 |
|
->setCurrentPageNumber($page); |
|
43
|
|
|
|
|
44
|
1 |
|
return $paginator; |
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
|
|
/** |
|
48
|
|
|
* @param string[] $tags |
|
49
|
|
|
* @throws InvalidShortCodeException |
|
50
|
|
|
*/ |
|
51
|
2 |
|
public function setTagsByShortCode(string $shortCode, array $tags = []): ShortUrl |
|
52
|
|
|
{ |
|
53
|
2 |
|
$shortUrl = $this->findByShortCode($this->em, $shortCode); |
|
54
|
1 |
|
$shortUrl->setTags($this->tagNamesToEntities($this->em, $tags)); |
|
55
|
1 |
|
$this->em->flush(); |
|
56
|
|
|
|
|
57
|
1 |
|
return $shortUrl; |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
|
|
/** |
|
61
|
|
|
* @throws InvalidShortCodeException |
|
62
|
|
|
*/ |
|
63
|
1 |
|
public function updateMetadataByShortCode(string $shortCode, ShortUrlMeta $shortCodeMeta): ShortUrl |
|
64
|
|
|
{ |
|
65
|
1 |
|
$shortUrl = $this->findByShortCode($this->em, $shortCode); |
|
66
|
1 |
|
if ($shortCodeMeta->hasValidSince()) { |
|
67
|
1 |
|
$shortUrl->setValidSince($shortCodeMeta->getValidSince()); |
|
68
|
|
|
} |
|
69
|
1 |
|
if ($shortCodeMeta->hasValidUntil()) { |
|
70
|
1 |
|
$shortUrl->setValidUntil($shortCodeMeta->getValidUntil()); |
|
71
|
|
|
} |
|
72
|
1 |
|
if ($shortCodeMeta->hasMaxVisits()) { |
|
73
|
1 |
|
$shortUrl->setMaxVisits($shortCodeMeta->getMaxVisits()); |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
|
|
/** @var ORM\EntityManager $em */ |
|
77
|
1 |
|
$em = $this->em; |
|
78
|
1 |
|
$em->flush($shortUrl); |
|
79
|
|
|
|
|
80
|
1 |
|
return $shortUrl; |
|
81
|
|
|
} |
|
82
|
|
|
} |
|
83
|
|
|
|