UserProfileController::index()   A
last analyzed

Complexity

Conditions 5
Paths 4

Size

Total Lines 34
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 17
dl 0
loc 34
rs 9.3888
c 0
b 0
f 0
cc 5
nc 4
nop 1
1
<?php
2
3
namespace App\Controller\Profile;
4
5
use App\Entity\User;
6
use App\Event\User\UserChangepasswordEvent;
7
use App\Event\User\UserUpdateAccountEvent;
8
use App\Form\UserChangePasswordFormType;
9
use App\Form\UserProfileFormType;
10
use Sensio\Bundle\FrameworkExtraBundle\Configuration\IsGranted;
11
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
12
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
13
use Symfony\Component\HttpFoundation\Request;
14
use Symfony\Component\Routing\Annotation\Route;
15
16
/**
17
 * Class UserProfileController
18
 * @package App\Controller\Profile
19
 * @IsGranted("ROLE_USER")
20
 */
21
class UserProfileController extends AbstractController
22
{
23
24
    /**
25
     * @var EventDispatcherInterface
26
     */
27
    private $dispatcher;
28
29
    public function __construct(EventDispatcherInterface $dispatcher)
30
    {
31
32
        $this->dispatcher = $dispatcher;
33
    }
34
35
    /**
36
     * @Route("/profile", name="admin.profile")
37
     */
38
    public function index(Request $request)
39
    {
40
        //Force login, we do not allow the remember me cookie for profile admin.
41
        $this->denyAccessUnlessGranted('IS_AUTHENTICATED_FULLY');
42
43
        /** @var User $user */
44
        $user = $this->getUser();
45
46
        $form = $this->createForm(UserProfileFormType::class, $user);
47
48
        $form->handleRequest($request);
49
50
        if ($form->isSubmitted() && $form->isValid()) {
51
52
            $event = new UserUpdateAccountEvent($user);
53
            $this->dispatcher->dispatch(UserUpdateAccountEvent::NAME, $event);
54
        }
55
56
        $formPassword = $this->createForm(UserChangePasswordFormType::class, $user);
57
58
        $formPassword->handleRequest($request);
59
        if ($formPassword->isSubmitted() && $formPassword->isValid()) {
60
61
            $password = $formPassword->get('plainPassword')->getData();
62
63
            $event = new UserChangepasswordEvent($user, $password);
64
            $this->dispatcher->dispatch(UserChangepasswordEvent::NAME, $event);
65
        }
66
67
68
        return $this->render('user/profile.html.twig', [
69
            'form' => $form->createView(),
70
            'formPassword' => $formPassword->createView(),
71
            'user' => $user,
72
        ]);
73
74
    }
75
}