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.

AdminUserController::formAction()   B
last analyzed

Complexity

Conditions 8
Paths 32

Size

Total Lines 58

Duplication

Lines 8
Ratio 13.79 %

Importance

Changes 0
Metric Value
dl 8
loc 58
rs 7.6719
c 0
b 0
f 0
cc 8
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 Doctrine\ORM\EntityManager;
25
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
26
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Security;
27
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
28
use Symfony\Component\Form\SubmitButton;
29
use Symfony\Component\HttpFoundation\Request;
30
31
/**
32
 * @Route("/admin/usuarios")
33
 * @Security("is_granted('ROLE_ADMIN')")
34
 */
35
class AdminUserController extends Controller
36
{
37
    /**
38
     * @Route("/", name="admin_users", methods={"GET"})
39
     */
40 View Code Duplication
    public function usersIndexAction(Request $request)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in 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...
41
    {
42
        /** @var EntityManager $em */
43
        $em = $this->getDoctrine()->getManager();
44
45
        $usersQuery = $em->createQuery('SELECT u FROM AppBundle:User u');
46
47
        $paginator  = $this->get('knp_paginator');
48
        $pagination = $paginator->paginate(
49
            $usersQuery,
50
            $request->query->getInt('page', 1),
51
            $this->getParameter('page.size'),
52
            [
53
                'defaultSortFieldName' => 'u.lastName',
54
                'defaultSortDirection' => 'asc'
55
            ]
56
        );
57
58
        return $this->render('admin/manage_users.html.twig',
59
            [
60
                'menu_item' => $this->get('app.menu_builders_chain')->getMenuItemByRouteName('admin_users'),
61
                'title' => null,
62
                'pagination' => $pagination
63
            ]);
64
    }
65
66
    /**
67
     * @Route("/nuevo", name="admin_user_new", methods={"GET", "POST"})
68
     * @Route("/{user}", name="admin_user_form", methods={"GET", "POST"}, requirements={"user": "\d+"})
69
     */
70
    public function formAction(User $user = null, Request $request)
71
    {
72
        $em = $this->getDoctrine()->getManager();
73
74
        $new = (null === $user);
75
        if ($new) {
76
            $user = $em->getRepository('AppBundle:User')->createNewUser();
77
            $user->setEnabled(true);
78
        }
79
80
        $me = ($user->getId() === $this->getUser()->getId());
81
82
        $form = $this->createForm('AppBundle\Form\Type\UserType', $user, [
83
            'admin' => $this->isGranted('ROLE_ADMIN'),
84
            'me' => $me,
85
            'new' => $new
86
        ]);
87
88
89
        $form->handleRequest($request);
90
91
        if ($form->isSubmitted() && $form->isValid()) {
92
93
            // Guardar el usuario en la base de datos
94
95
            // Si es solicitado, cambiar la contraseña
96
            $passwordSubmit = $form->get('changePassword');
97 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...
98
                $password = $this->container->get('security.password_encoder')
99
                    ->encodePassword($user, $form->get('newPassword')->get('first')->getData());
100
                $user->setPassword($password);
101
                $message = $this->get('translator')->trans('alert.password_changed', [], 'user');
102
            } else {
103
                $message = $this->get('translator')->trans('alert.saved', [], 'user');
104
            }
105
106
            // Probar a guardar los cambios
107
            try {
108
                $em->flush();
109
                $this->addFlash('success', $message);
110
                return $this->redirectToRoute('admin_users');
111
            } catch (\Exception $e) {
112
                $this->addFlash('error', $this->get('translator')->trans('alert.not_saved', [], 'user'));
113
            }
114
        }
115
116
        $titulo = ((string) $user) ?: $this->get('translator')->trans('user.new', [], 'user');
117
118
        return $this->render('admin/form_user.html.twig', [
119
            'form' => $form->createView(),
120
            'menu_item' => $this->get('app.menu_builders_chain')->getMenuItemByRouteName('admin_users'),
121
            'breadcrumb' => [
122
                ['fixed' => $titulo]
123
            ],
124
            'title' => $titulo,
125
            'user' => $user
126
        ]);
127
    }
128
129
    /**
130
     * @Route("/eliminar/{id}", name="admin_user_delete", methods={"GET", "POST"}, requirements={"user": "\d+"})
131
     */
132
    public function genericDeleteAction(User $element, Request $request)
133
    {
134
        if ('POST' === $request->getMethod() && $request->request->has('delete')) {
135
136
            // Eliminar el departamento de la base de datos
137
            $this->getDoctrine()->getManager()->remove($element);
138
            try {
139
                $this->getDoctrine()->getManager()->flush();
140
                $this->addFlash('success', $this->get('translator')->trans('alert.deleted', [], 'user'));
141
            } catch (\Exception $e) {
142
                $this->addFlash('error', $this->get('translator')->trans('alert.not_deleted', [], 'user'));
143
            }
144
            return $this->redirectToRoute('admin_users');
145
        }
146
147
        $title = (string) $element;
148
149
        $breadcrumb = [
150
            ['fixed' => $title],
151
            ['caption' => 'menu.delete']
152
        ];
153
154
        return $this->render('admin/delete_user.html.twig', [
155
            'menu_item' => $this->get('app.menu_builders_chain')->getMenuItemByRouteName('admin_users'),
156
            'breadcrumb' => $breadcrumb,
157
            'title' => $title,
158
            'element' => $element,
159
        ]);
160
    }
161
}
162