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