GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.
Completed
Push — master ( f80a23...a34f50 )
by Luis Ramón
03:18
created

AdminUserController::formAction()   C

Complexity

Conditions 9
Paths 32

Size

Total Lines 60
Code Lines 35

Duplication

Lines 8
Ratio 13.33 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 8
loc 60
rs 6.8358
cc 9
eloc 35
nc 32
nop 2

How to fix   Long Method   

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
  ÁTICA - Aplicación web para la gestión documental de centros educativos
4
5
  Copyright (C) 2015-2016: Luis Ramón López López
6
7
  This program is free software: you can redistribute it and/or modify
8
  it under the terms of the GNU Affero General Public License as published by
9
  the Free Software Foundation, either version 3 of the License, or
10
  (at your option) any later version.
11
12
  This program is distributed in the hope that it will be useful,
13
  but WITHOUT ANY WARRANTY; without even the implied warranty of
14
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15
  GNU Affero General Public License for more details.
16
17
  You should have received a copy of the GNU Affero General Public License
18
  along with this program.  If not, see [http://www.gnu.org/licenses/].
19
*/
20
21
namespace AppBundle\Controller;
22
23
use AppBundle\Entity\User;
24
use AppBundle\Form\Type\UserType;
25
use Doctrine\ORM\EntityManager;
26
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
27
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Security;
28
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
29
use Symfony\Component\Form\SubmitButton;
30
use Symfony\Component\HttpFoundation\RedirectResponse;
31
use Symfony\Component\HttpFoundation\Request;
32
33
/**
34
 * @Route("/admin/usuarios")
35
 * @Security("is_granted('ROLE_ADMIN')")
36
 */
37
class AdminUserController extends Controller
38
{
39
    /**
40
     * @Route("/", name="admin_users", methods={"GET"})
41
     */
42
    public function usersIndexAction(Request $request)
43
    {
44
        /** @var EntityManager $em */
45
        $em = $this->getDoctrine()->getManager();
46
47
        $usersQuery = $em->createQuery('SELECT u FROM AppBundle:User u JOIN AppBundle:Person p WITH u.person = p');
48
49
        $paginator  = $this->get('knp_paginator');
50
        $pagination = $paginator->paginate(
51
            $usersQuery,
52
            $request->query->getInt('page', 1),
53
            $this->getParameter('page.size'),
54
            [
55
                'defaultSortFieldName' => 'p.lastName',
56
                'defaultSortDirection' => 'asc'
57
            ]
58
        );
59
60
        return $this->render('admin/manage_users.html.twig',
61
            [
62
                'breadcrumb' => [
63
                    ['caption' => 'menu.manage', 'icon' => 'wrench', 'path' => 'admin_menu'],
64
                    ['caption' => 'menu.admin.manage.users', 'icon' => 'users']
65
                ],
66
                'title' => null,
67
                'pagination' => $pagination
68
            ]);
69
    }
70
71
    /**
72
     * @Route("/nuevo", name="admin_user_new", methods={"GET", "POST"})
73
     * @Route("/{user}", name="admin_user_form", methods={"GET", "POST"}, requirements={"profile": "\d+"})
74
     */
75
    public function formAction(User $user = null, Request $request)
76
    {
77
        $em = $this->getDoctrine()->getManager();
78
79
        $new = (null === $user);
80
        if ($new) {
81
            $user = $em->getRepository('AppBundle:User')->createNewUser();
82
        }
83
84
        $me = ($user->getId() === $this->getUser()->getId());
85
86
        $form = $this->createForm(UserType::class, $user, [
87
            'admin' => $this->isGranted('ROLE_ADMIN'),
88
            'me' => $me,
89
            'new' => $new
90
        ]);
91
92
93
        $form->handleRequest($request);
94
95
        if ($form->isSubmitted() && $form->isValid()) {
96
97
            // Guardar el usuario en la base de datos
98
99
            // Si es solicitado, cambiar la contraseña
100
            $passwordSubmit = $form->get('changePassword');
101 View Code Duplication
            if (($passwordSubmit instanceof SubmitButton) && $passwordSubmit->isClicked()) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
102
                $password = $this->container->get('security.password_encoder')
103
                    ->encodePassword($user, $form->get('newPassword')->get('first')->getData());
104
                $user->setPassword($password);
105
                $message = $this->get('translator')->trans('alert.password_changed', [], 'user');
106
            } else {
107
                $message = $this->get('translator')->trans('alert.saved', [], 'user');
108
            }
109
110
            // Probar a guardar los cambios
111
            try {
112
                $em->flush();
113
                $this->addFlash('success', $message);
114
                return new RedirectResponse(
115
                    $this->generateUrl($this->isGranted('ROLE_ADMIN') ? 'admin_users' : 'frontpage')
116
                );
117
            }
118
            catch (\Exception $e) {
119
                $this->addFlash('error', $this->get('translator')->trans('alert.not_saved', [], 'user'));
120
            }
121
        }
122
123
        $titulo = ((string) $user) ?: $this->get('translator')->trans('user.new', [], 'admin');
124
125
        return $this->render('admin/form_user.html.twig', [
126
            'form' => $form->createView(),
127
            'breadcrumb' => [
128
                ['caption' => 'menu.manage', 'icon' => 'wrench', 'path' => 'admin_menu'],
129
                ['caption' => 'menu.admin.manage.users', 'icon' => 'users', 'path' => 'admin_users'],
130
                ['fixed' => $titulo]
131
            ],
132
            'title' => $titulo
133
        ]);
134
    }
135
136
}
137