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

UserController   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 119
Duplicated Lines 43.7 %

Coupling/Cohesion

Components 1
Dependencies 8

Test Coverage

Coverage 100%

Importance

Changes 3
Bugs 1 Features 1
Metric Value
wmc 8
c 3
b 1
f 1
lcom 1
cbo 8
dl 52
loc 119
ccs 20
cts 20
cp 1
rs 10

2 Methods

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