SignInControl   A
last analyzed

Complexity

Total Complexity 10

Size/Duplication

Total Lines 126
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 0
Metric Value
wmc 10
lcom 1
cbo 1
dl 0
loc 126
rs 10
c 0
b 0
f 0

6 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A setUsernamePrefix() 0 4 1
A setExpiration() 0 4 1
A render() 0 5 1
A createComponentForm() 0 24 1
A processForm() 0 24 5
1
<?php
2
3
declare(strict_types=1);
4
5
namespace SixtyEightPublishers\User\Authentication\Control\SignIn;
6
7
use Nette\Security\User;
8
use Nette\Application\UI\Form;
9
use Nette\Security\IAuthenticator;
10
use Nette\Security\AuthenticationException;
11
use SixtyEightPublishers\SmartNetteComponent\UI\Control;
12
use SixtyEightPublishers\User\Common\Logger\LoggerInterface;
13
use SixtyEightPublishers\TranslationBridge\TranslatorAwareTrait;
14
use SixtyEightPublishers\TranslationBridge\TranslatorAwareInterface;
15
use SixtyEightPublishers\User\Authentication\Authenticator\AuthenticatorMount;
16
17
/**
18
 * @method void onLoggedIn()
19
 * @method void onAuthenticationFail(AuthenticationException $e)
20
 * @method void onFormCreation(Form $form)
21
 */
22
final class SignInControl extends Control implements TranslatorAwareInterface
23
{
24
	use TranslatorAwareTrait;
25
26
	/** @var \Nette\Security\User  */
27
	private $user;
28
29
	/** @var \SixtyEightPublishers\User\Common\Logger\LoggerInterface  */
30
	private $logger;
31
32
	/** @var NULL|string */
33
	private $usernamePrefix;
34
35
	/** @var string|int|\DateTimeInterface */
36
	private $expiration;
37
38
	/** @var callable[]  */
39
	public $onLoggedIn = [];
40
41
	/** @var callable[]  */
42
	public $onAuthenticationFail = [];
43
44
	/** @var callable[]  */
45
	public $onFormCreation = [];
46
47
	/**
48
	 * @param \Nette\Security\User                                     $user
49
	 * @param \SixtyEightPublishers\User\Common\Logger\LoggerInterface $logger
50
	 */
51
	public function __construct(User $user, LoggerInterface $logger)
52
	{
53
		$this->user = $user;
54
		$this->logger = $logger;
55
	}
56
57
	/**
58
	 * Use with AuthenticatorMount
59
	 *
60
	 * @param string $usernamePrefix
61
	 *
62
	 * @return void
63
	 */
64
	public function setUsernamePrefix(string $usernamePrefix): void
65
	{
66
		$this->usernamePrefix = $usernamePrefix;
67
	}
68
69
	/**
70
	 * @param \DateTimeInterface|int|string $expiration
71
	 *
72
	 * @return void
73
	 */
74
	public function setExpiration($expiration): void
75
	{
76
		$this->expiration = $expiration;
77
	}
78
79
	/**
80
	 * @return void
81
	 */
82
	public function render(): void
83
	{
84
		$this->template->setTranslator($this->getPrefixedTranslator());
85
		$this->doRender();
86
	}
87
88
	/**
89
	 * @return \Nette\Application\UI\Form
90
	 */
91
	protected function createComponentForm(): Form
92
	{
93
		$form = new Form();
94
95
		$form->setTranslator($this->getPrefixedTranslator());
96
97
		$form->addText('username', 'username.field')
98
			->setRequired('username.required')
99
			->setHtmlAttribute('autocomplete', 'username');
100
101
		$form->addPassword('password', 'password.field')
102
			->setRequired('password.required')
103
			->setHtmlAttribute('autocomplete', 'current-password');
104
105
		$form->addProtection('protection.rule');
106
107
		$form->onSuccess[] = [$this, 'processForm'];
108
109
		$this->onFormCreation($form);
110
111
		$form->addSubmit('login', 'login.field');
112
113
		return $form;
114
	}
115
116
	/**
117
	 * @internal
118
	 *
119
	 * @param \Nette\Application\UI\Form $form
120
	 *
121
	 * @return void
122
	 */
123
	public function processForm(Form $form): void
124
	{
125
		try {
126
			$username = $form->values->username;
127
			$password = $form->values->password;
128
129
			if (!empty($this->usernamePrefix)) {
130
				$username = $this->usernamePrefix . AuthenticatorMount::SEPARATOR . $username;
131
			}
132
133
			if (NULL !== $this->expiration) {
134
				$this->user->setExpiration($this->expiration);
135
			}
136
137
			$this->user->login($username, $password);
138
			$this->onLoggedIn();
139
		} catch (AuthenticationException $e) {
0 ignored issues
show
Bug introduced by
The class Nette\Security\AuthenticationException does not exist. Did you forget a USE statement, or did you not list all dependencies?

Scrutinizer analyzes your composer.json/composer.lock file if available to determine the classes, and functions that are defined by your dependencies.

It seems like the listed class was neither found in your dependencies, nor was it found in the analyzed files in your repository. If you are using some other form of dependency management, you might want to disable this analysis.

Loading history...
140
			if (IAuthenticator::FAILURE === $e->getCode()) {
141
				$this->logger->error((string) $e);
142
			}
143
144
			$this->onAuthenticationFail($e);
145
		}
146
	}
147
}
148