Completed
Pull Request — dev (#22)
by
unknown
12:19
created

UserController::userAddAction()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 18
Code Lines 12

Duplication

Lines 18
Ratio 100 %

Importance

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