Completed
Push — develop ( f71bd8...4fb2c6 )
by Alejandro
17s queued 14s
created

ShortUrlResolver   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 35
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 12
c 1
b 0
f 0
dl 0
loc 35
rs 10
ccs 13
cts 13
cp 1
wmc 5

3 Methods

Rating   Name   Duplication   Size   Complexity  
A shortCodeToShortUrl() 0 10 2
A shortCodeToEnabledShortUrl() 0 8 2
A __construct() 0 3 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Shlinkio\Shlink\Core\Service\ShortUrl;
6
7
use Doctrine\ORM\EntityManagerInterface;
8
use Shlinkio\Shlink\Core\Entity\ShortUrl;
9
use Shlinkio\Shlink\Core\Exception\ShortUrlNotFoundException;
10
use Shlinkio\Shlink\Core\Repository\ShortUrlRepository;
11
12
class ShortUrlResolver implements ShortUrlResolverInterface
13
{
14
    private EntityManagerInterface $em;
15
16 7
    public function __construct(EntityManagerInterface $em)
17
    {
18 7
        $this->em = $em;
19
    }
20
21
    /**
22
     * @throws ShortUrlNotFoundException
23
     */
24 7
    public function shortCodeToShortUrl(string $shortCode, ?string $domain = null): ShortUrl
25
    {
26
        /** @var ShortUrlRepository $shortUrlRepo */
27 7
        $shortUrlRepo = $this->em->getRepository(ShortUrl::class);
28 7
        $shortUrl = $shortUrlRepo->findOneByShortCode($shortCode, $domain);
29 7
        if ($shortUrl === null) {
30 1
            throw ShortUrlNotFoundException::fromNotFoundShortCode($shortCode, $domain);
31
        }
32
33 6
        return $shortUrl;
34
    }
35
36
    /**
37
     * @throws ShortUrlNotFoundException
38
     */
39 5
    public function shortCodeToEnabledShortUrl(string $shortCode, ?string $domain = null): ShortUrl
40
    {
41 5
        $shortUrl = $this->shortCodeToShortUrl($shortCode, $domain);
42 5
        if (! $shortUrl->isEnabled()) {
43 4
            throw ShortUrlNotFoundException::fromNotFoundShortCode($shortCode, $domain);
44
        }
45
46 1
        return $shortUrl;
47
    }
48
}
49