1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace SixtyEightPublishers\User\ForgotPassword\Control\ForgotPassword; |
6
|
|
|
|
7
|
|
|
use Nette\Application\UI\Form; |
8
|
|
|
use SixtyEightPublishers\SmartNetteComponent\UI\Control; |
9
|
|
|
use SixtyEightPublishers\TranslationBridge\TranslatorAwareTrait; |
10
|
|
|
use SixtyEightPublishers\TranslationBridge\TranslatorAwareInterface; |
11
|
|
|
use SixtyEightPublishers\User\ForgotPassword\Entity\PasswordRequestInterface; |
12
|
|
|
use SixtyEightPublishers\User\ForgotPassword\Exception\PasswordRequestCreationException; |
13
|
|
|
use SixtyEightPublishers\User\ForgotPassword\PasswordRequest\PasswordRequestSenderInterface; |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* @method void onSend(string $email, ?PasswordRequestInterface $passwordRequest) |
17
|
|
|
* @method void onError(PasswordRequestCreationException $e, string $email) |
18
|
|
|
* @method void onFormCreation(Form $form) |
19
|
|
|
*/ |
20
|
|
|
class ForgotPasswordControl extends Control implements TranslatorAwareInterface |
21
|
|
|
{ |
22
|
|
|
use TranslatorAwareTrait; |
23
|
|
|
|
24
|
|
|
/** @var \SixtyEightPublishers\User\ForgotPassword\PasswordRequest\PasswordRequestSenderInterface */ |
25
|
|
|
private $passwordRequestSender; |
26
|
|
|
|
27
|
|
|
/** @var callable[] */ |
28
|
|
|
public $onSend = []; |
29
|
|
|
|
30
|
|
|
/** @var callable[] */ |
31
|
|
|
public $onError = []; |
32
|
|
|
|
33
|
|
|
/** @var callable[] */ |
34
|
|
|
public $onFormCreation = []; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* @param \SixtyEightPublishers\User\ForgotPassword\PasswordRequest\PasswordRequestSenderInterface $passwordRequestSender |
38
|
|
|
*/ |
39
|
|
|
public function __construct(PasswordRequestSenderInterface $passwordRequestSender) |
40
|
|
|
{ |
41
|
|
|
$this->passwordRequestSender = $passwordRequestSender; |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* @return void |
46
|
|
|
*/ |
47
|
|
|
public function render(): void |
48
|
|
|
{ |
49
|
|
|
$this->template->setTranslator($this->getPrefixedTranslator()); |
50
|
|
|
$this->doRender(); |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* @return \Nette\Application\UI\Form |
55
|
|
|
*/ |
56
|
|
View Code Duplication |
protected function createComponentForm(): Form |
|
|
|
|
57
|
|
|
{ |
58
|
|
|
$form = new Form(); |
59
|
|
|
|
60
|
|
|
$form->setTranslator($this->getPrefixedTranslator()); |
61
|
|
|
|
62
|
|
|
$form->addText('email', 'email.field') |
63
|
|
|
->setRequired('email.required') |
64
|
|
|
->addRule($form::EMAIL, 'email.rule') |
65
|
|
|
->setHtmlAttribute('autocomplete', 'username'); |
66
|
|
|
|
67
|
|
|
$form->addProtection('protection.rule'); |
68
|
|
|
|
69
|
|
|
$form->onSuccess[] = [ $this, 'processForm' ]; |
70
|
|
|
|
71
|
|
|
$this->onFormCreation($form); |
72
|
|
|
|
73
|
|
|
$form->addSubmit('send', 'send.field'); |
74
|
|
|
|
75
|
|
|
return $form; |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* @internal |
80
|
|
|
* |
81
|
|
|
* @param \Nette\Application\UI\Form $form |
82
|
|
|
* |
83
|
|
|
* @return void |
84
|
|
|
*/ |
85
|
|
|
public function processForm(Form $form): void |
86
|
|
|
{ |
87
|
|
|
$email = $form->values->email; |
88
|
|
|
|
89
|
|
|
try { |
90
|
|
|
$request = $this->passwordRequestSender->send($email); |
91
|
|
|
|
92
|
|
|
$this->onSend($email, $request); |
93
|
|
|
} catch (PasswordRequestCreationException $e) { |
94
|
|
|
$this->onError($e, $email); |
95
|
|
|
} |
96
|
|
|
} |
97
|
|
|
} |
98
|
|
|
|
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.