Completed
Pull Request — dev (#34)
by nonanerz
04:07
created

UserController::usersListAction()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 10
Code Lines 7

Duplication

Lines 10
Ratio 100 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 3
Bugs 0 Features 1
Metric Value
dl 10
loc 10
rs 9.4285
c 3
b 0
f 1
ccs 0
cts 0
cp 0
cc 2
eloc 7
nc 2
nop 0
crap 6
1
<?php
2
3
namespace AppBundle\Controller;
4
5
use AppBundle\Entity\User;
6
use AppBundle\Entity\DTO\Filter;
7
use AppBundle\Exception\JsonHttpException;
8
use AppBundle\Form\FilterType;
9
use AppBundle\Form\User\EditType;
10
use AppBundle\Form\User\RegistrationType;
11
use AppBundle\Form\User\ActivationType;
12
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Method;
13
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
14
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;
15
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
16
use Symfony\Component\Form\Extension\Core\Type\SubmitType;
17
use Symfony\Component\HttpFoundation\JsonResponse;
18
use Symfony\Component\HttpFoundation\RedirectResponse;
19
use Symfony\Component\HttpFoundation\Request;
20
use Symfony\Component\Serializer\Normalizer\AbstractNormalizer;
21
22
class UserController extends Controller
23
{
24
    /**
25
     * @Route("/users", name="users_list")
26
     * @Template("@App/users.html.twig")
27
     *
28
     * @param Request $request
29
     * @return array
30
     */
31 3
    public function listAction(Request $request)
32
    {
33 3
        $em = $this->getDoctrine()->getManager();
34 3
        $filter = new Filter;
35 3
        $filterForm = $this->createForm(FilterType::class, $filter)
36 3
            ->add("Search", SubmitType::class, array(
37
                "attr" => array("class" => "fa fa-search")
38 3
            ));
39 3
        $filterForm->handleRequest($request);
40 3
        $users = $this->get('knp_paginator')->paginate(
41 3
            $em->getRepository(User::class)->selectUsersByParams($filter),
42 3
            $request->query->getInt('page', 1),
43 3
            10
44
        );
45 3
        $activationForm = [];
46 3
        foreach ($users as $user) {
47 3
            $activationForm[$user->getId()] = $this->createForm(ActivationType::class, $user, [
48 3
                'method' => "PUT",
49 3
                'action' => $this->generateUrl('activate_user', array('id' => $user->getId())),
50
                'validation_groups' => array('edit'),
51
            ])
52 3
                ->createView();
53
        }
54
55
        return [
56 3
            "users" => $users,
57 3
            "filterForm" => $filterForm->createView(),
58 3
            "activationForm" => $activationForm
59
        ];
60
    }
61
62
    /**
63
     * @Route("/user/activate/{id}", name="activate_user")
64
     * @Template("@App/add.html.twig")
65
     *
66
     * @Method("PUT")
67
     * @param  Request $request
68
     * @param  User $user
69
     * @return RedirectResponse
70
     */
71
    public function activationAction(Request $request, User $user)
72
    {
73
        $em = $this->getDoctrine()->getManager();
74
        $form = $this->createForm(ActivationType::class, $user, [
75
            'method' => "PUT",
76
            'action' => $this->generateUrl('activate_user', array('id' => $user->getId())),
77
            'validation_groups' => array('edit'),
78
        ]);
79
        $form->handleRequest($request);
80
        if ($form->isValid()) {
81
            $em->persist($user);
82
            $em->flush();
83
        }
84
85
        return $this->redirect($this->generateUrl("users_list"));
86
    }
87
88
    /**
89
     * @Route("/user/add", name="add_user")
90
     * @Template("@App/add.html.twig")
91
     *
92
     * @param Request $request
93
     * @return array|RedirectResponse
94
     */
95 1
    public function addAction(Request $request)
96
    {
97 1
        $em = $this->getDoctrine()->getManager();
98 1
        $user = new User();
99 1
        $form = $this->createForm(RegistrationType::class, $user, [
100 1
            'action' => $this->generateUrl('add_user'),
101
            'validation_groups' => array('registration'),
102
        ]);
103
        $form->handleRequest($request);
104 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...
105
            if ($form->isValid()) {
106
                $em->persist($user);
107
                $em->flush();
108
109
                return $this->redirect($this->generateUrl("users_list"));
110
            }
111
        }
112
113
        return ['form' => $form->createView()];
114
    }
115
116
    /**
117
     * @Route("/user/edit/{id}", name="edit_user")
118
     * @Template("@App/add.html.twig")
119
     *
120
     * @param Request $request
121
     * @param User $user
122
     * @return array|RedirectResponse
123
     */
124
    public function editAction(Request $request, User $user)
125
    {
126
        $em = $this->getDoctrine()->getManager();
127
        $form = $this->createForm(EditType::class, $user, [
128
            'action' => $this->generateUrl('edit_user', array('id' => $user->getId())),
129
            'method' => 'POST',
130
            'validation_groups' => array('edit'),
131
        ]);
132
        $form->handleRequest($request);
133 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...
134
            if ($form->isValid()) {
135
                $em->persist($user);
136
                $em->flush();
137
138
                return $this->redirect($this->generateUrl("users_list"));
139
            }
140
        }
141
142
        return ['form' => $form->createView()];
143
    }
144
145
    /**
146
     * @Route("/users-list")
147
     * @Method("GET")
148
     * @return JsonResponse
149
     */
150 View Code Duplication
    public function usersListAction()
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...
151
    {
152
        $users = $this->getDoctrine()
153
            ->getRepository('AppBundle:User')
154
            ->findAll();
155
        if (!$users) {
156
            throw new JsonHttpException(404, 'User not found.');
157
        }
158
        return $this->json(['users' => $users], 200, [], [AbstractNormalizer::GROUPS => ['Short']]);
159
    }
160
}
161