ProfileController   A
last analyzed

Complexity

Total Complexity 10

Size/Duplication

Total Lines 68
Duplicated Lines 0 %

Test Coverage

Coverage 35.29%

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 41
c 2
b 0
f 0
dl 0
loc 68
ccs 12
cts 34
cp 0.3529
rs 10
wmc 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
B index() 0 34 6
A changePassword() 0 26 4
1
<?php
2
3
namespace App\Controller;
4
5
use App\Entity\ActiveDirectoryUser;
6
use App\Entity\User;
7
use App\Form\AttributeDataTrait;
8
use App\Form\PasswordChangeType;
9
use App\Form\ProfileType;
10
use App\Security\EmailConfirmation\ConfirmationManager;
11
use App\Security\Voter\ProfileVoter;
12
use App\Service\AttributePersister;
13
use Doctrine\ORM\EntityManagerInterface;
14
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
15
use Symfony\Component\HttpFoundation\Request;
16
use Symfony\Component\HttpFoundation\Response;
17
use Symfony\Component\PasswordHasher\Hasher\UserPasswordHasherInterface;
18
use Symfony\Component\Routing\Annotation\Route;
19
20
#[Route(path: '/profile')]
21
class ProfileController extends AbstractController {
22
23
    use AttributeDataTrait;
24
25
    #[Route(path: '', name: 'profile')]
26
    public function index(Request $request, AttributePersister $attributePersister, EntityManagerInterface $em, ConfirmationManager $confirmationManager): Response {
27
        /** @var User $user */
28
        $user = $this->getUser();
29
30
        $form = $this->createForm(ProfileType::class, $user);
31
        $form->handleRequest($request);
32
33
        if($form->isSubmitted() && $form->isValid()) {
34
            $email = $form->get('email')->getData();
35
36
            if(empty($email)) {
37
                $user->setEmail(null);
38
            } else if($user->getEmail() !== $email) {
39
                $confirmationManager->newConfirmation($user, $email);
40
            }
41
42
            $em->persist($user);
43
            $em->flush();
44
45
            $attributeData = $this->getAttributeData($form);
46
            $attributePersister->persistUserAttributes($attributeData, $user);
47
48
            $this->addFlash('success', 'profile.success');
49
            return $this->redirectToRoute('profile');
50
        }
51
52
        if($confirmationManager->hasConfirmation($user)) {
53
            $this->addFlash('success', 'email_confirmation.pending');
54
        }
55
56
        return $this->render('profile/index.html.twig', [
57
            'form' => $form->createView(),
58
            'user' => $user
59
        ]);
60
    }
61
62
    #[Route(path: '/password', name: 'profile_password')]
63
    public function changePassword(Request $request, EntityManagerInterface $em, UserPasswordHasherInterface $passwordHasher): Response {
64
        $this->denyAccessUnlessGranted(ProfileVoter::CHANGE_PASSWORD);
65
66
        /** @var User $user */
67
        $user = $this->getUser();
68
69
        $form = $this->createForm(PasswordChangeType::class);
70 2
        $form->handleRequest($request);
71 2
72
        if($form->isSubmitted() && $form->isValid() && !$user instanceof ActiveDirectoryUser) {
73
            $password = $form->get('newPassword')->getData();
74 2
            $user->setPassword($passwordHasher->hashPassword($user, $password));
75
            $user->setMustChangePassword(false);
76 2
77 2
            $em->persist($user);
78
            $em->flush();
79 2
80 1
            $this->addFlash('success', 'profile.change_password.success');
81 1
            return $this->redirectToRoute('profile_password');
82 1
        }
83
84 1
        return $this->render('profile/change_password.html.twig', [
85 1
            'form' => $form->createView(),
86
            'user' => $user,
87 1
            'can_change_password' => !$user instanceof ActiveDirectoryUser
88 1
        ]);
89
    }
90
}