Completed
Pull Request — dev (#22)
by
unknown
12:19
created

UserController   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 120
Duplicated Lines 43.33 %

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 120
rs 10

4 Methods

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