Completed
Pull Request — master (#199)
by Alejandro
02:49
created

ShortUrlService::updateMetadataByShortCode()   A

Complexity

Conditions 4
Paths 8

Size

Total Lines 19

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 11
CRAP Score 4

Importance

Changes 0
Metric Value
cc 4
nc 8
nop 2
dl 0
loc 19
ccs 11
cts 11
cp 1
crap 4
rs 9.6333
c 0
b 0
f 0
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\Util\TagManagerTrait;
13
use Zend\Paginator\Paginator;
14
15
class ShortUrlService implements ShortUrlServiceInterface
16
{
17
    use TagManagerTrait;
18
19
    /**
20
     * @var ORM\EntityManagerInterface
21
     */
22
    private $em;
23
24 4
    public function __construct(ORM\EntityManagerInterface $em)
25
    {
26 4
        $this->em = $em;
27 4
    }
28
29
    /**
30
     * @param string[] $tags
31
     * @param array|string|null $orderBy
32
     * @return ShortUrl[]|Paginator
33
     */
34 1
    public function listShortUrls(int $page = 1, string $searchQuery = null, array $tags = [], $orderBy = null)
35
    {
36
        /** @var ShortUrlRepository $repo */
37 1
        $repo = $this->em->getRepository(ShortUrl::class);
38 1
        $paginator = new Paginator(new PaginableRepositoryAdapter($repo, $searchQuery, $tags, $orderBy));
39 1
        $paginator->setItemCountPerPage(PaginableRepositoryAdapter::ITEMS_PER_PAGE)
40 1
                  ->setCurrentPageNumber($page);
41
42 1
        return $paginator;
43
    }
44
45
    /**
46
     * @param string[] $tags
47
     * @throws InvalidShortCodeException
48
     */
49 2
    public function setTagsByShortCode(string $shortCode, array $tags = []): ShortUrl
50
    {
51 2
        $shortUrl = $this->findByShortCode($shortCode);
52 1
        $shortUrl->setTags($this->tagNamesToEntities($this->em, $tags));
53 1
        $this->em->flush();
54
55 1
        return $shortUrl;
56
    }
57
58
    /**
59
     * @throws InvalidShortCodeException
60
     */
61 1
    public function updateMetadataByShortCode(string $shortCode, ShortUrlMeta $shortCodeMeta): ShortUrl
62
    {
63 1
        $shortUrl = $this->findByShortCode($shortCode);
64 1
        if ($shortCodeMeta->hasValidSince()) {
65 1
            $shortUrl->setValidSince($shortCodeMeta->getValidSince());
66
        }
67 1
        if ($shortCodeMeta->hasValidUntil()) {
68 1
            $shortUrl->setValidUntil($shortCodeMeta->getValidUntil());
69
        }
70 1
        if ($shortCodeMeta->hasMaxVisits()) {
71 1
            $shortUrl->setMaxVisits($shortCodeMeta->getMaxVisits());
72
        }
73
74
        /** @var ORM\EntityManager $em */
75 1
        $em = $this->em;
76 1
        $em->flush($shortUrl);
77
78 1
        return $shortUrl;
79
    }
80
81
    /**
82
     * @throws InvalidShortCodeException
83
     */
84
    public function deleteByShortCode(string $shortCode): void
85
    {
86
        $this->em->remove($this->findByShortCode($shortCode));
87
        $this->em->flush();
88
    }
89
90
    /**
91
     * @param string $shortCode
92
     * @return ShortUrl
93
     * @throws InvalidShortCodeException
94
     */
95 3
    private function findByShortCode(string $shortCode): ShortUrl
96
    {
97
        /** @var ShortUrl|null $shortUrl */
98 3
        $shortUrl = $this->em->getRepository(ShortUrl::class)->findOneBy([
99 3
            'shortCode' => $shortCode,
100
        ]);
101 3
        if ($shortUrl === null) {
102 1
            throw InvalidShortCodeException::fromNotFoundShortCode($shortCode);
103
        }
104
105 2
        return $shortUrl;
106
    }
107
}
108