Completed
Push — dev ( a70858...5b49da )
by nonanerz
06:08 queued 06:04
created

UserController   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 147
Duplicated Lines 10.88 %

Coupling/Cohesion

Components 1
Dependencies 6

Test Coverage

Coverage 92.42%

Importance

Changes 4
Bugs 0 Features 0
Metric Value
wmc 11
lcom 1
cbo 6
dl 16
loc 147
ccs 61
cts 66
cp 0.9242
rs 10
c 4
b 0
f 0

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\DTO\UserFilterType;
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(UserFilterType::class, $filter)
36 3
            ->add('Search', SubmitType::class);
37 3
        $filterForm->handleRequest($request);
38 3
        $users = $this->get('knp_paginator')->paginate(
39 3
            $em->getRepository(User::class)->selectUsersByParams($filter),
40 3
            $request->query->getInt('page', 1),
41 3
            10
42
        );
43 3
        $activationForm = [];
44 3
        foreach ($users as $user) {
45 3
            $activationForm[$user->getId()] = $this->createForm(ActivationType::class, $user, [
46 3
                'method' => 'PUT',
47 3
                'action' => $this->generateUrl('activate_user', ['id' => $user->getId()]),
48 3
                'validation_groups' => 'edit',
49
            ])
50 3
                ->createView();
51
        }
52
53
        return [
54 3
            'users' => $users,
55 3
            'filterForm' => $filterForm->createView(),
56 3
            'activationForm' => $activationForm,
57
        ];
58
    }
59
60
    /**
61
     * @Route("/user/activate/{id}", name="activate_user")
62
     *
63
     * @Method("PUT")
64
     *
65
     * @param Request $request
66
     * @param User    $user
67
     *
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
     *
93
     * @return array|RedirectResponse
94
     */
95 1
    public function addAction(Request $request)
96
    {
97 1
        $em = $this->getDoctrine()->getManager();
98 1
        $user = new User();
99 1
        $form = $this->createForm(EditType::class, $user, [
100 1
            'action' => $this->generateUrl('add_user'),
101 1
            'validation_groups' => 'registration',
102
        ])
103 1
            ->add('Register', SubmitType::class, [
104 1
                'attr' => ['class' => 'btn btn-primary'],
105
            ]);
106 1
        $form->handleRequest($request);
107 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...
108 1
            if ($form->isValid()) {
109 1
                $em->persist($user);
110 1
                $em->flush();
111
112 1
                return $this->redirect($this->generateUrl('users_list'));
113
            }
114
        }
115
116 1
        return ['form' => $form->createView()];
117
    }
118
119
    /**
120
     * @Route("/user/edit/{id}", name="edit_user")
121
     * @Template("@App/User/add.html.twig")
122
     *
123
     * @param Request $request
124
     * @param User    $user
125
     *
126
     * @return array|RedirectResponse
127
     */
128 1
    public function editAction(Request $request, User $user)
129
    {
130 1
        $em = $this->getDoctrine()->getManager();
131 1
        $form = $this->createForm(EditType::class, $user, [
132 1
            'action' => $this->generateUrl('edit_user', ['id' => $user->getId()]),
133 1
            'method' => 'POST',
134 1
            'validation_groups' => 'edit',
135
        ])
136 1
            ->add('Save', SubmitType::class, [
137 1
                'attr' => ['class' => 'btn btn-primary'],
138
            ]);
139 1
        $form->handleRequest($request);
140 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...
141 1
            if ($form->isValid()) {
142 1
                $em->persist($user);
143 1
                $em->flush();
144
145 1
                return $this->redirect($this->generateUrl('users_list'));
146
            }
147
        }
148
149 1
        return ['form' => $form->createView()];
150
    }
151
152
    /**
153
     * @Route("/users-list", name="users-list", options={"expose"=true})
154
     *
155
     * @Method("GET")
156
     *
157
     * @return JsonResponse
158
     */
159
    public function jsonListAction()
160
    {
161
        $users = $this->getDoctrine()
162
            ->getRepository('AppBundle:User')
163
            ->selectNotBlocked();
164
165
        return $this->json(['users' => $users], 200, [], [AbstractNormalizer::GROUPS => ['Default']]);
166
    }
167
}
168