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

UserController::userAddAction()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 21
Code Lines 15

Duplication

Lines 21
Ratio 100 %

Importance

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