Completed
Push — master ( 5b9784...ff8441 )
by Alejandro
12s
created

FindShortCodeTrait::findByShortCode()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 12

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 2

Importance

Changes 0
Metric Value
cc 2
nc 2
nop 2
dl 0
loc 12
ccs 6
cts 6
cp 1
crap 2
rs 9.8666
c 0
b 0
f 0
1
<?php
2
declare(strict_types=1);
3
4
namespace Shlinkio\Shlink\Core\Service\ShortUrl;
5
6
use Doctrine\ORM\EntityManagerInterface;
7
use Shlinkio\Shlink\Core\Entity\ShortUrl;
8
use Shlinkio\Shlink\Core\Exception\InvalidShortCodeException;
9
10
trait FindShortCodeTrait
11
{
12
    /**
13
     * @param string $shortCode
14
     * @return ShortUrl
15
     * @throws InvalidShortCodeException
16
     */
17 7
    private function findByShortCode(EntityManagerInterface $em, string $shortCode): ShortUrl
18
    {
19
        /** @var ShortUrl|null $shortUrl */
20 7
        $shortUrl = $em->getRepository(ShortUrl::class)->findOneBy([
21 7
            'shortCode' => $shortCode,
22
        ]);
23 7
        if ($shortUrl === null) {
24 1
            throw InvalidShortCodeException::fromNotFoundShortCode($shortCode);
25
        }
26
27 6
        return $shortUrl;
28
    }
29
}
30