|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace App\Security\Registration; |
|
4
|
|
|
|
|
5
|
|
|
use App\Entity\RegistrationCode; |
|
6
|
|
|
use App\Entity\User; |
|
7
|
|
|
use App\Repository\RegistrationCodeRepositoryInterface; |
|
8
|
|
|
use App\Repository\UserRepositoryInterface; |
|
9
|
|
|
use App\Repository\UserTypeRepositoryInterface; |
|
10
|
|
|
use App\Security\EmailConfirmation\ConfirmationManager; |
|
11
|
|
|
use Symfony\Component\PasswordHasher\Hasher\UserPasswordHasherInterface; |
|
12
|
|
|
|
|
13
|
|
|
class RegistrationCodeManager { |
|
14
|
|
|
|
|
15
|
|
|
public function __construct(private RegistrationCodeRepositoryInterface $codeRepository, private UserRepositoryInterface $userRepository, private UserPasswordHasherInterface $passwordHasher, private UserTypeRepositoryInterface $typeRepository, private ConfirmationManager $confirmationManager) |
|
16
|
|
|
{ |
|
17
|
|
|
} |
|
18
|
|
|
|
|
19
|
|
|
public function getTemplateUser(): User { |
|
20
|
|
|
$type = $this->typeRepository->findOneByAlias('parent'); |
|
21
|
|
|
$user = new User(); |
|
22
|
|
|
$user->setType($type); |
|
|
|
|
|
|
23
|
|
|
|
|
24
|
|
|
return $user; |
|
25
|
|
|
} |
|
26
|
|
|
|
|
27
|
|
|
public function isRedeemed(RegistrationCode $code): bool { |
|
28
|
|
|
return $code->getRedeemingUser() !== null; |
|
29
|
|
|
} |
|
30
|
|
|
|
|
31
|
|
|
/** |
|
32
|
|
|
* @throws CodeAlreadyRedeemedException |
|
33
|
|
|
*/ |
|
34
|
|
|
public function complete(RegistrationCode $code, User $user, string $password): void { |
|
35
|
|
|
if($this->isRedeemed($code)) { |
|
36
|
|
|
throw new CodeAlreadyRedeemedException(); |
|
37
|
|
|
} |
|
38
|
|
|
|
|
39
|
|
|
$type = $this->typeRepository->findOneByAlias('parent'); |
|
40
|
|
|
$user |
|
41
|
|
|
->setType($type) |
|
|
|
|
|
|
42
|
|
|
->setIsEmailConfirmationPending($user->getEmail() !== null) |
|
43
|
|
|
->setPassword($this->passwordHasher->hashPassword($user, $password)); |
|
44
|
|
|
|
|
45
|
|
|
$user->addLinkedStudent($code->getStudent()); |
|
|
|
|
|
|
46
|
|
|
$code->setRedeemingUser($user); |
|
47
|
|
|
|
|
48
|
|
|
$this->userRepository->persist($user); |
|
49
|
|
|
$this->codeRepository->persist($code); |
|
50
|
|
|
|
|
51
|
|
|
if($user->getEmail() !== null) { |
|
52
|
|
|
$this->confirmationManager->newConfirmation($user, $user->getEmail()); |
|
53
|
|
|
$user->setIsEmailConfirmationPending(true); |
|
54
|
|
|
$user->setEmail(null); |
|
55
|
|
|
|
|
56
|
|
|
$this->userRepository->persist($user); |
|
57
|
|
|
} |
|
58
|
|
|
} |
|
59
|
|
|
} |