Completed
Pull Request — dev (#44)
by
unknown
03:04
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\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
    /**
179
     * @param Request $request, string $token
0 ignored issues
show
Documentation introduced by
There is no parameter named $request,. Did you maybe mean $request?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function. It has, however, found a similar but not annotated parameter which might be a good fit.

Consider the following example. The parameter $ireland is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $ireland
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was changed, but the annotation was not.

Loading history...
180
     * @Route("/password_update/{token}", name="password_update")
181
     * @Template("@App/User/update_password.html.twig")
182
     * @Method({"GET", "POST"})
183
     *
184
     * @return array
185
     */
186 1
    public function updatePasswordAction(Request $request, $token)
187
    {
188 1
        $user = $this->getDoctrine()->getRepository(User::class)->loadUserByToken($token);
189 1
        if ((!$user)) {
190
            return ['message' => 'Your link is expired!'];
191
        }
192 1
        $linkDate = $user->getLinkExpiredAt();
193 1
        $date = new \DateTime('now');
194 1
        if (($linkDate < $date)) {
195
            return ['message' => 'Your link is expired!'];
196
        }
197 1
        $form = $this->createForm(\AppBundle\Form\User\ResetPasswordType::class, $user);
198 1
        $form->handleRequest($request);
199
200 1
        if ($form->isSubmitted() && $form->isValid()) {
201
            $em = $this->getDoctrine()->getManager();
202
            $encoder = $this->get('security.password_encoder');
203
            $user->setPassword($encoder->encodePassword($user, $user->getPlainPassword()));
204
            $em->persist($user);
205
            $em->flush();
206
207
            return ['message' => 'Your password was successfully updated!', 'user' => $user];
208
        }
209
210 1
        return ['message' => 'Please, enter your new password', 'form' => $form->createView()];
211
    }
212
}
213