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

UserController::userEditAction()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 16
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 16
rs 9.4285
cc 2
eloc 12
nc 2
nop 2
1
<?php
2
3
namespace AppBundle\Controller;
4
5
use AppBundle\Entity\User;
6
use AppBundle\Form\User\EditType;
7
use AppBundle\Form\User\RegistrationType;
8
use AppBundle\Form\User\ActivationType;
9
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Method;
10
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
11
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;
12
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
13
use Symfony\Component\HttpFoundation\RedirectResponse;
14
use Symfony\Component\HttpFoundation\Request;
15
16
class UserController extends Controller
17
{
18
    /**
19
     * @Route("/users", name="users_list")
20
     * @Template("@App/users.html.twig")
21
     *
22
     * @param Request $request
23
     * @return array
24
     */
25
    public function usersListAction(Request $request)
26
    {
27
        $em = $this->getDoctrine()->getManager();
28
        $paginator = $this->get('knp_paginator');
29
        $users = $paginator->paginate(
30
            $em->getRepository(User::class)->selectUsersByParams($request->query),
31
            $request->query->getInt('page', 1),
32
            10
33
        );
34
        $activationForm = [];
35
        foreach ($users as $user) {
36
            $activationForm[$user->getId()] = $form = $this->createForm(ActivationType::class, $user, [
0 ignored issues
show
Unused Code introduced by
$form is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
37
                'method' => "PUT",
38
                'action' => $this->generateUrl('activate_user', ['id' => $user->getId()]),
39
                'validation_groups' => array('edit'),
40
            ])
41
                ->createView();
42
        }
43
        return [
44
            "users" => $users,
45
            'activationForm' => $activationForm
46
        ];
47
    }
48
49
    /**
50
     * @Route("/user/activate/{id}", name="activate_user")
51
     * @Template("@App/add.html.twig")
52
     *
53
     * @Method("PUT")
54
     * @param  Request $request
55
     * @param  User $user
56
     * @return RedirectResponse
57
     */
58
    public function activationAction(Request $request, User $user)
59
    {
60
        $em = $this->getDoctrine()->getManager();
61
        $form = $this->createForm(ActivationType::class, $user, [
62
            'method' => "PUT",
63
            'action' => $this->generateUrl('activate_user'),
64
            'validation_groups' => array('edit'),
65
        ])
66
            ->createView();
67
        $form->handleRequest($request);
0 ignored issues
show
Bug introduced by
The method handleRequest() does not seem to exist on object<Symfony\Component\Form\FormView>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
68
        if ($form->isValid()) {
0 ignored issues
show
Bug introduced by
The method isValid() does not seem to exist on object<Symfony\Component\Form\FormView>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
69
            $em->persist($user);
70
            $em->flush();
71
            return $this->redirect($this->generateUrl("users_list"));
72
        }
73
        return $this->redirect($this->generateUrl("users_list"));
74
75
    }
76
77
    /**
78
     * @Route("/user/add", name="add_user")
79
     * @Template("@App/add.html.twig")
80
     *
81
     * @param Request $request
82
     * @return array|RedirectResponse
83
     */
84
    public function userAddAction(Request $request)
85
    {
86
        $em = $this->getDoctrine()->getManager();
87
        $user = new User();
88
        $form = $this->createForm(RegistrationType::class, $user, [
89
            'action' => $this->generateUrl('add_user'),
90
            'validation_groups' => array('registration'),
91
        ]);
92
        $form->handleRequest($request);
93
        if ($form->isValid()) {
94
            $em->persist($user);
95
            $em->flush();
96
            return $this->redirect($this->generateUrl("users_list"));
97
        }
98
        return ['form' => $form->createView()];
99
    }
100
101
    /**
102
     * @Route("/user/edit/{id}", name="edit_user")
103
     * @Template("@App/add.html.twig")
104
     *
105
     * @param Request $request
106
     * @param User $user
107
     * @return array|RedirectResponse
108
     */
109
    public function userEditAction(Request $request, User $user)
110
    {
111
        $em = $this->getDoctrine()->getManager();
112
        $form = $this->createForm(EditType::class, $user, [
113
            'action' => $this->generateUrl('edit_user', array('id' => $user->getId())),
114
            'method' => 'POST',
115
            'validation_groups' => array('edit'),
116
        ]);
117
        $form->handleRequest($request);
118
        if ($form->isValid()) {
119
            $em->persist($user);
120
            $em->flush();
121
            return $this->redirect($this->generateUrl("users_list"));
122
        }
123
        return ['form' => $form->createView()];
124
    }
125
}
126