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

UserController   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 136
Duplicated Lines 11.76 %

Coupling/Cohesion

Components 1
Dependencies 6

Test Coverage

Coverage 100%

Importance

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

2 Methods

Rating   Name   Duplication   Size   Complexity  
A activationAction() 0 16 2
A addAction() 8 23 3

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\RedirectResponse;
17
use Symfony\Component\HttpFoundation\Request;
18
19
class UserController extends Controller
20
{
21
    /**
22
     * @Route("/users", name="users_list")
23
     * @Template("@App/User/users.html.twig")
24
     *
25
     * @param Request $request
26
     * @return array
27
     */
28 3
    public function listAction(Request $request)
29
    {
30 3
        $em = $this->getDoctrine()->getManager();
31 3
        $filter = new Filter;
32 3
        $filterForm = $this->createForm(FilterType::class, $filter)
33 3
            ->add("Search", SubmitType::class, [
34
                "attr" => ["class" => "fa fa-search"]
35 3
            ]);
36 3
        $filterForm->handleRequest($request);
37 3
        $users = $this->get('knp_paginator')->paginate(
38 3
            $em->getRepository(User::class)->selectUsersByParams($filter),
39 3
            $request->query->getInt('page', 1),
40 3
            10
41
        );
42 3
        $activationForm = [];
43 3
        foreach ($users as $user) {
44 3
            $activationForm[$user->getId()] = $this->createForm(ActivationType::class, $user, [
45 3
                'method' => "PUT",
46 3
                'action' => $this->generateUrl('activate_user', ['id' => $user->getId()]),
47 3
                'validation_groups' => 'edit',
48
            ])
49 3
                ->createView();
50
        }
51
52
        return [
53 3
            "users" => $users,
54 3
            "filterForm" => $filterForm->createView(),
55 3
            "activationForm" => $activationForm
56
        ];
57
    }
58
59
    /**
60
     * @Route("/user/activate/{id}", name="activate_user")
61
     *
62
     * @Method("PUT")
63
     * @param  Request $request
64
     * @param  User $user
65
     * @return RedirectResponse
66
     */
67 1
    public function activationAction(Request $request, User $user)
68
    {
69 1
        $em = $this->getDoctrine()->getManager();
70 1
        $form = $this->createForm(ActivationType::class, $user, [
71 1
            'method' => "PUT",
72 1
            'action' => $this->generateUrl('activate_user', ['id' => $user->getId()]),
73 1
            'validation_groups' => 'edit',
74
        ]);
75 1
        $form->handleRequest($request);
76 1
        if ($form->isValid()) {
77 1
            $em->persist($user);
78 1
            $em->flush();
79
        }
80
81 1
        return $this->redirect($this->generateUrl("users_list"));
82
    }
83
84
    /**
85
     * @Route("/user/add", name="add_user")
86
     * @Template("@App/User/add.html.twig")
87
     *
88
     * @param Request $request
89
     * @return array|RedirectResponse
90
     */
91 1
    public function addAction(Request $request)
92
    {
93 1
        $em = $this->getDoctrine()->getManager();
94 1
        $user = new User();
95 1
        $form = $this->createForm(EditType::class, $user, [
96 1
            'action' => $this->generateUrl('add_user'),
97 1
            'validation_groups' => 'registration',
98
        ])
99 1
            ->add('Register', SubmitType::class, [
100
                'attr' => ['class' => 'btn btn-primary']
101 1
            ]);
102 1
        $form->handleRequest($request);
103 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...
104 1
            if ($form->isValid()) {
105 1
                $em->persist($user);
106 1
                $em->flush();
107
108 1
                return $this->redirect($this->generateUrl("users_list"));
109
            }
110
        }
111
112 1
        return ['form' => $form->createView()];
113
    }
114
115
    /**
116
     * @Route("/user/edit/{id}", name="edit_user")
117
     * @Template("@App/User/add.html.twig")
118
     *
119
     * @param Request $request
120
     * @param User $user
121
     * @return array|RedirectResponse
122
     */
123 1
    public function editAction(Request $request, User $user)
124
    {
125 1
        $em = $this->getDoctrine()->getManager();
126 1
        $form = $this->createForm(EditType::class, $user, [
127 1
            'action' => $this->generateUrl('edit_user', ['id' => $user->getId()]),
128 1
            'method' => 'POST',
129 1
            'validation_groups' => 'edit',
130
        ])
131 1
            ->add('image', TextType::class, [
132
                'attr' => [
133
                    'placeholder' => 'image',
134
                    'class' => 'form-control'
135 1
                ],
136
                'label' => false,
137
                'required' => false,
138
            ])
139 1
            ->add('Save', SubmitType::class, [
140
                'attr' => ['class' => 'btn btn-primary']
141 1
            ]);
142 1
        $form->handleRequest($request);
143 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...
144 1
            if ($form->isValid()) {
145 1
                $em->persist($user);
146 1
                $em->flush();
147
148 1
                return $this->redirect($this->generateUrl("users_list"));
149
            }
150
        }
151
152 1
        return ['form' => $form->createView()];
153
    }
154
}
155