Completed
Pull Request — master (#30)
by nonanerz
04:46
created

UserController::activationAction()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 16
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 11
CRAP Score 2

Importance

Changes 2
Bugs 0 Features 0
Metric Value
c 2
b 0
f 0
dl 0
loc 16
ccs 11
cts 11
cp 1
rs 9.4285
cc 2
eloc 11
nc 2
nop 2
crap 2
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\ActivationType;
11
use Symfony\Component\Form\Extension\Core\Type\TextType;
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/User/users.html.twig")
27
     *
28
     * @param Request $request
29
     *
30
     * @return array
31
     */
32 3
    public function listAction(Request $request)
33
    {
34 3
        $em = $this->getDoctrine()->getManager();
35 3
        $filter = new Filter();
36 3
        $filterForm = $this->createForm(FilterType::class, $filter)
37 3
            ->add('Search', SubmitType::class, [
38 3
                'attr' => ['class' => 'fa fa-search'],
39
            ]);
40 3
        $filterForm->handleRequest($request);
41 3
        $users = $this->get('knp_paginator')->paginate(
42 3
            $em->getRepository(User::class)->selectUsersByParams($filter),
43 3
            $request->query->getInt('page', 1),
44 3
            10
45
        );
46 3
        $activationForm = [];
47 3
        foreach ($users as $user) {
48 3
            $activationForm[$user->getId()] = $this->createForm(ActivationType::class, $user, [
49 3
                'method' => 'PUT',
50 3
                'action' => $this->generateUrl('activate_user', ['id' => $user->getId()]),
51 3
                'validation_groups' => 'edit',
52
            ])
53 3
                ->createView();
54
        }
55
56
        return [
57 3
            'users' => $users,
58 3
            'filterForm' => $filterForm->createView(),
59 3
            'activationForm' => $activationForm,
60
        ];
61
    }
62
63
    /**
64
     * @Route("/user/activate/{id}", name="activate_user")
65
     *
66
     * @Method("PUT")
67
     *
68
     * @param Request $request
69
     * @param User    $user
70
     *
71
     * @return RedirectResponse
72
     */
73 1
    public function activationAction(Request $request, User $user)
74
    {
75 1
        $em = $this->getDoctrine()->getManager();
76 1
        $form = $this->createForm(ActivationType::class, $user, [
77 1
            'method' => 'PUT',
78 1
            'action' => $this->generateUrl('activate_user', ['id' => $user->getId()]),
79 1
            'validation_groups' => 'edit',
80
        ]);
81 1
        $form->handleRequest($request);
82 1
        if ($form->isValid()) {
83 1
            $em->persist($user);
84 1
            $em->flush();
85
        }
86
87 1
        return $this->redirect($this->generateUrl('users_list'));
88
    }
89
90
    /**
91
     * @Route("/user/add", name="add_user")
92
     * @Template("@App/User/add.html.twig")
93
     *
94
     * @param Request $request
95
     *
96
     * @return array|RedirectResponse
97
     */
98 1
    public function addAction(Request $request)
99
    {
100 1
        $em = $this->getDoctrine()->getManager();
101 1
        $user = new User();
102 1
        $form = $this->createForm(EditType::class, $user, [
103 1
            'action' => $this->generateUrl('add_user'),
104 1
            'validation_groups' => 'registration',
105
        ])
106 1
            ->add('Register', SubmitType::class, [
107 1
                'attr' => ['class' => 'btn btn-primary'],
108
            ]);
109 1
        $form->handleRequest($request);
110 1 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...
111 1
            if ($form->isValid()) {
112 1
                $em->persist($user);
113 1
                $em->flush();
114
115 1
                return $this->redirect($this->generateUrl('users_list'));
116
            }
117
        }
118
119 1
        return ['form' => $form->createView()];
120
    }
121
122
    /**
123
     * @Route("/user/edit/{id}", name="edit_user")
124
     * @Template("@App/User/add.html.twig")
125
     *
126
     * @param Request $request
127
     * @param User    $user
128
     *
129
     * @return array|RedirectResponse
130
     */
131 1
    public function editAction(Request $request, User $user)
132
    {
133 1
        $em = $this->getDoctrine()->getManager();
134 1
        $form = $this->createForm(EditType::class, $user, [
135 1
            'action' => $this->generateUrl('edit_user', ['id' => $user->getId()]),
136 1
            'method' => 'POST',
137 1
            'validation_groups' => 'edit',
138
        ])
139 1
            ->add('image', TextType::class, [
140
                'attr' => [
141
                    'placeholder' => 'image',
142
                    'class' => 'form-control',
143 1
                ],
144
                'label' => false,
145
                'required' => false,
146
            ])
147 1
            ->add('Save', SubmitType::class, [
148 1
                'attr' => ['class' => 'btn btn-primary'],
149
            ]);
150 1
        $form->handleRequest($request);
151 1 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...
152 1
            if ($form->isValid()) {
153 1
                $em->persist($user);
154 1
                $em->flush();
155
156 1
                return $this->redirect($this->generateUrl('users_list'));
157
            }
158
        }
159
160 1
        return ['form' => $form->createView()];
161
    }
162
163
    /**
164
     * @Route("/users-list")
165
     *
166
     * @Method("GET")
167
     * @return JsonResponse
168
     */
169
    public function jsonListAction()
170
    {
171
        $users = $this->getDoctrine()
172
            ->getRepository('AppBundle:User')
173
            ->selectNotBlocked();
174
        return $this->json(['users' => $users], 200, [], [AbstractNormalizer::GROUPS => ['Default']]);
175
    }
176
}
177