Completed
Push — master ( adc6b0...de05ab )
by PastisD
02:13
created

UserController   A

Complexity

Total Complexity 15

Size/Duplication

Total Lines 237
Duplicated Lines 40.93 %

Coupling/Cohesion

Components 1
Dependencies 11

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 15
c 1
b 0
f 0
lcom 1
cbo 11
dl 97
loc 237
rs 10

7 Methods

Rating   Name   Duplication   Size   Complexity  
A indexAction() 12 12 1
B editAction() 0 32 4
B newAction() 0 24 2
B createNewEditForm() 29 29 2
A deleteConfirmAction() 17 17 2
B deleteAction() 24 24 3
A createDeleteForm() 15 15 1

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
3
namespace Xdaysaysay\AdminBundle\Controller;
4
5
use FOS\UserBundle\Model\UserInterface;
6
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
7
use Symfony\Component\Form\Extension\Core\Type\SubmitType;
8
use Symfony\Component\HttpFoundation\Request;
9
use Xdaysaysay\AdminBundle\Form\Type\UserType;
10
use Xdaysaysay\UserBundle\Entity\User;
11
12
class UserController extends Controller
13
{
14
    /**
15
     * Displays list of existing User entities.
16
     *
17
     * @return \Symfony\Component\HttpFoundation\Response
18
     *
19
     * @throws \LogicException
20
     * @throws \UnexpectedValueException
21
     */
22 View Code Duplication
    public function indexAction()
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...
23
    {
24
        $this->get('avanzu_admin_theme.theme_manager')->registerScript('datatble', 'bundles/xdaysaysayadmin/js/datatable.js');
25
26
        $users = $this->getDoctrine()->getRepository('XdaysaysayUserBundle:User')->findBy([], ['id' => 'DESC']);
27
28
        return $this->render(
29
            'XdaysaysayAdminBundle:User:index.html.twig', [
30
                'users' => $users,
31
            ]
32
        );
33
    }
34
35
    /**
36
     * Displays a form to edit an existing User entity.
37
     *
38
     * @param Request $request
39
     * @param int $id
40
     *
41
     * @return \Symfony\Component\HttpFoundation\Response
42
     *
43
     * @throws \Symfony\Component\HttpKernel\Exception\NotFoundHttpException
44
     * @throws \LogicException
45
     * @throws \InvalidArgumentException
46
     * @throws \Symfony\Component\Form\Exception\AlreadySubmittedException
47
     * @throws \Symfony\Component\Form\Exception\LogicException
48
     * @throws \Symfony\Component\Form\Exception\UnexpectedTypeException
49
     * @throws \OutOfBoundsException
50
     */
51
    public function editAction(Request $request, $id)
52
    {
53
        $em = $this->getDoctrine()->getManager();
54
55
        $entity = $em->getRepository('XdaysaysayUserBundle:User')->find($id);
56
57
        if (!$entity) {
58
            throw $this->createNotFoundException('Unable to find User entity.');
59
        }
60
61
        $form = $this->createNewEditForm($entity);
62
63
        $form->handleRequest($request);
64
65
        if ($form->isValid()) {
66
            if ($form->get('password')->getData() !== '') {
67
                $entity->setPlainPassword($form->get('password')->getData());
68
            }
69
            $this->get('fos_user.user_manager')->updateUser($entity);
70
71
            $this->addFlash('success', $this->get('translator')->trans('admin.user.flash.edit', [], 'admin'));
72
73
            return $this->redirect($this->generateUrl('xdaysaysay_admin_user_edit', ['id' => $entity->getId()]));
74
        }
75
76
        return $this->render(
77
            '@XdaysaysayAdmin/User/edit.html.twig', [
78
                'entity' => $entity,
79
                'form'   => $form->createView(),
80
            ]
81
        );
82
    }
83
84
    /**
85
     * Displays a form to edit an existing User entity.
86
     *
87
     * @param Request $request
88
     *
89
     * @return \Symfony\Component\HttpFoundation\Response
90
     *
91
     * @throws \Symfony\Component\HttpKernel\Exception\NotFoundHttpException
92
     * @throws \LogicException
93
     * @throws \InvalidArgumentException
94
     * @throws \Symfony\Component\Form\Exception\AlreadySubmittedException
95
     * @throws \Symfony\Component\Form\Exception\LogicException
96
     * @throws \Symfony\Component\Form\Exception\UnexpectedTypeException
97
     * @throws \OutOfBoundsException
98
     */
99
    public function newAction(Request $request)
100
    {
101
        $entity = $this->get('fos_user.user_manager')->createUser();
102
103
        $form = $this->createNewEditForm($entity, true);
104
105
        $form->handleRequest($request);
106
107
        if ($form->isValid()) {
108
            $entity->setPlainPassword($form->get('password')->getData());
109
            $this->get('fos_user.user_manager')->updateUser($entity);
110
111
            $this->addFlash('success', $this->get('translator')->trans('admin.user.flash.create', [], 'admin'));
112
113
            return $this->redirect($this->generateUrl('xdaysaysay_admin_user_edit', ['id' => $entity->getId()]));
114
        }
115
116
        return $this->render(
117
            '@XdaysaysayAdmin/User/new.html.twig', [
118
                'entity' => $entity,
119
                'form'   => $form->createView(),
120
            ]
121
        );
122
    }
123
124
125
    /**
126
     * Creates a form to create a User entity.
127
     *
128
     * @param UserInterface $entity The entity
129
     * @param boolean $newUser      Define if the for is for a new user entity
130
     *
131
     * @return \Symfony\Component\Form\Form The form
132
     *
133
     * @throws \InvalidArgumentException
134
     * @throws \Symfony\Component\Form\Exception\AlreadySubmittedException
135
     * @throws \Symfony\Component\Form\Exception\LogicException
136
     * @throws \Symfony\Component\Form\Exception\UnexpectedTypeException
137
     */
138 View Code Duplication
    private function createNewEditForm(UserInterface $entity, $newUser = false)
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...
139
    {
140
        if ($entity->getId() !== null) {
141
            $action = $this->generateUrl('xdaysaysay_admin_user_edit', ['id' => $entity->getId()]);
142
            $submitText = $this->get('translator')->trans('admin.common.form.update', [], 'admin');
143
        } else {
144
            $action = $this->generateUrl('xdaysaysay_admin_user_new');
145
            $submitText = $this->get('translator')->trans('admin.common.form.create', [], 'admin');
146
        }
147
148
        $form = $this->createForm(
149
            UserType::class, $entity, [
150
                'action'   => $action,
151
                'method'   => 'PUT',
152
                'new_user' => $newUser,
153
            ]
154
        );
155
156
        $form->add(
157
            'submit', SubmitType::class, [
158
                'label' => $submitText,
159
                'attr'  => [
160
                    'class' => 'btn btn-primary',
161
                ],
162
            ]
163
        );
164
165
        return $form;
166
    }
167
168
    /**
169
     * @param $id
170
     *
171
     * @return \Symfony\Component\HttpFoundation\Response
172
     */
173 View Code Duplication
    public function deleteConfirmAction($id)
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...
174
    {
175
        $em = $this->getDoctrine()->getManager();
176
177
        $entity = $em->getRepository('XdaysaysayUserBundle:User')->find($id);
178
179
        if (!$entity) {
180
            throw $this->createNotFoundException('Unable to find User entity.');
181
        }
182
183
        return $this->render(
184
            '@XdaysaysayAdmin/User/confirm_delete.html.twig', [
185
                'entity' => $entity,
186
                'form'   => $this->createDeleteForm($entity)->createView(),
187
            ]
188
        );
189
    }
190
191
    /**
192
     * @param Request $request
193
     * @param $id
194
     *
195
     * @return \Symfony\Component\HttpFoundation\RedirectResponse
196
     *
197
     * @throws \LogicException
198
     * @throws \InvalidArgumentException
199
     * @throws \Symfony\Component\HttpKernel\Exception\NotFoundHttpException
200
     */
201 View Code Duplication
    public function deleteAction(Request $request, $id)
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...
202
    {
203
        $em = $this->getDoctrine()->getManager();
204
205
        $entity = $em->getRepository('XdaysaysayUserBundle:User')->find($id);
206
207
        if (!$entity) {
208
            throw $this->createNotFoundException('Unable to find User entity.');
209
        }
210
211
        $form = $this->createDeleteForm($entity);
212
213
        $form->handleRequest($request);
214
        if ($form->isValid()) {
215
            $em->remove($entity);
216
            $em->flush();
217
218
            $this->addFlash('success', $this->get('translator')->trans('admin.user.flash.delete', [], 'admin'));
219
220
            return $this->redirectToRoute('xdaysaysay_admin_user_index');
221
        } else {
222
            return $this->redirectToRoute('xdaysaysay_admin_user_delete_confirm', ['id' => $id]);
223
        }
224
    }
225
226
    /**
227
     * @param User $entity
228
     *
229
     * @return \Symfony\Component\Form\Form
230
     *
231
     * @throws \InvalidArgumentException
232
     */
233 View Code Duplication
    private function createDeleteForm(User $entity)
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...
234
    {
235
        $form = $this->createFormBuilder()
236
            ->setAction($this->generateUrl('xdaysaysay_admin_user_delete', ['id' => $entity->getId()]))
237
            ->setMethod('DELETE')
238
            ->add('submit', SubmitType::class, [
239
                'label' => $this->get('translator')->trans('admin.common.action.delete', [], 'admin'),
240
                'attr'  => [
241
                    'class' => 'btn btn-danger',
242
                ],
243
            ])
244
            ->getForm();
245
246
        return $form;
247
    }
248
}