Passed
Pull Request — master (#50)
by
unknown
04:25 queued 32s
created

DoctrineTenantStorage::findById()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 4
c 1
b 0
f 0
nc 1
nop 1
dl 0
loc 8
rs 10
1
<?php declare(strict_types = 1);
2
3
namespace AtlassianConnectBundle\Storage;
4
5
use AtlassianConnectBundle\Entity\TenantInterface;
6
use Doctrine\ORM\EntityManagerInterface;
7
8
/**
9
 * Class DoctrineTenantStorage
10
 */
11
class DoctrineTenantStorage implements TenantStorageInterface
12
{
13
    /**
14
     * @var EntityManagerInterface
15
     */
16
    private $entityManager;
17
18
    /**
19
     * @var string
20
     */
21
    private $tenantEntityClass;
22
23
    /**
24
     * DoctrineTenantStorage constructor.
25
     *
26
     * @param EntityManagerInterface $entityManager
27
     * @param string                 $tenantEntityClass
28
     */
29
    public function __construct(EntityManagerInterface $entityManager, string $tenantEntityClass)
30
    {
31
        $this->entityManager = $entityManager;
32
        $this->tenantEntityClass = $tenantEntityClass;
33
    }
34
35
    /**
36
     * @param string $clientKey
37
     * @return TenantInterface|null
38
     */
39
    public function findByClientKey(string $clientKey): ?TenantInterface
40
    {
41
        /** @var TenantInterface|null $tenant */
42
        $tenant = $this->entityManager
43
            ->getRepository($this->tenantEntityClass)
44
            ->findOneBy(['clientKey' => $clientKey]);
45
46
        return $tenant;
47
    }
48
49
    /**
50
     * @param int $tenantId
51
     * @return TenantInterface|null
52
     */
53
    public function findById(int $tenantId): ?TenantInterface
54
    {
55
        /** @var TenantInterface|null $tenant */
56
        $tenant = $this->entityManager
57
            ->getRepository($this->tenantEntityClass)
58
            ->find($tenantId);
59
60
        return $tenant;
61
    }
62
63
    /**
64
     * @param TenantInterface $tenant
65
     */
66
    public function persist(TenantInterface $tenant): void
67
    {
68
        $this->entityManager->persist($tenant);
69
        $this->entityManager->flush();
70
    }
71
}
72