|
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
|
|
|
|