Completed
Pull Request — dev (#22)
by nonanerz
03:38
created

UserController::activationAction()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 16
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
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\DTO\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 listAction(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
        $users = $this->get('knp_paginator')->paginate(
38
            $em->getRepository(User::class)->selectUsersByParams($filter),
39
            $request->query->getInt('page', 1),
40
            10
41
        );
42
        $activationForm = [];
43
        foreach ($users as $user) {
44
            $activationForm[$user->getId()] = $this->createForm(ActivationType::class, $user, [
45
                'method' => "PUT",
46
                'action' => $this->generateUrl('activate_user', array('id' => $user->getId())),
47
                'validation_groups' => array('edit'),
48
            ])
49
                ->createView();
50
        }
51
52
        return [
53
            "users" => $users,
54
            "filterForm" => $filterForm->createView(),
55
            "activationForm" => $activationForm
56
        ];
57
    }
58
59
    /**
60
     * @Route("/user/activate/{id}", name="activate_user")
61
     * @Template("@App/add.html.twig")
62
     *
63
     * @Method("PUT")
64
     * @param  Request $request
65
     * @param  User $user
66
     * @return RedirectResponse
67
     */
68
    public function activationAction(Request $request, User $user)
69
    {
70
        $em = $this->getDoctrine()->getManager();
71
        $form = $this->createForm(ActivationType::class, $user, [
72
            'method' => "PUT",
73
            'action' => $this->generateUrl('activate_user', array('id' => $user->getId())),
74
            'validation_groups' => array('edit'),
75
        ]);
76
        $form->handleRequest($request);
77
        if ($form->isValid()) {
78
            $em->persist($user);
79
            $em->flush();
80
        }
81
82
        return $this->redirect($this->generateUrl("users_list"));
83
    }
84
85
    /**
86
     * @Route("/user/add", name="add_user")
87
     * @Template("@App/add.html.twig")
88
     *
89
     * @param Request $request
90
     * @return array|RedirectResponse
91
     */
92
    public function addAction(Request $request)
93
    {
94
        $em = $this->getDoctrine()->getManager();
95
        $user = new User();
96
        $form = $this->createForm(RegistrationType::class, $user, [
97
            'action' => $this->generateUrl('add_user'),
98
            'validation_groups' => array('registration'),
99
        ]);
100
        $form->handleRequest($request);
101 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...
102
            if ($form->isValid()) {
103
                $em->persist($user);
104
                $em->flush();
105
106
                return $this->redirect($this->generateUrl("users_list"));
107
            }
108
        }
109
110
        return ['form' => $form->createView()];
111
    }
112
113
    /**
114
     * @Route("/user/edit/{id}", name="edit_user")
115
     * @Template("@App/add.html.twig")
116
     *
117
     * @param Request $request
118
     * @param User $user
119
     * @return array|RedirectResponse
120
     */
121
    public function editAction(Request $request, User $user)
122
    {
123
        $em = $this->getDoctrine()->getManager();
124
        $form = $this->createForm(EditType::class, $user, [
125
            'action' => $this->generateUrl('edit_user', array('id' => $user->getId())),
126
            'method' => 'POST',
127
            'validation_groups' => array('edit'),
128
        ]);
129
        $form->handleRequest($request);
130 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...
131
            if ($form->isValid()) {
132
                $em->persist($user);
133
                $em->flush();
134
135
                return $this->redirect($this->generateUrl("users_list"));
136
            }
137
        }
138
139
        return ['form' => $form->createView()];
140
    }
141
}
142