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

UserController   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 108
Duplicated Lines 29.63 %

Coupling/Cohesion

Components 1
Dependencies 7

Importance

Changes 3
Bugs 1 Features 1
Metric Value
wmc 8
c 3
b 1
f 1
lcom 1
cbo 7
dl 32
loc 108
rs 10

4 Methods

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