Completed
Pull Request — dev (#22)
by
unknown
03:24
created

UserController   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 115
Duplicated Lines 45.22 %

Coupling/Cohesion

Components 1
Dependencies 8

Importance

Changes 3
Bugs 1 Features 1
Metric Value
wmc 8
c 3
b 1
f 1
lcom 1
cbo 8
dl 52
loc 115
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
B usersListAction() 0 26 2
A activationAction() 16 16 2
A userAddAction() 18 18 2
A userEditAction() 18 18 2

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 AppBundle\Controller;
4
5
use AppBundle\Entity\User;
6
use AppBundle\Entity\Filter;
7
use AppBundle\Form\FilterType;
8
use AppBundle\Form\User\EditType;
9
use AppBundle\Form\User\RegistrationType;
10
use AppBundle\Form\User\ActivationType;
11
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Method;
12
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
13
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;
14
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
15
use Symfony\Component\HttpFoundation\RedirectResponse;
16
use Symfony\Component\HttpFoundation\Request;
17
18
class UserController extends Controller
19
{
20
    /**
21
     * @Route("/users", name="users_list")
22
     * @Template("@App/users.html.twig")
23
     *
24
     * @param Request $request
25
     * @return array
26
     */
27
    public function usersListAction(Request $request)
28
    {
29
        $em = $this->getDoctrine()->getManager();
30
        $filter = $this->createForm(FilterType::class, new Filter);
31
        $paginator = $this->get('knp_paginator');
32
        $users = $paginator->paginate(
33
            $em->getRepository(User::class)->selectUsersByParams($request->get('name')),
34
            $request->query->getInt('page', 1),
35
            10
36
        );
37
        $activationForm = [];
38
        foreach ($users as $user) {
39
            $activationForm[$user->getId()] = $this->createForm(ActivationType::class, $user, [
40
                'method' => "PUT",
41
                'action' => $this->generateUrl('activate_user', array('id' => $user->getId())),
42
                'validation_groups' => array('edit'),
43
            ])
44
                ->createView();
45
        }
46
47
        return [
48
            "users" => $users,
49
            "filter" => $filter->createView(),
50
            "activationForm" => $activationForm
51
        ];
52
    }
53
54
    /**
55
     * @Route("/user/activate/{id}", name="activate_user")
56
     * @Template("@App/add.html.twig")
57
     *
58
     * @Method("PUT")
59
     * @param  Request $request
60
     * @param  User $user
61
     * @return RedirectResponse
62
     */
63 View Code Duplication
    public function activationAction(Request $request, 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...
64
    {
65
        $em = $this->getDoctrine()->getManager();
66
        $form = $this->createForm(ActivationType::class, $user, [
67
            'method' => "PUT",
68
            'action' => $this->generateUrl('activate_user', array('id' => $user->getId())),
69
            'validation_groups' => array('edit'),
70
        ]);
71
        $form->handleRequest($request);
72
        if ($form->isValid()) {
73
            $em->persist($user);
74
            $em->flush();
75
        }
76
77
        return $this->redirect($this->generateUrl("users_list"));
78
    }
79
80
    /**
81
     * @Route("/user/add", name="add_user")
82
     * @Template("@App/add.html.twig")
83
     *
84
     * @param Request $request
85
     * @return array|RedirectResponse
86
     */
87 View Code Duplication
    public function userAddAction(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...
88
    {
89
        $em = $this->getDoctrine()->getManager();
90
        $user = new User();
91
        $form = $this->createForm(RegistrationType::class, $user, [
92
            'action' => $this->generateUrl('add_user'),
93
            'validation_groups' => array('registration'),
94
        ]);
95
        $form->handleRequest($request);
96
        if ($form->isValid()) {
97
            $em->persist($user);
98
            $em->flush();
99
100
            return $this->redirect($this->generateUrl("users_list"));
101
        }
102
103
        return ['form' => $form->createView()];
104
    }
105
106
    /**
107
     * @Route("/user/edit/{id}", name="edit_user")
108
     * @Template("@App/add.html.twig")
109
     *
110
     * @param Request $request
111
     * @param User $user
112
     * @return array|RedirectResponse
113
     */
114 View Code Duplication
    public function userEditAction(Request $request, 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...
115
    {
116
        $em = $this->getDoctrine()->getManager();
117
        $form = $this->createForm(EditType::class, $user, [
118
            'action' => $this->generateUrl('edit_user', array('id' => $user->getId())),
119
            'method' => 'POST',
120
            'validation_groups' => array('edit'),
121
        ]);
122
        $form->handleRequest($request);
123
        if ($form->isValid()) {
124
            $em->persist($user);
125
            $em->flush();
126
127
            return $this->redirect($this->generateUrl("users_list"));
128
        }
129
130
        return ['form' => $form->createView()];
131
    }
132
}
133