1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace SumoCoders\FrameworkMultiUserBundle\Controller; |
4
|
|
|
|
5
|
|
|
use SumoCoders\FrameworkMultiUserBundle\Command\RequestPasswordResetHandler; |
6
|
|
|
use SumoCoders\FrameworkMultiUserBundle\Exception\UserNotFound; |
7
|
|
|
use SumoCoders\FrameworkMultiUserBundle\Form\RequestPasswordType; |
8
|
|
|
use Symfony\Bundle\FrameworkBundle\Routing\Router; |
9
|
|
|
use Symfony\Bundle\FrameworkBundle\Templating\EngineInterface; |
10
|
|
|
use Symfony\Bundle\FrameworkBundle\Translation\Translator; |
11
|
|
|
use Symfony\Component\Form\FormError; |
12
|
|
|
use Symfony\Component\Form\FormFactoryInterface; |
13
|
|
|
use Symfony\Component\HttpFoundation\RedirectResponse; |
14
|
|
|
use Symfony\Component\HttpFoundation\Request; |
15
|
|
|
use Symfony\Component\HttpFoundation\Session\Flash\FlashBagInterface; |
16
|
|
|
use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorage; |
17
|
|
|
|
18
|
|
|
final class RequestPasswordResetController |
19
|
|
|
{ |
20
|
|
|
/** @var EngineInterface */ |
21
|
|
|
private $templating; |
22
|
|
|
|
23
|
|
|
/** @var FormFactoryInterface */ |
24
|
|
|
private $formFactory; |
25
|
|
|
|
26
|
|
|
/** @var RequestPasswordResetHandler */ |
27
|
|
|
private $requestPasswordResetHandler; |
28
|
|
|
|
29
|
|
|
/** @var Router */ |
30
|
|
|
private $router; |
31
|
|
|
|
32
|
|
|
/** @var Translator */ |
33
|
|
|
private $translator; |
34
|
|
|
|
35
|
|
|
/** @var FlashBagInterface */ |
36
|
|
|
private $flashBag; |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* @param EngineInterface $templating |
40
|
|
|
* @param FormFactoryInterface $formFactory |
41
|
|
|
* @param RequestPasswordResetHandler $requestPasswordResetHandler |
42
|
|
|
* @param Router $router |
43
|
|
|
* @param Translator $translator |
44
|
|
|
* @param FlashBagInterface $flashBag |
45
|
|
|
*/ |
46
|
|
View Code Duplication |
public function __construct( |
|
|
|
|
47
|
|
|
EngineInterface $templating, |
48
|
|
|
FormFactoryInterface $formFactory, |
49
|
|
|
RequestPasswordResetHandler $requestPasswordResetHandler, |
50
|
|
|
Router $router, |
51
|
|
|
Translator $translator, |
52
|
|
|
FlashBagInterface $flashBag |
53
|
|
|
) { |
54
|
|
|
$this->templating = $templating; |
55
|
|
|
$this->formFactory = $formFactory; |
56
|
|
|
$this->requestPasswordResetHandler = $requestPasswordResetHandler; |
57
|
|
|
$this->router = $router; |
58
|
|
|
$this->translator = $translator; |
59
|
|
|
$this->flashBag = $flashBag; |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* @param Request $request |
64
|
|
|
* |
65
|
|
|
* @return array |
66
|
|
|
*/ |
67
|
|
|
public function requestAction(Request $request) |
68
|
|
|
{ |
69
|
|
|
$form = $this->formFactory->create(RequestPasswordType::class); |
70
|
|
|
|
71
|
|
|
$form->handleRequest($request); |
72
|
|
|
|
73
|
|
|
if ($form->isSubmitted() && $form->isValid()) { |
74
|
|
|
try { |
75
|
|
|
$this->requestPasswordResetHandler->handle($form->getData()); |
76
|
|
|
|
77
|
|
|
$this->flashBag->add( |
78
|
|
|
'success', |
79
|
|
|
$this->translator->trans( |
80
|
|
|
'sumocoders.multiuserbundle.flash.password_reset_request_success' |
81
|
|
|
) |
82
|
|
|
); |
83
|
|
|
|
84
|
|
|
return new RedirectResponse($this->router->generate('multi_user_login')); |
85
|
|
|
} catch (UserNotFound $exception) { |
86
|
|
|
$errorMessage = $this->translator->trans( |
87
|
|
|
'sumocoders.multiuserbundle.request_password_reset.username_not_found', |
88
|
|
|
[ |
89
|
|
|
'%username%' => $form->getData()->userName |
90
|
|
|
], |
91
|
|
|
'validators' |
92
|
|
|
); |
93
|
|
|
$form->addError(new FormError($errorMessage)); |
94
|
|
|
} |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
return $this->templating->renderResponse( |
98
|
|
|
'SumoCodersFrameworkMultiUserBundle:PasswordReset:request.html.twig', |
99
|
|
|
[ |
100
|
|
|
'form' => $form->createView(), |
101
|
|
|
] |
102
|
|
|
); |
103
|
|
|
} |
104
|
|
|
} |
105
|
|
|
|
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.