Passed
Branch develop (f6de6e)
by Nicolas
04:27
created

UserPutController   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 53
Duplicated Lines 0 %

Test Coverage

Coverage 92.31%

Importance

Changes 0
Metric Value
dl 0
loc 53
ccs 12
cts 13
cp 0.9231
rs 10
c 0
b 0
f 0
wmc 4

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __invoke() 0 14 3
A __construct() 0 8 1
1
<?php
2
3
namespace App\Controller\Api;
4
5
use App\Form\UserType;
6
use App\Security\UserResolver;
7
use Doctrine\ORM\EntityManagerInterface;
8
use FOS\RestBundle\Controller\Annotations\View;
9
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Method;
10
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Security;
11
use Symfony\Component\Form\FormFactoryInterface;
12
use Symfony\Component\Form\FormInterface;
13
use Symfony\Component\HttpFoundation\Request;
14
use Symfony\Component\Routing\Annotation\Route;
15
16
/**
17
 * @Route("/api/user", name="api_users_put")
18
 * @Method("PUT")
19
 *
20
 * @View(statusCode=200, serializerGroups={"me"})
21
 *
22
 * @Security("is_granted('ROLE_USER')")
23
 */
24
final class UserPutController
25
{
26
    /**
27
     * @var FormFactoryInterface
28
     */
29
    private $formFactory;
30
31
    /**
32
     * @var EntityManagerInterface
33
     */
34
    private $entityManager;
35
36
    /**
37
     * @var UserResolver
38
     */
39
    private $userResolver;
40
41
    /**
42
     * @param FormFactoryInterface   $formFactory
43
     * @param EntityManagerInterface $entityManager
44
     * @param UserResolver           $userResolver
45
     */
46 2
    public function __construct(
47
        FormFactoryInterface $formFactory,
48
        EntityManagerInterface $entityManager,
49
        UserResolver $userResolver
50
    ) {
51 2
        $this->formFactory = $formFactory;
52 2
        $this->entityManager = $entityManager;
53 2
        $this->userResolver = $userResolver;
54 2
    }
55
56
    /**
57
     * @param Request $request
58
     *
59
     * @throws \Exception
60
     *
61
     * @return array|FormInterface
62
     */
63 1
    public function __invoke(Request $request)
64
    {
65 1
        $user = $this->userResolver->getCurrentUser();
66
67 1
        $form = $this->formFactory->createNamed('user', UserType::class, $user);
68 1
        $form->submit($request->request->get('user'), false);
69
70 1
        if ($form->isSubmitted() && $form->isValid()) {
71 1
            $this->entityManager->flush();
72
73 1
            return ['user' => $user];
74
        }
75
76
        return $form;
77
    }
78
}
79