Passed
Push — develop ( 0d7fb1...8577d6 )
by Alejandro
09:01 queued 01:01
created

PersistenceShortUrlRelationResolver   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 29
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 5
eloc 11
dl 0
loc 29
rs 10
c 1
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 1
A resolveDomain() 0 9 2
A resolveApiKey() 0 9 2
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Shlinkio\Shlink\Core\ShortUrl\Resolver;
6
7
use Doctrine\ORM\EntityManagerInterface;
8
use Shlinkio\Shlink\Core\Entity\Domain;
9
use Shlinkio\Shlink\Rest\Entity\ApiKey;
10
11
class PersistenceShortUrlRelationResolver implements ShortUrlRelationResolverInterface
12
{
13
    private EntityManagerInterface $em;
14
15
    public function __construct(EntityManagerInterface $em)
16
    {
17
        $this->em = $em;
18
    }
19
20
    public function resolveDomain(?string $domain): ?Domain
21
    {
22
        if ($domain === null) {
23
            return null;
24
        }
25
26
        /** @var Domain|null $existingDomain */
27
        $existingDomain = $this->em->getRepository(Domain::class)->findOneBy(['authority' => $domain]);
28
        return $existingDomain ?? new Domain($domain);
29
    }
30
31
    public function resolveApiKey(?string $key): ?ApiKey
32
    {
33
        if ($key === null) {
34
            return null;
35
        }
36
37
        /** @var ApiKey|null $existingApiKey */
38
        $existingApiKey = $this->em->getRepository(ApiKey::class)->findOneBy(['key' => $key]);
39
        return $existingApiKey;
40
    }
41
}
42