Completed
Pull Request — master (#121)
by Alejandro
08:44
created

ShortUrlService::updateMetadataByShortCode()   A

Complexity

Conditions 4
Paths 8

Size

Total Lines 18
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 11
CRAP Score 4

Importance

Changes 0
Metric Value
cc 4
eloc 11
nc 8
nop 2
dl 0
loc 18
rs 9.2
c 0
b 0
f 0
ccs 11
cts 11
cp 1
crap 4
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 int $page
31
     * @param string $searchQuery
32
     * @param array $tags
33
     * @param null $orderBy
34
     * @return ShortUrl[]|Paginator
35
     */
36 1
    public function listShortUrls($page = 1, $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 $shortCode
49
     * @param string[] $tags
50
     * @return ShortUrl
51
     * @throws InvalidShortCodeException
52
     */
53 2
    public function setTagsByShortCode(string $shortCode, array $tags = []): ShortUrl
54
    {
55 2
        $shortUrl = $this->findByShortCode($shortCode);
56 1
        $shortUrl->setTags($this->tagNamesToEntities($this->em, $tags));
57 1
        $this->em->flush();
58
59 1
        return $shortUrl;
60
    }
61
62
    /**
63
     * @param string $shortCode
64
     * @param ShortUrlMeta $shortCodeMeta
65
     * @return ShortUrl
66
     * @throws InvalidShortCodeException
67
     */
68 1
    public function updateMetadataByShortCode(string $shortCode, ShortUrlMeta $shortCodeMeta): ShortUrl
69
    {
70 1
        $shortUrl = $this->findByShortCode($shortCode);
71 1
        if ($shortCodeMeta->hasValidSince()) {
72 1
            $shortUrl->setValidSince($shortCodeMeta->getValidSince());
73
        }
74 1
        if ($shortCodeMeta->hasValidUntil()) {
75 1
            $shortUrl->setValidUntil($shortCodeMeta->getValidUntil());
76
        }
77 1
        if ($shortCodeMeta->hasMaxVisits()) {
78 1
            $shortUrl->setMaxVisits($shortCodeMeta->getMaxVisits());
79
        }
80
81
        /** @var ORM\EntityManager $em */
82 1
        $em = $this->em;
83 1
        $em->flush($shortUrl);
84 1
        return $shortUrl;
85
    }
86
87
    /**
88
     * @param string $shortCode
89
     * @return ShortUrl
90
     * @throws InvalidShortCodeException
91
     */
92 3
    private function findByShortCode(string $shortCode): ShortUrl
93
    {
94
        /** @var ShortUrl|null $shortUrl */
95 3
        $shortUrl = $this->em->getRepository(ShortUrl::class)->findOneBy([
96 3
            'shortCode' => $shortCode,
97
        ]);
98 3
        if ($shortUrl === null) {
99 1
            throw InvalidShortCodeException::fromNotFoundShortCode($shortCode);
100
        }
101
102 2
        return $shortUrl;
103
    }
104
}
105