Completed
Pull Request — dev (#22)
by
unknown
23:02
created

UserController::usersListAction()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 10
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 10
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 7
nc 1
nop 1
1
<?php
2
3
namespace AppBundle\Controller;
4
5
use AppBundle\Entity\User;
6
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Method;
7
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
8
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;
9
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
10
use Symfony\Component\HttpFoundation\RedirectResponse;
11
use Symfony\Component\HttpFoundation\Request;
12
use Symfony\Component\Form\Extension\Core\Type\SubmitType;
13
14
class UserController extends Controller
15
{
16
    /**
17
     * @Route("/users", name="users_list")
18
     * @Template("@App/users.html.twig")
19
     *
20
     * @return array
21
     */
22
    public function usersListAction(Request $request)
23
    {
24
        $em = $this->getDoctrine()->getManager();
25
        $users = $em->getRepository(User::class)->getUsersByParams($request->query);
26
        $paginator = $this->get('knp_paginator');
27
        $pagination = $paginator->paginate($users, $request->query->getInt('page', 1), 10);
28
        return [
29
            "users" => $pagination
30
        ];
31
    }
32
33
    /**
34
     * @Route("/user/activate/{id}", name="activate_user")
35
     * @Template("@App/add.html.twig")
36
     *
37
     * @return RedirectResponse
38
     */
39
    public function userActivateAction(Request $request, User $user)
0 ignored issues
show
Unused Code introduced by
The parameter $request is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
40
    {
41
        $em = $this->getDoctrine()->getManager();
42
        if($user->isEnabled()){
43
            $user->setStatus(false);
0 ignored issues
show
Bug introduced by
The method setStatus() does not seem to exist on object<AppBundle\Entity\User>.

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...
44
        } else {
45
            $user->setStatus(true);
0 ignored issues
show
Bug introduced by
The method setStatus() does not seem to exist on object<AppBundle\Entity\User>.

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...
46
        }
47
        $em->persist($user);
48
        $em->flush();
49
50
        return new RedirectResponse($this->generateUrl("users_list"));
51
    }
52
53
    /**
54
     * @Route("/user/add", name="add_user")
55
     * @Template("@App/add.html.twig")
56
     *
57
     * @return array|RedirectResponse
58
     */
59 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...
60
    {
61
        $em = $this->getDoctrine()->getManager();
62
        $user = new User();
63
        $form = $this->createForm('AppBundle\Form\UserType', $user, [
64
            'action' => $this->generateUrl('add_user'),
65
            'method' => 'POST',
66
            'validation_groups' => array('registration'),
67
        ])
68
            ->add('Save', SubmitType::class, array(
69
                'attr' => ['class' => 'btn pull-right btn-warning']
70
            ));
71
        $form->handleRequest($request);
72
        if ($form->isValid()) {
73
            $em->persist($user);
74
            $em->flush();
75
76
            return new RedirectResponse($this->generateUrl("users_list"));
77
        }
78
        return ['form' => $form->createView()];
79
    }
80
81
    /**
82
     * @Route("/user/edit/{id}", name="edit_user")
83
     * @Template("@App/add.html.twig")
84
     *
85
     * @return array|RedirectResponse
86
     */
87 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...
88
    {
89
        $em = $this->getDoctrine()->getManager();
90
        $form = $this->createForm('AppBundle\Form\UserType', $user, [
91
            'action' => $this->generateUrl('edit_user', array('id' => $user->getId())),
92
            'method' => 'POST',
93
            'validation_groups' => array('edit'),
94
        ])
95
            ->add('Save', SubmitType::class, array(
96
                'attr' => ['class' => 'btn pull-right btn-warning']
97
            ));
98
        $form->handleRequest($request);
99
        if ($form->isValid()) {
100
            $em->persist($user);
101
            $em->flush();
102
103
            return new RedirectResponse($this->generateUrl("users_list"));
104
        }
105
        return ['form' => $form->createView()];
106
    }
107
}
108