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