Completed
Push — master ( 249002...76efa7 )
by Julito
09:55
created

AccountController::editAction()   A

Complexity

Conditions 5
Paths 3

Size

Total Lines 27
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 5
eloc 14
c 1
b 0
f 0
nc 3
nop 3
dl 0
loc 27
rs 9.4888
1
<?php
2
/* For licensing terms, see /license.txt */
3
4
namespace Chamilo\CoreBundle\Controller;
5
6
use Chamilo\ThemeBundle\Model\UserInterface;
7
use Chamilo\UserBundle\Entity\User;
8
use Chamilo\UserBundle\Form\ProfileType;
9
use Chamilo\UserBundle\Repository\UserRepository;
10
use FOS\UserBundle\Model\UserManagerInterface;
11
use Symfony\Component\HttpFoundation\RedirectResponse;
12
use Symfony\Component\HttpFoundation\Request;
13
use Symfony\Component\Routing\Annotation\Route;
14
use Symfony\Contracts\Translation\TranslatorInterface;
15
16
/**
17
 * Class UserController.
18
 *
19
 * @Route("/account")
20
 *
21
 * @author Julio Montoya <[email protected]>
22
 */
23
class AccountController extends BaseController
24
{
25
    private $userRepository;
26
    private $formFactory;
0 ignored issues
show
introduced by
The private property $formFactory is not used, and could be removed.
Loading history...
27
28
    public function __construct(UserRepository $userRepository)
29
    {
30
        $this->userRepository = $userRepository;
31
    }
32
33
    /**
34
     * @Route("/edit", methods={"GET", "POST"}, name="chamilo_core_account_edit")
35
     *
36
     * @param string $username
37
     */
38
    public function editAction(Request $request, UserManagerInterface $userManager, TranslatorInterface $translator)
39
    {
40
        $user = $this->getUser();
41
42
        if (!is_object($user) || !$user instanceof UserInterface) {
43
            throw $this->createAccessDeniedException(
44
                'This user does not have access to this section'
45
            );
46
        }
47
48
        $form = $this->createForm(ProfileType::class, $user);
49
        $form->setData($user);
50
51
        $form->handleRequest($request);
52
53
        if ($form->isSubmitted() && $form->isValid()) {
54
            //$event = new FormEvent($form, $request);
55
            $userManager->updateUser($user);
56
57
            $this->addFlash('success', $translator->trans('Updated'));
58
            $url = $this->generateUrl('chamilo_core_user_profile', ['username' => $user->getUsername()]);
59
            $response = new RedirectResponse($url);
60
61
            return $response;
62
        }
63
64
        return $this->render('@ChamiloCore/Account/edit.html.twig', ['form' => $form->createView()]);
65
    }
66
}
67