Completed
Pull Request — dev (#22)
by
unknown
03:24
created

UserController::activationAction()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 16
Code Lines 11

Duplication

Lines 16
Ratio 100 %

Importance

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