ResetPasswordControl   A
last analyzed

Complexity

Total Complexity 8

Size/Duplication

Total Lines 132
Duplicated Lines 15.15 %

Coupling/Cohesion

Components 1
Dependencies 5

Importance

Changes 0
Metric Value
wmc 8
lcom 1
cbo 5
dl 20
loc 132
rs 10
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 13 1
A setAutoLogin() 0 4 1
A render() 0 5 1
A createComponentForm() 20 20 1
A processForm() 0 28 4

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
3
declare(strict_types=1);
4
5
namespace SixtyEightPublishers\User\ForgotPassword\Control\ResetPassword;
6
7
use Throwable;
8
use Nette\Security\User;
9
use Nette\Application\UI\Form;
10
use SixtyEightPublishers\SmartNetteComponent\UI\Control;
11
use SixtyEightPublishers\User\Common\Logger\LoggerInterface;
12
use SixtyEightPublishers\TranslationBridge\TranslatorAwareTrait;
13
use SixtyEightPublishers\TranslationBridge\TranslatorAwareInterface;
14
use SixtyEightPublishers\User\ForgotPassword\Entity\PasswordRequestInterface;
15
use SixtyEightPublishers\User\ForgotPassword\Mail\PasswordHasBeenResetEmailInterface;
16
use SixtyEightPublishers\User\ForgotPassword\Exception\PasswordRequestProcessException;
17
use SixtyEightPublishers\User\ForgotPassword\PasswordRequest\PasswordRequestManagerInterface;
18
19
/**
20
 * @method void onSuccess(PasswordRequestInterface $request)
21
 * @method void onError(PasswordRequestInterface $request, PasswordRequestProcessException $e)
22
 * @method void onFormCreation(Form $form)
23
 */
24
class ResetPasswordControl extends Control implements TranslatorAwareInterface
25
{
26
	use TranslatorAwareTrait;
27
28
	/** @var \SixtyEightPublishers\User\ForgotPassword\Entity\PasswordRequestInterface  */
29
	private $passwordRequest;
30
31
	/** @var \SixtyEightPublishers\User\Common\Logger\LoggerInterface  */
32
	private $logger;
33
34
	/** @var \SixtyEightPublishers\User\ForgotPassword\Mail\PasswordHasBeenResetEmailInterface  */
35
	private $passwordHasBeenResetEmail;
36
37
	/** @var \SixtyEightPublishers\User\ForgotPassword\PasswordRequest\PasswordRequestManagerInterface  */
38
	private $passwordRequestManager;
39
40
	/** @var \Nette\Security\User  */
41
	private $user;
42
43
	/** @var bool  */
44
	private $autoLogin = FALSE;
45
46
	/** @var callable[] */
47
	public $onSuccess = [];
48
49
	/** @var callable[] */
50
	public $onError = [];
51
52
	/** @var callable[] */
53
	public $onFormCreation = [];
54
55
	/**
56
	 * @param \SixtyEightPublishers\User\ForgotPassword\Entity\PasswordRequestInterface                 $passwordRequest
57
	 * @param \SixtyEightPublishers\User\Common\Logger\LoggerInterface                                  $logger
58
	 * @param \SixtyEightPublishers\User\ForgotPassword\Mail\PasswordHasBeenResetEmailInterface         $passwordHasBeenResetEmail
59
	 * @param \SixtyEightPublishers\User\ForgotPassword\PasswordRequest\PasswordRequestManagerInterface $passwordRequestManager
60
	 * @param \Nette\Security\User                                                                      $user
61
	 */
62
	public function __construct(
63
		PasswordRequestInterface $passwordRequest,
64
		LoggerInterface $logger,
65
		PasswordHasBeenResetEmailInterface $passwordHasBeenResetEmail,
66
		PasswordRequestManagerInterface $passwordRequestManager,
67
		User $user
68
	) {
69
		$this->passwordRequest = $passwordRequest;
70
		$this->logger = $logger;
71
		$this->passwordHasBeenResetEmail = $passwordHasBeenResetEmail;
72
		$this->passwordRequestManager = $passwordRequestManager;
73
		$this->user = $user;
74
	}
75
76
	/**
77
	 * @param bool $autoLogin
78
	 *
79
	 * @return void
80
	 */
81
	public function setAutoLogin(bool $autoLogin): void
82
	{
83
		$this->autoLogin = $autoLogin;
84
	}
85
86
	/**
87
	 * @return void
88
	 */
89
	public function render(): void
90
	{
91
		$this->template->setTranslator($this->getPrefixedTranslator());
92
		$this->doRender();
93
	}
94
95
	/**
96
	 * @return \Nette\Application\UI\Form
97
	 */
98 View Code Duplication
	protected function createComponentForm(): 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...
99
	{
100
		$form = new Form();
101
102
		$form->setTranslator($this->getPrefixedTranslator());
103
104
		$form->addPassword('password', 'password.field')
105
			->setRequired('password.required')
106
			->setHtmlAttribute('autocomplete', 'new-password');
107
108
		$form->addProtection('protection.rule');
109
110
		$form->onSuccess[] = [$this, 'processForm'];
111
112
		$this->onFormCreation($form);
113
114
		$form->addSubmit('send', 'send.field');
115
116
		return $form;
117
	}
118
119
	/**
120
	 * @internal
121
	 *
122
	 * @param \Nette\Application\UI\Form $form
123
	 *
124
	 * @return void
125
	 * @throws \Nette\Security\AuthenticationException
126
	 */
127
	public function processForm(Form $form): void
128
	{
129
		try {
130
			$request = $this->passwordRequest;
131
			$password = $form->values->password;
132
133
			$this->passwordRequestManager->reset($request, $password);
134
135
			try {
136
				$this->passwordHasBeenResetEmail->send($request->getUser());
137
				$this->logger->info(sprintf(
138
					'Mail %s was successfully sent to %s',
139
					get_class($this->passwordHasBeenResetEmail),
140
					$request->getUser()->getEmail()
141
				));
142
			} catch (Throwable $e) {
143
				$this->logger->error((string) $e);
144
			}
145
146
			if ($this->autoLogin) {
147
				$this->user->login($request->getUser()->getUsername(), $password);
148
			}
149
150
			$this->onSuccess($this->passwordRequest);
151
		} catch (PasswordRequestProcessException $e) {
152
			$this->onError($this->passwordRequest, $e);
153
		}
154
	}
155
}
156