Completed
Pull Request — master (#30)
by
unknown
03:36
created

UserController   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 150
Duplicated Lines 10.67 %

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 150
ccs 64
cts 64
cp 1
rs 10
c 6
b 1
f 1

3 Methods

Rating   Name   Duplication   Size   Complexity  
A activationAction() 0 16 2
A addAction() 8 23 3
A jsonListAction() 0 7 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\Exception\JsonHttpException;
8
use AppBundle\Form\FilterType;
9
use AppBundle\Form\User\EditType;
10
use AppBundle\Form\User\ActivationType;
11
use Symfony\Component\Form\Extension\Core\Type\TextType;
12
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Method;
13
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
14
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;
15
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
16
use Symfony\Component\Form\Extension\Core\Type\SubmitType;
17
use Symfony\Component\HttpFoundation\JsonResponse;
18
use Symfony\Component\HttpFoundation\RedirectResponse;
19
use Symfony\Component\HttpFoundation\Request;
20
use Symfony\Component\Serializer\Normalizer\AbstractNormalizer;
21
22
class UserController extends Controller
23
{
24
    /**
25
     * @Route("/users", name="users_list")
26
     * @Template("@App/User/users.html.twig")
27
     *
28
     * @param Request $request
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
                "attr" => ["class" => "fa fa-search"]
38 3
            ]);
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
     * @param  Request $request
67
     * @param  User $user
68
     * @return RedirectResponse
69
     */
70 1
    public function activationAction(Request $request, User $user)
71
    {
72 1
        $em = $this->getDoctrine()->getManager();
73 1
        $form = $this->createForm(ActivationType::class, $user, [
74 1
            'method' => "PUT",
75 1
            'action' => $this->generateUrl('activate_user', ['id' => $user->getId()]),
76 1
            'validation_groups' => 'edit',
77
        ]);
78 1
        $form->handleRequest($request);
79 1
        if ($form->isValid()) {
80 1
            $em->persist($user);
81 1
            $em->flush();
82
        }
83
84 1
        return $this->redirect($this->generateUrl("users_list"));
85
    }
86
87
    /**
88
     * @Route("/user/add", name="add_user")
89
     * @Template("@App/User/add.html.twig")
90
     *
91
     * @param Request $request
92
     * @return array|RedirectResponse
93
     */
94 1
    public function addAction(Request $request)
95
    {
96 1
        $em = $this->getDoctrine()->getManager();
97 1
        $user = new User();
98 1
        $form = $this->createForm(EditType::class, $user, [
99 1
            'action' => $this->generateUrl('add_user'),
100 1
            'validation_groups' => 'registration',
101
        ])
102 1
            ->add('Register', SubmitType::class, [
103
                'attr' => ['class' => 'btn btn-primary']
104 1
            ]);
105 1
        $form->handleRequest($request);
106 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...
107 1
            if ($form->isValid()) {
108 1
                $em->persist($user);
109 1
                $em->flush();
110
111 1
                return $this->redirect($this->generateUrl("users_list"));
112
            }
113
        }
114
115 1
        return ['form' => $form->createView()];
116
    }
117
118
    /**
119
     * @Route("/user/edit/{id}", name="edit_user")
120
     * @Template("@App/User/add.html.twig")
121
     *
122
     * @param Request $request
123
     * @param User $user
124
     * @return array|RedirectResponse
125
     */
126 1
    public function editAction(Request $request, User $user)
127
    {
128 1
        $em = $this->getDoctrine()->getManager();
129 1
        $form = $this->createForm(EditType::class, $user, [
130 1
            'action' => $this->generateUrl('edit_user', ['id' => $user->getId()]),
131 1
            'method' => 'POST',
132 1
            'validation_groups' => 'edit',
133
        ])
134 1
            ->add('image', TextType::class, [
135
                'attr' => [
136
                    'placeholder' => 'image',
137
                    'class' => 'form-control'
138 1
                ],
139
                'label' => false,
140
                'required' => false,
141
            ])
142 1
            ->add('Save', SubmitType::class, [
143
                'attr' => ['class' => 'btn btn-primary']
144 1
            ]);
145 1
        $form->handleRequest($request);
146 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...
147 1
            if ($form->isValid()) {
148 1
                $em->persist($user);
149 1
                $em->flush();
150
151 1
                return $this->redirect($this->generateUrl("users_list"));
152
            }
153
        }
154
155 1
        return ['form' => $form->createView()];
156
    }
157
158
    /**
159
     * @Route("/users-list")
160
     *
161
     * @Method("GET")
162
     * @return JsonResponse
163
     */
164
    public function jsonListAction()
165
    {
166
        $users = $this->getDoctrine()
167
            ->getRepository('AppBundle:User')
168
            ->selectNotBlocked();
169
        return $this->json(['users' => $users], 200, [], [AbstractNormalizer::GROUPS => ['Default']]);
170
    }
171
}
172