1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
|
4
|
|
|
namespace Tahoe\Bundle\MultiTenancyBundle\Handler; |
5
|
|
|
|
6
|
|
|
|
7
|
|
|
use Doctrine\ORM\EntityManager; |
8
|
|
|
use Doctrine\ORM\EntityRepository; |
9
|
|
|
use Tahoe\Bundle\MultiTenancyBundle\Factory\InvitationFactory; |
10
|
|
|
use Tahoe\Bundle\MultiTenancyBundle\Model\InvitationInterface; |
11
|
|
|
use Tahoe\Bundle\MultiTenancyBundle\Repository\InvitationRepository; |
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* Class InvitationHandler |
15
|
|
|
* |
16
|
|
|
* @author Konrad Podgórski <[email protected]> |
17
|
|
|
*/ |
18
|
|
|
class InvitationHandler |
19
|
|
|
{ |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* @var EntityManager |
23
|
|
|
*/ |
24
|
|
|
protected $entityManager; |
25
|
|
|
/** |
26
|
|
|
* @var InvitationFactory |
27
|
|
|
*/ |
28
|
|
|
protected $invitationFactory; |
29
|
|
|
/** |
30
|
|
|
* @var InvitationRepository |
31
|
|
|
*/ |
32
|
|
|
protected $invitationRepository; |
33
|
|
|
/** |
34
|
|
|
* @var EntityRepository |
35
|
|
|
*/ |
36
|
|
|
protected $userRepository; |
37
|
|
|
/** |
38
|
|
|
* @var TenantUserHandler |
39
|
|
|
*/ |
40
|
|
|
protected $tenantUserHandler; |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* @param $entityManager |
44
|
|
|
* @param $invitationFactory |
45
|
|
|
* @param $invitationRepository |
46
|
|
|
* @param $tenantUserHandler |
47
|
|
|
* @param $userRepository |
48
|
|
|
*/ |
49
|
|
|
function __construct( |
50
|
|
|
$entityManager, |
51
|
|
|
$invitationFactory, |
52
|
|
|
$invitationRepository, |
53
|
|
|
$tenantUserHandler, |
54
|
|
|
$userRepository |
55
|
|
|
) { |
56
|
|
|
$this->entityManager = $entityManager; |
57
|
|
|
$this->invitationFactory = $invitationFactory; |
58
|
|
|
$this->invitationRepository = $invitationRepository; |
59
|
|
|
$this->tenantUserHandler = $tenantUserHandler; |
60
|
|
|
$this->userRepository = $userRepository; |
61
|
|
|
} |
62
|
|
|
|
63
|
|
View Code Duplication |
public function acceptInvitationById($invitationId) |
|
|
|
|
64
|
|
|
{ |
65
|
|
|
$invitation = $this->invitationRepository->find($invitationId); |
66
|
|
|
|
67
|
|
|
if ($invitation === null) { |
68
|
|
|
throw new \Exception(sprintf( |
69
|
|
|
'Invitation with id %d doesn\'t exist. Maybe it\'s from different tenant?', |
70
|
|
|
$invitationId |
71
|
|
|
)); |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
return $this->acceptInvitation($invitation); |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* @param InvitationInterface $invitation |
79
|
|
|
* |
80
|
|
|
* @return \Tahoe\Bundle\MultiTenancyBundle\Entity\TenantUser |
81
|
|
|
*/ |
82
|
|
|
public function acceptInvitation(InvitationInterface $invitation) |
83
|
|
|
{ |
84
|
|
|
$user = $this->userRepository->findOneBy(array('emailCanonical' => $invitation->getEmail())); |
85
|
|
|
|
86
|
|
|
$tenant = $invitation->getTenant(); |
87
|
|
|
|
88
|
|
|
$tenantUser = $this->tenantUserHandler->addUserToTenant($user, $tenant); |
89
|
|
|
|
90
|
|
|
$this->entityManager->flush(); |
91
|
|
|
|
92
|
|
|
$this->delete($invitation, true); |
93
|
|
|
|
94
|
|
|
return $tenantUser; |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
public function delete($invitation, $withFlush = false) |
98
|
|
|
{ |
99
|
|
|
$this->entityManager->remove($invitation); |
100
|
|
|
|
101
|
|
|
if ($withFlush) { |
102
|
|
|
$this->entityManager->flush(); |
103
|
|
|
} |
104
|
|
|
} |
105
|
|
|
|
106
|
|
View Code Duplication |
public function rejectInvitationById($invitationId) |
|
|
|
|
107
|
|
|
{ |
108
|
|
|
$invitation = $this->invitationRepository->find($invitationId); |
109
|
|
|
|
110
|
|
|
if ($invitation === null) { |
111
|
|
|
throw new \Exception(sprintf( |
112
|
|
|
'Invitation with id %d doesn\'t exist. Maybe it\'s from different tenant?', |
113
|
|
|
$invitationId |
114
|
|
|
)); |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
return $this->rejectInvitation($invitation); |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
public function rejectInvitation(InvitationInterface $invitation) |
121
|
|
|
{ |
122
|
|
|
$this->delete($invitation, true); |
123
|
|
|
|
124
|
|
|
return true; |
125
|
|
|
} |
126
|
|
|
} |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.