Completed
Pull Request — master (#30)
by
unknown
04:23
created

UserController::addAction()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 23
Code Lines 15

Duplication

Lines 8
Ratio 34.78 %

Code Coverage

Tests 15
CRAP Score 3

Importance

Changes 2
Bugs 0 Features 0
Metric Value
c 2
b 0
f 0
dl 8
loc 23
ccs 15
cts 15
cp 1
rs 9.0856
cc 3
eloc 15
nc 3
nop 1
crap 3
1
<?php
2
3
namespace AppBundle\Controller;
4
5
use AppBundle\Entity\User;
6
use AppBundle\Entity\DTO\Filter;
7
use AppBundle\Form\FilterType;
8
use AppBundle\Form\User\EditType;
9
use AppBundle\Form\User\ActivationType;
10
use Symfony\Component\Form\Extension\Core\Type\TextType;
11
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Method;
12
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
13
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;
14
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
15
use Symfony\Component\Form\Extension\Core\Type\SubmitType;
16
use Symfony\Component\HttpFoundation\JsonResponse;
17
use Symfony\Component\HttpFoundation\RedirectResponse;
18
use Symfony\Component\HttpFoundation\Request;
19
use Symfony\Component\Serializer\Normalizer\AbstractNormalizer;
20
21
class UserController extends Controller
22
{
23
    /**
24
     * @Route("/users", name="users_list")
25
     * @Template("@App/User/users.html.twig")
26
     *
27
     * @param Request $request
28
     *
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, [
37 3
                'attr' => ['class' => 'fa fa-search'],
38
            ]);
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', ['id' => $user->getId()]),
50 3
                'validation_groups' => '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
     *
65
     * @Method("PUT")
66
     *
67
     * @param Request $request
68
     * @param User    $user
69
     *
70
     * @return RedirectResponse
71
     */
72 1
    public function activationAction(Request $request, User $user)
73
    {
74 1
        $em = $this->getDoctrine()->getManager();
75 1
        $form = $this->createForm(ActivationType::class, $user, [
76 1
            'method' => 'PUT',
77 1
            'action' => $this->generateUrl('activate_user', ['id' => $user->getId()]),
78 1
            'validation_groups' => 'edit',
79
        ]);
80 1
        $form->handleRequest($request);
81 1
        if ($form->isValid()) {
82 1
            $em->persist($user);
83 1
            $em->flush();
84
        }
85
86 1
        return $this->redirect($this->generateUrl('users_list'));
87
    }
88
89
    /**
90
     * @Route("/user/add", name="add_user")
91
     * @Template("@App/User/add.html.twig")
92
     *
93
     * @param Request $request
94
     *
95
     * @return array|RedirectResponse
96
     */
97 1
    public function addAction(Request $request)
98
    {
99 1
        $em = $this->getDoctrine()->getManager();
100 1
        $user = new User();
101 1
        $form = $this->createForm(EditType::class, $user, [
102 1
            'action' => $this->generateUrl('add_user'),
103 1
            'validation_groups' => 'registration',
104
        ])
105 1
            ->add('Register', SubmitType::class, [
106 1
                'attr' => ['class' => 'btn btn-primary'],
107
            ]);
108 1
        $form->handleRequest($request);
109 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...
110 1
            if ($form->isValid()) {
111 1
                $em->persist($user);
112 1
                $em->flush();
113
114 1
                return $this->redirect($this->generateUrl('users_list'));
115
            }
116
        }
117
118 1
        return ['form' => $form->createView()];
119
    }
120
121
    /**
122
     * @Route("/user/edit/{id}", name="edit_user")
123
     * @Template("@App/User/add.html.twig")
124
     *
125
     * @param Request $request
126
     * @param User    $user
127
     *
128
     * @return array|RedirectResponse
129
     */
130 1
    public function editAction(Request $request, User $user)
131
    {
132 1
        $em = $this->getDoctrine()->getManager();
133 1
        $form = $this->createForm(EditType::class, $user, [
134 1
            'action' => $this->generateUrl('edit_user', ['id' => $user->getId()]),
135 1
            'method' => 'POST',
136 1
            'validation_groups' => 'edit',
137
        ])
138 1
            ->add('image', TextType::class, [
139
                'attr' => [
140
                    'placeholder' => 'image',
141
                    'class' => 'form-control',
142 1
                ],
143
                'label' => false,
144
                'required' => false,
145
            ])
146 1
            ->add('Save', SubmitType::class, [
147 1
                'attr' => ['class' => 'btn btn-primary'],
148
            ]);
149 1
        $form->handleRequest($request);
150 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...
151 1
            if ($form->isValid()) {
152 1
                $em->persist($user);
153 1
                $em->flush();
154
155 1
                return $this->redirect($this->generateUrl('users_list'));
156
            }
157
        }
158
159 1
        return ['form' => $form->createView()];
160
    }
161
162
    /**
163
     * @Route("/users-list")
164
     *
165
     * @Method("GET")
166
     *
167
     * @return JsonResponse
168
     */
169
    public function jsonListAction()
170
    {
171
        $users = $this->getDoctrine()
172
            ->getRepository('AppBundle:User')
173
            ->selectNotBlocked();
174
175
        return $this->json(['users' => $users], 200, [], [AbstractNormalizer::GROUPS => ['Default']]);
176
    }
177
}
178