Completed
Push — master ( 7e6185...8b8003 )
by Nicolas
04:35
created

AbstractSecurityController::profileAction()   B

Complexity

Conditions 4
Paths 3

Size

Total Lines 32
Code Lines 19

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 32
rs 8.5806
c 0
b 0
f 0
cc 4
eloc 19
nc 3
nop 1
1
<?php
2
3
namespace Smart\AuthenticationBundle\Controller;
4
5
use Smart\AuthenticationBundle\Security\Form\Type\UserProfileType;
6
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
7
use Symfony\Component\HttpFoundation\Request;
8
use Symfony\Component\HttpFoundation\Response;
9
use Symfony\Component\Security\Http\Authentication\AuthenticationUtils;
10
11
/**
12
 * @author Nicolas Bastien <[email protected]>
13
 */
14
class AbstractSecurityController extends Controller
15
{
16
    /**
17
     * Define application context, override this in your controller
18
     * @var string
19
     */
20
    protected $context;
21
22
    /**
23
     * @param string      $id         The message id (may also be an object that can be cast to string)
24
     * @param array       $parameters An array of parameters for the message
25
     * @param string|null $domain     The domain for the message or null to use the default
26
     *
27
     * @return string
28
     */
29
    protected function translate($id, array $parameters = array(), $domain = null)
30
    {
31
        return $this->get('translator')->trans($id, $parameters, $domain);
32
    }
33
    
34
    /**
35
     * @return Response
36
     */
37
    public function loginAction()
38
    {
39
        $helper = $this->getAuthenticationUtils();
40
41
        return $this->render($this->context . '/security/login.html.twig', [
42
            'last_username' => $helper->getLastUsername(),
43
            'error'         => $helper->getLastAuthenticationError(),
44
            'layout_template' => $this->context . '/empty_layout.html.twig',
45
            'security_login_check_url' => $this->generateUrl($this->context . '_security_login_check'),
46
//            'security_forgot_password_url' => $this->generateUrl($this->context . '_security_forgot_password'),
0 ignored issues
show
Unused Code Comprehensibility introduced by
56% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
47
        ]);
48
    }
49
50
    /**
51
     * @return AuthenticationUtils
52
     */
53
    private function getAuthenticationUtils()
54
    {
55
        return $this->get('security.authentication_utils');
56
    }
57
58
    /**
59
     * @param Request $request
60
     *
61
     * @return Response
62
     */
63
    public function profileAction(Request $request)
64
    {
65
        $user = $this->getUser();
66
67
        $form = $this->createForm(UserProfileType::class, $user, []);
68
69
        $form->handleRequest($request);
70
71
        if (!$form->isSubmitted() || !$form->isValid()) {
72
            return $this->render($this->context . '/security/profile.html.twig', [
73
                'base_template' => $this->get('sonata.admin.pool')->getTemplate('layout'),
74
                'admin_pool'    => $this->get('sonata.admin.pool'),
75
                'form'          => $form->createView(),
76
                'security_profile_url' => $this->generateUrl('admin_security_profile'),
77
            ]);
78
        }
79
80
        if (null !== $user->getPlainPassword()) {
81
            $encoder = $this->get('security.password_encoder');
82
            $user->setPassword(
83
                $encoder->encodePassword($user, $user->getPlainPassword())
84
            );
85
        }
86
87
        $manager = $this->getDoctrine()->getManager();
88
        $manager->persist($user);
0 ignored issues
show
Bug introduced by
It seems like $user defined by $this->getUser() on line 65 can also be of type null; however, Doctrine\Common\Persiste...bjectManager::persist() does only seem to accept object, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
89
        $manager->flush();
90
91
        $this->addFlash('success', $this->translate('profile_edit.processed', [], 'security'));
92
93
        return $this->redirectToRoute('sonata_admin_dashboard');
94
    }
95
}
96