UserController   A
last analyzed

Complexity

Total Complexity 9

Size/Duplication

Total Lines 100
Duplicated Lines 31 %

Coupling/Cohesion

Components 1
Dependencies 5

Importance

Changes 0
Metric Value
wmc 9
lcom 1
cbo 5
dl 31
loc 100
rs 10
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A indexAction() 0 9 1
A showAction() 10 10 1
A editAction() 0 20 3
A deleteAction() 13 13 3
A createDeleteForm() 8 8 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 Badger\Bundle\UserBundle\Controller;
4
5
use Badger\Bundle\UserBundle\Entity\User;
6
use Badger\Bundle\UserBundle\Form\UserType;
7
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
8
use Symfony\Component\Form\Form;
9
use Symfony\Component\HttpFoundation\RedirectResponse;
10
use Symfony\Component\HttpFoundation\Request;
11
use Symfony\Component\HttpFoundation\Response;
12
13
/**
14
 * User controller.
15
 *
16
 * @license http://opensource.org/licenses/MIT The MIT License (MIT)
17
 */
18
class UserController extends Controller
19
{
20
    /**
21
     * Lists all User entities.
22
     */
23
    public function indexAction()
24
    {
25
        $users = $this->get('badger.user.repository.user')
26
            ->findAll();
27
28
        return $this->render('@User/user/index.html.twig', [
29
            'users' => $users,
30
        ]);
31
    }
32
33
    /**
34
     * Finds and displays a User entity.
35
     *
36
     * @param string $id
37
     *
38
     * @return Response
39
     */
40 View Code Duplication
    public function showAction($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...
41
    {
42
        $user = $this->get('badger.user.repository.user')->find($id);
43
        $deleteForm = $this->createDeleteForm($user);
44
45
        return $this->render('@User/user/show.html.twig', [
46
            'user' => $user,
47
            'delete_form' => $deleteForm->createView(),
48
        ]);
49
    }
50
51
    /**
52
     * Displays a form to edit an existing User entity.
53
     *
54
     * @param Request $request
55
     * @param string  $id
56
     *
57
     * @return RedirectResponse|Response
58
     */
59
    public function editAction(Request $request, $id)
60
    {
61
        $user = $this->get('badger.user.repository.user')->find($id);
62
        $deleteForm = $this->createDeleteForm($user);
63
        $editForm = $this->createForm('Badger\Bundle\UserBundle\Form\UserType', $user);
64
        $editForm->handleRequest($request);
65
66
        if ($editForm->isSubmitted() && $editForm->isValid()) {
67
            $userSaver = $this->get('badger.user.saver.user');
68
            $userSaver->save($user);
69
70
            return $this->redirectToRoute('admin_user_edit', ['id' => $user->getId()]);
71
        }
72
73
        return $this->render('@User/user/edit.html.twig', [
74
            'user' => $user,
75
            'edit_form' => $editForm->createView(),
76
            'delete_form' => $deleteForm->createView(),
77
        ]);
78
    }
79
80
    /**
81
     * Deletes a User entity.
82
     *
83
     * @param Request $request
84
     * @param string  $id
85
     *
86
     * @return RedirectResponse
87
     */
88 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...
89
    {
90
        $user = $this->get('badger.user.repository.user')->find($id);
91
        $form = $this->createDeleteForm($user);
92
        $form->handleRequest($request);
93
94
        if ($form->isSubmitted() && $form->isValid()) {
95
            $userRemover = $this->get('badger.user.remover.user');
96
            $userRemover->remove($user);
97
        }
98
99
        return $this->redirectToRoute('admin_user_index');
100
    }
101
102
    /**
103
     * Creates a form to delete a User entity.
104
     *
105
     * @param User $user The User entity
106
     *
107
     * @return Form The form
108
     */
109 View Code Duplication
    private function createDeleteForm(User $user)
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...
110
    {
111
        return $this->createFormBuilder()
0 ignored issues
show
Bug introduced by
The method getForm() does not exist on Symfony\Component\Form\FormConfigBuilder. Did you maybe mean getFormConfig()?

This check marks calls to methods that do not seem to exist on an object.

This is most likely the result of a method being renamed without all references to it being renamed likewise.

Loading history...
112
            ->setAction($this->generateUrl('admin_user_delete', ['id' => $user->getId()]))
113
            ->setMethod('DELETE')
114
            ->getForm()
115
        ;
116
    }
117
}
118