Completed
Pull Request — master (#2)
by Vojtěch
10:05
created

ResetPasswordControl::createComponentForm()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 23

Duplication

Lines 23
Ratio 100 %

Importance

Changes 0
Metric Value
dl 23
loc 23
rs 9.552
c 0
b 0
f 0
cc 1
nc 1
nop 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace SixtyEightPublishers\User\ForgotPassword\Control\ResetPassword;
6
7
use Nette;
8
use SixtyEightPublishers;
9
10
/**
11
 * @method void onSuccess(SixtyEightPublishers\User\ForgotPassword\DoctrineEntity\IPasswordRequest $request, string $rawPassword)
12
 * @method void onError(SixtyEightPublishers\User\ForgotPassword\DoctrineEntity\IPasswordRequest $request, SixtyEightPublishers\User\ForgotPassword\Exception\PasswordRequestProcessException $e)
13
 * @method void onFormCreation(Nette\Application\UI\Form $form)
14
 */
15
class ResetPasswordControl 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\DoctrineEntity\IPasswordRequest  */
20
	private $passwordRequest;
21
22
	/** @var \SixtyEightPublishers\User\Common\Logger\ILogger  */
23
	private $logger;
24
25
	/** @var \SixtyEightPublishers\User\ForgotPassword\Mail\IPasswordHasBeenResetEmail  */
26
	private $passwordHasBeenResetEmail;
27
28
	/** @var \SixtyEightPublishers\User\ForgotPassword\PasswordRequest\IPasswordRequestManager  */
29
	private $passwordRequestManager;
30
31
	/** @var callable[] */
32
	public $onSuccess = [];
33
34
	/** @var callable[] */
35
	public $onError = [];
36
37
	/** @var callable[] */
38
	public $onFormCreation = [];
39
40
	/**
41
	 * @param \SixtyEightPublishers\User\ForgotPassword\DoctrineEntity\IPasswordRequest         $passwordRequest
42
	 * @param \SixtyEightPublishers\User\Common\Logger\ILogger                                  $logger
43
	 * @param \SixtyEightPublishers\User\ForgotPassword\Mail\IPasswordHasBeenResetEmail         $passwordHasBeenResetEmail
44
	 * @param \SixtyEightPublishers\User\ForgotPassword\PasswordRequest\IPasswordRequestManager $passwordRequestManager
45
	 */
46
	public function __construct(
47
		SixtyEightPublishers\User\ForgotPassword\DoctrineEntity\IPasswordRequest $passwordRequest,
48
		SixtyEightPublishers\User\Common\Logger\ILogger $logger,
49
		SixtyEightPublishers\User\ForgotPassword\Mail\IPasswordHasBeenResetEmail $passwordHasBeenResetEmail,
50
		SixtyEightPublishers\User\ForgotPassword\PasswordRequest\IPasswordRequestManager $passwordRequestManager
51
	) {
52
		parent::__construct();
53
54
		$this->passwordRequest = $passwordRequest;
55
		$this->logger = $logger;
56
		$this->passwordHasBeenResetEmail = $passwordHasBeenResetEmail;
57
		$this->passwordRequestManager = $passwordRequestManager;
58
	}
59
60
	/**
61
	 * @return \Nette\Application\UI\Form
62
	 */
63 View Code Duplication
	protected function createComponentForm(): Nette\Application\UI\Form
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in 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...
64
	{
65
		$form = new Nette\Application\UI\Form();
66
67
		$form->setTranslator(SixtyEightPublishers\User\Common\Translator\PrefixedTranslator::createFromClassName(
68
			$this->getTranslator(),
69
			static::class
70
		));
71
72
		$form->addPassword('password', 'password.field')
73
			->setRequired('password.required')
0 ignored issues
show
Documentation introduced by
'password.required' is of type string, but the function expects a boolean.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
74
			->setAttribute('autocomplete', 'new-password');
0 ignored issues
show
Documentation introduced by
'new-password' is of type string, but the function expects a boolean.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
75
76
		$form->addProtection('protection.rule');
77
78
		$form->onSuccess[] = [$this, 'processForm'];
79
80
		$this->onFormCreation($form);
81
82
		$form->addSubmit('send', 'send.field');
83
84
		return $form;
85
	}
86
87
	/**
88
	 * @return void
89
	 */
90
	public function render(): void
91
	{
92
		$this->doRender();
93
	}
94
95
	/**
96
	 * @internal
97
	 *
98
	 * @param \Nette\Application\UI\Form $form
99
	 *
100
	 * @return void
101
	 */
102
	public function processForm(Nette\Application\UI\Form $form): void
103
	{
104
		try {
105
			$password = $form->values->password;
106
107
			$this->passwordRequestManager->reset($this->passwordRequest, $password);
108
109
			try {
110
				$this->passwordHasBeenResetEmail->send($this->passwordRequest->getUser());
111
				$this->logger->info(sprintf(
112
					'Mail %s was successfully sent to %s',
113
					get_class($this->passwordHasBeenResetEmail),
114
					$this->passwordRequest->getUser()->getEmail()
115
				));
116
			} catch (\Throwable $e) {
117
				$this->logger->error((string) $e);
118
			}
119
120
			$this->onSuccess($this->passwordRequest, $password);
121
		} catch (SixtyEightPublishers\User\ForgotPassword\Exception\PasswordRequestProcessException $e) {
122
			$this->onError($this->passwordRequest, $e);
123
		}
124
	}
125
}
126