|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace App\Controller\Registration; |
|
6
|
|
|
|
|
7
|
|
|
use App\Entity\User; |
|
8
|
|
|
use App\Form\UserType; |
|
9
|
|
|
use Doctrine\ORM\EntityManagerInterface; |
|
10
|
|
|
use FOS\RestBundle\Controller\Annotations\View; |
|
11
|
|
|
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController; |
|
12
|
|
|
use Symfony\Component\Form\FormFactoryInterface; |
|
13
|
|
|
use Symfony\Component\HttpFoundation\Request; |
|
14
|
|
|
use Symfony\Component\PasswordHasher\Hasher\UserPasswordHasherInterface; |
|
15
|
|
|
use Symfony\Component\Routing\Annotation\Route; |
|
16
|
|
|
|
|
17
|
|
|
/** |
|
18
|
|
|
* @Route("/api/users", methods={"POST"}, name="api_users_post") |
|
19
|
|
|
* |
|
20
|
|
|
* @View(statusCode=201, serializerGroups={"me"}) |
|
21
|
|
|
*/ |
|
22
|
|
|
final class RegisterController extends AbstractController |
|
23
|
|
|
{ |
|
24
|
|
|
private FormFactoryInterface $formFactory; |
|
25
|
|
|
private UserPasswordHasherInterface $userPasswordHasher; |
|
26
|
|
|
private EntityManagerInterface $entityManager; |
|
27
|
|
|
|
|
28
|
2 |
|
public function __construct( |
|
29
|
|
|
FormFactoryInterface $formFactory, |
|
30
|
|
|
UserPasswordHasherInterface $userPasswordHasher, |
|
31
|
|
|
EntityManagerInterface $entityManager |
|
32
|
|
|
) { |
|
33
|
2 |
|
$this->formFactory = $formFactory; |
|
34
|
2 |
|
$this->userPasswordHasher = $userPasswordHasher; |
|
35
|
2 |
|
$this->entityManager = $entityManager; |
|
36
|
|
|
} |
|
37
|
|
|
|
|
38
|
2 |
|
public function __invoke(Request $request): array |
|
39
|
|
|
{ |
|
40
|
2 |
|
$user = new User(); |
|
41
|
|
|
|
|
42
|
2 |
|
$form = $this->formFactory->createNamed('user', UserType::class, $user); |
|
43
|
2 |
|
$form->submit($request->request->get('user')); |
|
44
|
|
|
|
|
45
|
2 |
|
if ($form->isValid()) { |
|
46
|
1 |
|
$user->setPassword($this->userPasswordHasher->hashPassword($user, $user->getPassword())); |
|
47
|
1 |
|
$this->entityManager->persist($user); |
|
48
|
1 |
|
$this->entityManager->flush(); |
|
49
|
|
|
|
|
50
|
1 |
|
return ['user' => $user]; |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
1 |
|
return ['form' => $form]; |
|
54
|
|
|
} |
|
55
|
|
|
} |
|
56
|
|
|
|