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

UserController::userEditAction()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 20
Code Lines 14

Duplication

Lines 20
Ratio 100 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 20
loc 20
rs 9.4285
cc 2
eloc 14
nc 2
nop 2
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
        if(!$users) {
27
            return [
28
                'error' => 'Nothing found!'
29
            ];
30
        }
31
        $paginator = $this->get('knp_paginator');
32
        $pagination = $paginator->paginate($users, $request->query->getInt('page', 1), 10);
33
        return [
34
            "users" => $pagination
35
        ];
36
    }
37
38
    /**
39
     * @Route("/user/activate/{id}", name="activate_user")
40
     * @Template("@App/add.html.twig")
41
     *
42
     * @param  User $user
43
     * @return RedirectResponse
44
     */
45
    public function userActivateAction(User $user)
46
    {
47
        $em = $this->getDoctrine()->getManager();
48
        $user->setStatus($user->isEnabled() ? false : true);
49
        $em->persist($user);
50
        $em->flush();
51
52
        return new RedirectResponse($this->generateUrl("users_list"));
53
    }
54
55
    /**
56
     * @Route("/user/add", name="add_user")
57
     * @Template("@App/add.html.twig")
58
     *
59
     * @return array|RedirectResponse
60
     */
61 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...
62
    {
63
        $em = $this->getDoctrine()->getManager();
64
        $user = new User();
65
        $form = $this->createForm('AppBundle\Form\UserType', $user, [
66
            'action' => $this->generateUrl('add_user'),
67
            'method' => 'POST',
68
            'validation_groups' => array('registration'),
69
        ])
70
            ->add('Save', SubmitType::class, array(
71
                'attr' => ['class' => 'btn pull-right btn-warning']
72
            ));
73
        $form->handleRequest($request);
74
        if ($form->isValid()) {
75
            $em->persist($user);
76
            $em->flush();
77
78
            return new RedirectResponse($this->generateUrl("users_list"));
79
        }
80
        return ['form' => $form->createView()];
81
    }
82
83
    /**
84
     * @Route("/user/edit/{id}", name="edit_user")
85
     * @Template("@App/add.html.twig")
86
     *
87
     * @return array|RedirectResponse
88
     */
89 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...
90
    {
91
        $em = $this->getDoctrine()->getManager();
92
        $form = $this->createForm('AppBundle\Form\UserType', $user, [
93
            'action' => $this->generateUrl('edit_user', array('id' => $user->getId())),
94
            'method' => 'POST',
95
            'validation_groups' => array('edit'),
96
        ])
97
            ->add('Save', SubmitType::class, array(
98
                'attr' => ['class' => 'btn pull-right btn-warning']
99
            ));
100
        $form->handleRequest($request);
101
        if ($form->isValid()) {
102
            $em->persist($user);
103
            $em->flush();
104
105
            return new RedirectResponse($this->generateUrl("users_list"));
106
        }
107
        return ['form' => $form->createView()];
108
    }
109
}
110