|
1
|
|
|
<?php |
|
2
|
|
|
namespace App\Controller\Security; |
|
3
|
|
|
|
|
4
|
|
|
use App\DTO\UserRegistrationDTO; |
|
5
|
|
|
use App\Form\Type\UserRegistrationType; |
|
6
|
|
|
use App\Repository\UserRepository; |
|
7
|
|
|
use App\Command\RegisterUserCommand; |
|
8
|
|
|
use Symfony\Bundle\FrameworkBundle\Controller\Controller; |
|
9
|
|
|
use Symfony\Component\HttpFoundation\Request; |
|
10
|
|
|
use Symfony\Component\Routing\Annotation\Route; |
|
11
|
|
|
use Ramsey\Uuid\Uuid; |
|
12
|
|
|
use Symfony\Component\Form\FormError; |
|
13
|
|
|
use Psr\Log\LoggerInterface; |
|
14
|
|
|
use Symfony\Component\Messenger\MessageBusInterface; |
|
15
|
|
|
use App\Service\TokenStorageService; |
|
16
|
|
|
|
|
17
|
|
|
class RegistrationController extends Controller |
|
|
|
|
|
|
18
|
|
|
{ |
|
19
|
|
|
/** |
|
20
|
|
|
* @var MessageBusInterface |
|
21
|
|
|
*/ |
|
22
|
|
|
private $commandBus; |
|
23
|
|
|
/** |
|
24
|
|
|
* @var LoggerInterface |
|
25
|
|
|
*/ |
|
26
|
|
|
private $logger; |
|
27
|
|
|
/** |
|
28
|
|
|
* @var UserRepository |
|
29
|
|
|
*/ |
|
30
|
|
|
private $repository; |
|
31
|
|
|
|
|
32
|
|
|
/** |
|
33
|
|
|
* @param MessageBusInterface $commandBus |
|
34
|
|
|
* @param LoggerInterface $logger |
|
35
|
|
|
* @param UserRepository $repository |
|
36
|
|
|
*/ |
|
37
|
|
|
public function __construct( |
|
38
|
|
|
MessageBusInterface $commandBus, |
|
39
|
|
|
LoggerInterface $logger, |
|
40
|
|
|
UserRepository $repository |
|
41
|
|
|
) |
|
42
|
|
|
{ |
|
43
|
|
|
$this->commandBus = $commandBus; |
|
44
|
|
|
$this->logger = $logger; |
|
45
|
|
|
$this->repository = $repository; |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
|
|
/** |
|
49
|
|
|
* @Route("/register", name="register", methods={"GET", "POST"}) |
|
50
|
|
|
*/ |
|
51
|
|
|
public function register(Request $request, TokenStorageService $tokenStorageService) |
|
52
|
|
|
{ |
|
53
|
|
|
$userRegistrationDTO = new UserRegistrationDTO; |
|
54
|
|
|
$form = $this->createForm(UserRegistrationType::class, $userRegistrationDTO); |
|
55
|
|
|
$form->handleRequest($request); |
|
56
|
|
|
try { |
|
57
|
|
|
if ($form->isSubmitted() && $form->isValid()) { |
|
58
|
|
|
$userRegistrationDTO = $form->getData(); |
|
59
|
|
|
$command = new RegisterUserCommand( |
|
60
|
|
|
Uuid::uuid4(), |
|
61
|
|
|
$userRegistrationDTO->getFirstName(), |
|
62
|
|
|
$userRegistrationDTO->getLastName(), |
|
63
|
|
|
$userRegistrationDTO->getEmail(), |
|
64
|
|
|
$userRegistrationDTO->getPlainPassword() |
|
65
|
|
|
); |
|
66
|
|
|
|
|
67
|
|
|
$this->commandBus->dispatch($command); |
|
68
|
|
|
$user = $this->repository->getUser($command->getId()); |
|
69
|
|
|
$tokenStorageService->storeToken($user); |
|
70
|
|
|
$this->addFlash('success', "Your accound was created"); |
|
71
|
|
|
return $this->redirectToRoute('admin_dashboard'); |
|
72
|
|
|
} |
|
73
|
|
|
} catch (\Exception $e) { |
|
74
|
|
|
$this->addFlash('danger','Error while registering'); |
|
75
|
|
|
$error = new FormError("There is an error: ".$e->getMessage()); |
|
76
|
|
|
$form->addError($error); |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
|
|
return $this->render( |
|
80
|
|
|
'security/register.html.twig',['form' => $form->createView()] |
|
81
|
|
|
); |
|
82
|
|
|
} |
|
83
|
|
|
} |