ShortUrlResolver::__construct()   A
last analyzed

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\Model\ShortUrlIdentifier;
11
use Shlinkio\Shlink\Core\Repository\ShortUrlRepository;
12
13
class ShortUrlResolver implements ShortUrlResolverInterface
14
{
15
    private EntityManagerInterface $em;
16
17 8
    public function __construct(EntityManagerInterface $em)
18
    {
19 8
        $this->em = $em;
20 8
    }
21
22
    /**
23
     * @throws ShortUrlNotFoundException
24
     */
25 3
    public function resolveShortUrl(ShortUrlIdentifier $identifier): ShortUrl
26
    {
27
        /** @var ShortUrlRepository $shortUrlRepo */
28 3
        $shortUrlRepo = $this->em->getRepository(ShortUrl::class);
29 3
        $shortUrl = $shortUrlRepo->findOne($identifier->shortCode(), $identifier->domain());
30 3
        if ($shortUrl === null) {
31 2
            throw ShortUrlNotFoundException::fromNotFound($identifier);
32
        }
33
34 2
        return $shortUrl;
35
    }
36
37
    /**
38
     * @throws ShortUrlNotFoundException
39
     */
40 6
    public function resolveEnabledShortUrl(ShortUrlIdentifier $identifier): ShortUrl
41
    {
42
        /** @var ShortUrlRepository $shortUrlRepo */
43 6
        $shortUrlRepo = $this->em->getRepository(ShortUrl::class);
44 6
        $shortUrl = $shortUrlRepo->findOneWithDomainFallback($identifier->shortCode(), $identifier->domain());
45 6
        if ($shortUrl === null || ! $shortUrl->isEnabled()) {
46 5
            throw ShortUrlNotFoundException::fromNotFound($identifier);
47
        }
48
49 2
        return $shortUrl;
50
    }
51
}
52