Passed
Push — master ( 0171d8...f6fed5 )
by Nicolas
05:33
created

UpdateUserController   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 30
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 14
dl 0
loc 30
ccs 13
cts 13
cp 1
rs 10
c 1
b 0
f 0
wmc 3

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 8 1
A __invoke() 0 14 2
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