UserController   A
last analyzed

Complexity

Total Complexity 10

Size/Duplication

Total Lines 178
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 10
dl 0
loc 178
ccs 54
cts 54
cp 1
rs 10
c 0
b 0
f 0

7 Methods

Rating   Name   Duplication   Size   Complexity  
A redirectToRoute() 0 5 1
A loginCheckAction() 0 2 1
A __construct() 0 9 1
A loginAction() 0 21 1
B registrationAction() 0 25 3
A activateemailAction() 0 12 2
A registrationsuccessAction() 0 3 1
1
<?php
2
/* Copyright (C) 2015 Michael Giesler
3
 *
4
 * This file is part of Dembelo.
5
 *
6
 * Dembelo is free software: you can redistribute it and/or modify
7
 * it under the terms of the GNU Affero General Public License as published by
8
 * the Free Software Foundation, either version 3 of the License, or
9
 * (at your option) any later version.
10
 *
11
 * Dembelo is distributed in the hope that it will be useful,
12
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14
 * GNU Affero General Public License 3 for more details.
15
 *
16
 * You should have received a copy of the GNU Affero General Public License 3
17
 * along with Dembelo. If not, see <http://www.gnu.org/licenses/>.
18
 */
19
20
/**
21
 * @package DembeloMain
22
 */
23
24
namespace DembeloMain\Controller;
25
26
use DembeloMain\Document\User;
27
use DembeloMain\Form\Login;
28
use DembeloMain\Form\Registration;
29
use DembeloMain\Model\Repository\UserRepositoryInterface;
30
use Doctrine\ODM\MongoDB\DocumentManager;
31
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
32
use Symfony\Component\Form\FormFactoryInterface;
33
use Symfony\Component\HttpFoundation\RedirectResponse;
34
use Symfony\Component\HttpFoundation\Request;
35
use Symfony\Component\HttpFoundation\Response;
36
use Symfony\Component\Security\Core\Encoder\UserPasswordEncoder;
37
use Symfony\Component\Security\Http\Authentication\AuthenticationUtils;
38
use Symfony\Bundle\FrameworkBundle\Templating\EngineInterface as Templating;
39
use Symfony\Bundle\FrameworkBundle\Routing\Router;
40
41
/**
42
 * Class DefaultController
43
 * @Route(service="app.controller_user")
44
 */
45
class UserController
46
{
47
    /**
48
     * @var AuthenticationUtils
49
     */
50
    private $authenticationUtils;
51
52
    /**
53
     * @var UserRepositoryInterface
54
     */
55
    private $userRepository;
56
57
    /**
58
     * @var DocumentManager
59
     */
60
    private $documentManager;
61
62
    /**
63
     * @var Templating
64
     */
65
    private $templating;
66
67
    /**
68
     * @var Router
69
     */
70
    private $router;
71
72
    /**
73
     * @var FormFactoryInterface
74
     */
75
    private $formFactory;
76
77
    /**
78
     * @var UserPasswordEncoder
79
     */
80
    private $passwordEncoder;
81
82
    /**
83
     * UserController constructor.
84
     * @param AuthenticationUtils     $authenticationUtils
85
     * @param UserRepositoryInterface $userRepository
86
     * @param DocumentManager         $documentManager
87
     * @param Templating              $templating
88
     * @param Router                  $router
89
     * @param FormFactoryInterface    $formFactory
90
     * @param UserPasswordEncoder     $passwordEncoder
91
     */
92 7
    public function __construct(AuthenticationUtils $authenticationUtils, UserRepositoryInterface $userRepository, DocumentManager $documentManager, Templating $templating, Router $router, FormFactoryInterface $formFactory, UserPasswordEncoder $passwordEncoder)
93
    {
94 7
        $this->authenticationUtils = $authenticationUtils;
95 7
        $this->userRepository = $userRepository;
96 7
        $this->documentManager = $documentManager;
97 7
        $this->templating = $templating;
98 7
        $this->router = $router;
99 7
        $this->formFactory = $formFactory;
100 7
        $this->passwordEncoder = $passwordEncoder;
101 7
    }
102
103
    /**
104
     * @Route("/login", name="login_route")
105
     *
106
     * @return Response
107
     */
108 1
    public function loginAction(): Response
109
    {
110 1
        $error = $this->authenticationUtils->getLastAuthenticationError();
111
112
        // last username entered by the user
113 1
        $lastUsername = $this->authenticationUtils->getLastUsername();
114
115 1
        $user = new User();
116 1
        $user->setEmail($lastUsername);
117
118 1
        $url = $this->router->generate('login_check');
119
120 1
        $form = $this->formFactory->create(Login::class, $user, [
121 1
            'action' => $url,
122
        ]);
123
124 1
        return $this->templating->renderResponse(
125 1
            'DembeloMain::user/login.html.twig',
126
            [
127 1
                'error' => $error,
128 1
                'form' => $form->createView(),
129
            ]
130
        );
131
    }
132
133
    /**
134
     * @Route("/login_check", name="login_check")
135
     *
136
     * @return void
137
     */
138 1
    public function loginCheckAction(): void
139
    {
140 1
    }
141
142
    /**
143
     * @Route("/registration", name="register")
144
     *
145
     * @param Request $request request object
146
     *
147
     * @return Response
148
     *
149
     * @throws \Exception
150
     */
151 2
    public function registrationAction(Request $request): Response
152
    {
153 2
        $user = new User();
154 2
        $user->setRoles(['ROLE_USER']);
155 2
        $user->setStatus(0);
156
157 2
        $form = $this->formFactory->create(Registration::class, $user);
158
159 2
        $form->handleRequest($request);
160
161 2
        if ($form->isSubmitted() && $form->isValid()) {
162 1
            $password = $this->passwordEncoder->encodePassword($user, $user->getPassword());
163 1
            $user->setPassword($password);
164 1
            $user->setMetadata('created', time());
165 1
            $user->setMetadata('updated', time());
166 1
            $this->documentManager->persist($user);
167 1
            $this->documentManager->flush();
168
169 1
            return $this->redirectToRoute('registration_success');
170
        }
171
172 1
        return $this->templating->renderResponse(
173 1
            'DembeloMain::user/register.html.twig',
174
            [
175 1
                'form' => $form->createView(),
176
            ]
177
        );
178
    }
179
180
    /**
181
     * @Route("/registrationSuccess", name="registration_success")
182
     *
183
     * @return Response
184
     */
185 1
    public function registrationsuccessAction(): Response
186
    {
187 1
        return $this->templating->renderResponse('DembeloMain::user/registrationSuccess.html.twig');
188
    }
189
190
    /**
191
     * @Route("/activation/{hash}", name="emailactivation")
192
     *
193
     * @param string $hash activation hash
194
     *
195
     * @return Response
196
     */
197 2
    public function activateemailAction(string $hash): Response
198
    {
199 2
        $user = $this->userRepository->findOneBy(['activationHash' => $hash]);
200 2
        if (null === $user) {
201 1
            throw new \InvalidArgumentException('no user found for hash');
202
        }
203 1
        $user->setActivationHash('');
204 1
        $user->setStatus(1);
205 1
        $this->documentManager->persist($user);
206 1
        $this->documentManager->flush();
207
208 1
        return $this->templating->renderResponse('DembeloMain::user/activationSuccess.html.twig');
209
    }
210
211
    /**
212
     * @param string $route
213
     * @param array  $parameters
214
     * @param int    $status
215
     *
216
     * @return RedirectResponse
217
     */
218 1
    protected function redirectToRoute($route, array $parameters = array(), $status = 302): RedirectResponse
219
    {
220 1
        $url = $this->router->generate($route, $parameters);
221
222 1
        return new RedirectResponse($url, $status);
223
    }
224
}
225