Test Failed
Branch master (e769ca)
by Stone
05:52
created

UserProfileController   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 64
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 30
dl 0
loc 64
rs 10
c 0
b 0
f 0
wmc 6

2 Methods

Rating   Name   Duplication   Size   Complexity  
A index() 0 47 5
A __construct() 0 4 1
1
<?php
2
3
namespace App\Controller\Profile;
4
5
use App\Event\User\UserChangepasswordEvent;
6
use App\Event\User\UserUpdateAccountEvent;
7
use App\Form\UserChangePasswordFormType;
8
use App\Form\UserProfileFormType;
9
use Sensio\Bundle\FrameworkExtraBundle\Configuration\IsGranted;
10
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
11
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
12
use Symfony\Component\Form\Extension\Core\Type\SubmitType;
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 \App\Entity\User $user */
44
        $user = $this->getUser();
45
46
        $form = $this->createForm(UserProfileFormType::class, $user);
47
        $form
48
            ->add('updateProfile', SubmitType::class, [
49
                'label' => 'Update profile',
50
                'attr' => [
51
                    'class' => 'waves-effect waves-light btn right mr-2'
52
                ]
53
            ]);
54
        $form->handleRequest($request);
55
56
        if ($form->isSubmitted() && $form->isValid()) {
57
58
            $event = new UserUpdateAccountEvent($user);
59
            $this->dispatcher->dispatch(UserUpdateAccountEvent::NAME, $event);
60
        }
61
62
        $formPassword = $this->createForm(UserChangePasswordFormType::class, $user);
63
        $formPassword
64
            ->add('updatePassword', SubmitType::class, [
65
                'label' => 'Update Password',
66
                'attr' => [
67
                    'class' => 'waves-effect waves-light btn right mr-2'
68
                ]
69
            ]);
70
71
        $formPassword->handleRequest($request);
72
        if ($formPassword->isSubmitted() && $formPassword->isValid()) {
73
74
            $password = $formPassword->get('plainPassword')->getData();
75
76
            $event = new UserChangepasswordEvent($user, $password);
77
            $this->dispatcher->dispatch(UserChangepasswordEvent::NAME, $event);
78
        }
79
80
81
        return $this->render('user/profile.html.twig', [
82
            'form' => $form->createView(),
83
            'formPassword' => $formPassword->createView(),
84
            'user' => $user,
85
        ]);
86
87
    }
88
}