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

ShortUrlResolver::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 1
b 0
f 0
nc 1
nop 1
dl 0
loc 3
rs 10
ccs 2
cts 2
cp 1
crap 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