Passed
Branch feature/backend (79c18d)
by Stone
06:15
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 App\Image\ProfileImage;
0 ignored issues
show
Bug introduced by
The type App\Image\ProfileImage was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
10
use Sensio\Bundle\FrameworkExtraBundle\Configuration\IsGranted;
11
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
12
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
13
use Symfony\Component\Form\Extension\Core\Type\SubmitType;
14
use Symfony\Component\HttpFoundation\Request;
15
use Symfony\Component\Routing\Annotation\Route;
16
17
/**
18
 * Class UserProfileController
19
 * @package App\Controller\Profile
20
 * @IsGranted("ROLE_USER")
21
 */
22
class UserProfileController extends AbstractController
23
{
24
25
    /**
26
     * @var EventDispatcherInterface
27
     */
28
    private $dispatcher;
29
30
    public function __construct(EventDispatcherInterface $dispatcher)
31
    {
32
33
        $this->dispatcher = $dispatcher;
34
    }
35
36
    /**
37
     * @Route("/profile", name="admin.profile")
38
     */
39
    public function index(Request $request)
40
    {
41
        //Force login, we do not allow the remember me cookie for profile admin.
42
        $this->denyAccessUnlessGranted('IS_AUTHENTICATED_FULLY');
43
44
        /** @var \App\Entity\User $user */
45
        $user = $this->getUser();
46
47
        $form = $this->createForm(UserProfileFormType::class, $user);
48
        $form
49
            ->add('updateProfile', SubmitType::class, [
50
                'label' => 'Update profile',
51
                'attr' => [
52
                    'class' => 'waves-effect waves-light btn right mr-2'
53
                ]
54
            ]);
55
        $form->handleRequest($request);
56
57
        if ($form->isSubmitted() && $form->isValid()) {
58
59
            $event = new UserUpdateAccountEvent($user);
60
            $this->dispatcher->dispatch(UserUpdateAccountEvent::NAME, $event);
61
        }
62
63
        $formPassword = $this->createForm(UserChangePasswordFormType::class, $user);
64
        $formPassword
65
            ->add('updatePassword', SubmitType::class, [
66
                'label' => 'Update Password',
67
                'attr' => [
68
                    'class' => 'waves-effect waves-light btn right mr-2'
69
                ]
70
            ]);
71
72
        $formPassword->handleRequest($request);
73
        if ($formPassword->isSubmitted() && $formPassword->isValid()) {
74
75
            $password = $formPassword->get('plainPassword')->getData();
76
77
            $event = new UserChangepasswordEvent($user, $password);
78
            $this->dispatcher->dispatch(UserChangepasswordEvent::NAME, $event);
79
        }
80
81
82
        return $this->render('user/profile.html.twig', [
83
            'form' => $form->createView(),
84
            'formPassword' => $formPassword->createView(),
85
            'user' => $user,
86
        ]);
87
88
    }
89
}