Completed
Push — master ( eaf36c...f92548 )
by Marcel
03:19
created

ProfileController::index()   B

Complexity

Conditions 10
Paths 14

Size

Total Lines 45
Code Lines 26

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 24
CRAP Score 10.1371

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 10
eloc 26
c 1
b 0
f 0
nc 14
nop 5
dl 0
loc 45
ccs 24
cts 27
cp 0.8889
crap 10.1371
rs 7.6666

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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\ProfileType;
9
use App\Security\EmailConfirmation\ConfirmationManager;
10
use App\Service\AttributePersister;
11
use Doctrine\ORM\EntityManagerInterface;
12
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
13
use Symfony\Component\HttpFoundation\Request;
14
use Symfony\Component\Routing\Annotation\Route;
15
use Symfony\Component\Security\Core\Encoder\UserPasswordEncoderInterface;
16
17
/**
18
 * @Route("/profile")
19
 */
20
class ProfileController extends AbstractController {
21
22
    use AttributeDataTrait;
23
24
    /**
25
     * @Route("", name="profile")
26
     */
27 1
    public function index(Request $request, UserPasswordEncoderInterface $passwordEncoder, AttributePersister $attributePersister,
28
                          EntityManagerInterface $em, ConfirmationManager $confirmationManager) {
29
        /** @var User $user */
30 1
        $user = $this->getUser();
31
32 1
        $form = $this->createForm(ProfileType::class, $user);
33 1
        $form->handleRequest($request);
34
35 1
        if($form->isSubmitted() && $form->isValid()) {
36 1
            if($form->has('group_password')) {
37 1
                $password = $form->get('group_password')->get('password')->getData();
38
39 1
                if (!empty($password) && !$user instanceof ActiveDirectoryUser) {
40 1
                    $user->setPassword($passwordEncoder->encodePassword($user, $password));
41 1
                    $user->setMustChangePassword(false);
42
                }
43
            }
44
45 1
            if($form->has('group_email')) {
46 1
                $email = $form->get('group_email')->get('email')->getData();
47
48 1
                if(empty($email)) {
49
                    $user->setEmail(null);
50 1
                } else if($user->getEmail() !== $email) {
51
                    $confirmationManager->newConfirmation($user, $email);
52
                }
53
            }
54
55 1
            $em->persist($user);
56 1
            $em->flush();
57
58 1
            $attributeData = $this->getAttributeData($form);
59 1
            $attributePersister->persistUserAttributes($attributeData, $user);
60
61 1
            $this->addFlash('success', 'profile.success');
62 1
            return $this->redirectToRoute('profile');
63
        }
64
65 1
        if($confirmationManager->hasConfirmation($user)) {
66
            $this->addFlash('success', 'email_confirmation.sent');
67
        }
68
69 1
        return $this->render('profile/index.html.twig', [
70 1
            'form' => $form->createView(),
71 1
            'user' => $user
72
        ]);
73
    }
74
}