Completed
Push — master ( c1ba47...3b776f )
by Benjamin
10:05 queued 04:46
created

AccountController::editAccount()   A

Complexity

Conditions 4
Paths 3

Size

Total Lines 22
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 15
c 2
b 0
f 0
dl 0
loc 22
rs 9.7666
cc 4
nc 3
nop 2
1
<?php
2
3
namespace Obblm\Core\Controller;
4
5
use Obblm\Core\Form\Coach\EditUserForm;
6
use Obblm\Core\Security\Roles;
7
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
8
use Symfony\Component\HttpFoundation\Request;
9
use Symfony\Component\HttpFoundation\Response;
10
use Symfony\Component\Routing\Annotation\Route;
11
use Symfony\Component\Security\Core\Encoder\UserPasswordEncoderInterface;
12
13
/**
14
 * @Route("/account", name="obblm_account")
15
 */
16
class AccountController extends AbstractController
17
{
18
    /**
19
     * @Route("/", name="")
20
     */
21
    public function editAccount(Request $request, UserPasswordEncoderInterface $passwordEncoder):Response
22
    {
23
        $coach = $this->getUser();
24
        $this->denyAccessUnlessGranted(Roles::COACH, $coach);
25
26
        $form = $this->createForm(EditUserForm::class, $this->getUser());
27
28
        $form->handleRequest($request);
29
        if ($form->isSubmitted() && $form->isValid()) {
30
            $data = $form->getData();
31
            if ($data->getPlainPassword()) {
32
                $password = $passwordEncoder->encodePassword($coach, $coach->getPlainPassword());
0 ignored issues
show
Bug introduced by
It seems like $coach can also be of type null; however, parameter $user of Symfony\Component\Securi...rface::encodePassword() does only seem to accept Symfony\Component\Security\Core\User\UserInterface, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

32
                $password = $passwordEncoder->encodePassword(/** @scrutinizer ignore-type */ $coach, $coach->getPlainPassword());
Loading history...
33
                $coach->setPassword($password)
34
                    ->setHash(hash('sha256', $coach->getEmail()));
35
            }
36
            $entityManager = $this->getDoctrine()->getManager();
37
            $entityManager->persist($coach);
38
            $entityManager->flush();
39
        }
40
41
        return $this->render('@ObblmCore/account/edit.html.twig', [
42
            'form' => $form->createView(),
43
        ]);
44
    }
45
}
46