Completed
Push — master ( bf9357...375c96 )
by Julito
14:39 queued 22s
created

AccountController   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 63
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 27
c 1
b 0
f 0
dl 0
loc 63
rs 10
wmc 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 1
A homeAction() 0 9 3
A editAction() 0 32 6
1
<?php
2
/* For licensing terms, see /license.txt */
3
4
namespace Chamilo\CoreBundle\Controller;
5
6
use Chamilo\CoreBundle\Repository\IllustrationRepository;
7
use Chamilo\ThemeBundle\Model\UserInterface;
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
15
/**
16
 * Class UserController.
17
 *
18
 * @Route("/account")
19
 *
20
 * @author Julio Montoya <[email protected]>
21
 */
22
class AccountController extends BaseController
23
{
24
    private $userRepository;
25
    private $formFactory;
0 ignored issues
show
introduced by
The private property $formFactory is not used, and could be removed.
Loading history...
26
27
    public function __construct(UserRepository $userRepository)
28
    {
29
        $this->userRepository = $userRepository;
30
    }
31
32
    /**
33
     * @Route("/home", methods={"GET"}, name="chamilo_core_account_home")
34
     *
35
     * @param string $username
36
     */
37
    public function homeAction()
38
    {
39
        $user = $this->getUser();
40
41
        if (!is_object($user) || !$user instanceof UserInterface) {
42
            throw $this->createAccessDeniedException('This user does not have access to this section');
43
        }
44
45
        return $this->render('@ChamiloCore/Account/home.html.twig', ['user' => $user]);
46
    }
47
48
    /**
49
     * @Route("/edit", methods={"GET", "POST"}, name="chamilo_core_account_edit")
50
     *
51
     * @param string $username
52
     */
53
    public function editAction(Request $request, UserManagerInterface $userManager, IllustrationRepository $illustrationRepository)
54
    {
55
        $user = $this->getUser();
56
57
        if (!is_object($user) || !$user instanceof UserInterface) {
58
            throw $this->createAccessDeniedException('This user does not have access to this section');
59
        }
60
61
        $form = $this->createForm(ProfileType::class, $user);
62
        $form->setData($user);
63
64
        $form->handleRequest($request);
65
66
        if ($form->isSubmitted() && $form->isValid()) {
67
            $illustration = $form['illustration']->getData();
68
            if ($illustration) {
69
                $file = $illustrationRepository->addIllustration($resource, $this->getUser(), $illustration);
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $resource seems to be never defined.
Loading history...
70
                $em = $illustrationRepository->getEntityManager();
71
                $em->persist($file);
72
                $em->flush();
73
            }
74
75
            $userManager->updateUser($user);
76
77
            $this->addFlash('success', $this->trans('Updated'));
78
            $url = $this->generateUrl('chamilo_core_account_home', ['username' => $user->getUsername()]);
79
            $response = new RedirectResponse($url);
80
81
            return $response;
82
        }
83
84
        return $this->render('@ChamiloCore/Account/edit.html.twig', ['form' => $form->createView(), 'user' => $user]);
85
    }
86
}
87