Completed
Pull Request — master (#30)
by
unknown
04:23
created

UserController   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 157
Duplicated Lines 10.19 %

Coupling/Cohesion

Components 1
Dependencies 6

Test Coverage

Coverage 100%

Importance

Changes 6
Bugs 1 Features 1
Metric Value
wmc 11
lcom 1
cbo 6
dl 16
loc 157
rs 10
c 6
b 1
f 1
ccs 64
cts 64
cp 1

3 Methods

Rating   Name   Duplication   Size   Complexity  
A activationAction() 0 16 2
A addAction() 8 23 3
A jsonListAction() 0 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 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\ActivationType;
10
use Symfony\Component\Form\Extension\Core\Type\TextType;
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\JsonResponse;
17
use Symfony\Component\HttpFoundation\RedirectResponse;
18
use Symfony\Component\HttpFoundation\Request;
19
use Symfony\Component\Serializer\Normalizer\AbstractNormalizer;
20
21
class UserController extends Controller
22
{
23
    /**
24
     * @Route("/users", name="users_list")
25
     * @Template("@App/User/users.html.twig")
26
     *
27
     * @param Request $request
28
     *
29
     * @return array
30
     */
31 3
    public function listAction(Request $request)
32
    {
33 3
        $em = $this->getDoctrine()->getManager();
34 3
        $filter = new Filter();
35 3
        $filterForm = $this->createForm(FilterType::class, $filter)
36 3
            ->add('Search', SubmitType::class, [
37 3
                'attr' => ['class' => 'fa fa-search'],
38
            ]);
39 3
        $filterForm->handleRequest($request);
40 3
        $users = $this->get('knp_paginator')->paginate(
41 3
            $em->getRepository(User::class)->selectUsersByParams($filter),
42 3
            $request->query->getInt('page', 1),
43 3
            10
44
        );
45 3
        $activationForm = [];
46 3
        foreach ($users as $user) {
47 3
            $activationForm[$user->getId()] = $this->createForm(ActivationType::class, $user, [
48 3
                'method' => 'PUT',
49 3
                'action' => $this->generateUrl('activate_user', ['id' => $user->getId()]),
50 3
                'validation_groups' => 'edit',
51
            ])
52 3
                ->createView();
53
        }
54
55
        return [
56 3
            'users' => $users,
57 3
            'filterForm' => $filterForm->createView(),
58 3
            'activationForm' => $activationForm,
59
        ];
60
    }
61
62
    /**
63
     * @Route("/user/activate/{id}", name="activate_user")
64
     *
65
     * @Method("PUT")
66
     *
67
     * @param Request $request
68
     * @param User    $user
69
     *
70
     * @return RedirectResponse
71
     */
72 1
    public function activationAction(Request $request, User $user)
73
    {
74 1
        $em = $this->getDoctrine()->getManager();
75 1
        $form = $this->createForm(ActivationType::class, $user, [
76 1
            'method' => 'PUT',
77 1
            'action' => $this->generateUrl('activate_user', ['id' => $user->getId()]),
78 1
            'validation_groups' => 'edit',
79
        ]);
80 1
        $form->handleRequest($request);
81 1
        if ($form->isValid()) {
82 1
            $em->persist($user);
83 1
            $em->flush();
84
        }
85
86 1
        return $this->redirect($this->generateUrl('users_list'));
87
    }
88
89
    /**
90
     * @Route("/user/add", name="add_user")
91
     * @Template("@App/User/add.html.twig")
92
     *
93
     * @param Request $request
94
     *
95
     * @return array|RedirectResponse
96
     */
97 1
    public function addAction(Request $request)
98
    {
99 1
        $em = $this->getDoctrine()->getManager();
100 1
        $user = new User();
101 1
        $form = $this->createForm(EditType::class, $user, [
102 1
            'action' => $this->generateUrl('add_user'),
103 1
            'validation_groups' => 'registration',
104
        ])
105 1
            ->add('Register', SubmitType::class, [
106 1
                'attr' => ['class' => 'btn btn-primary'],
107
            ]);
108 1
        $form->handleRequest($request);
109 1 View Code Duplication
        if ($form->isSubmitted()) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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 1
            if ($form->isValid()) {
111 1
                $em->persist($user);
112 1
                $em->flush();
113
114 1
                return $this->redirect($this->generateUrl('users_list'));
115
            }
116
        }
117
118 1
        return ['form' => $form->createView()];
119
    }
120
121
    /**
122
     * @Route("/user/edit/{id}", name="edit_user")
123
     * @Template("@App/User/add.html.twig")
124
     *
125
     * @param Request $request
126
     * @param User    $user
127
     *
128
     * @return array|RedirectResponse
129
     */
130 1
    public function editAction(Request $request, User $user)
131
    {
132 1
        $em = $this->getDoctrine()->getManager();
133 1
        $form = $this->createForm(EditType::class, $user, [
134 1
            'action' => $this->generateUrl('edit_user', ['id' => $user->getId()]),
135 1
            'method' => 'POST',
136 1
            'validation_groups' => 'edit',
137
        ])
138 1
            ->add('image', TextType::class, [
139
                'attr' => [
140
                    'placeholder' => 'image',
141
                    'class' => 'form-control',
142 1
                ],
143
                'label' => false,
144
                'required' => false,
145
            ])
146 1
            ->add('Save', SubmitType::class, [
147 1
                'attr' => ['class' => 'btn btn-primary'],
148
            ]);
149 1
        $form->handleRequest($request);
150 1 View Code Duplication
        if ($form->isSubmitted()) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
151 1
            if ($form->isValid()) {
152 1
                $em->persist($user);
153 1
                $em->flush();
154
155 1
                return $this->redirect($this->generateUrl('users_list'));
156
            }
157
        }
158
159 1
        return ['form' => $form->createView()];
160
    }
161
162
    /**
163
     * @Route("/users-list")
164
     *
165
     * @Method("GET")
166
     *
167
     * @return JsonResponse
168
     */
169
    public function jsonListAction()
170
    {
171
        $users = $this->getDoctrine()
172
            ->getRepository('AppBundle:User')
173
            ->selectNotBlocked();
174
175
        return $this->json(['users' => $users], 200, [], [AbstractNormalizer::GROUPS => ['Default']]);
176
    }
177
}
178