-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathPasswordPrompt.php
More file actions
41 lines (36 loc) · 1.22 KB
/
Copy pathPasswordPrompt.php
File metadata and controls
41 lines (36 loc) · 1.22 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
<?php
declare(strict_types=1);
namespace Scriptor\Boot\Cli;
/**
* Interactive TTY password reader with confirmation.
*
* Echo is suppressed via {@see Console::promptSecret()}; this class
* adds the confirmation loop and the "passwords don't match" retry.
* Up to three attempts before giving up; an empty string is returned
* on give-up so the caller's validatePassword() rejects it.
*/
final class PasswordPrompt
{
private const MAX_ATTEMPTS = 3;
public function __construct(private readonly Console $console)
{
}
public function readWithConfirmation(int $minLength): string
{
if (! $this->console->stdinIsTty()) {
return '';
}
for ($attempt = 1; $attempt <= self::MAX_ATTEMPTS; $attempt++) {
$first = $this->console->promptSecret(
"Enter admin password ({$minLength}+ chars): "
);
$second = $this->console->promptSecret('Confirm admin password: ');
if ($first === $second && $first !== '') {
return $first;
}
$this->console->errln('Passwords did not match, try again.');
}
$this->console->errln('Too many failed attempts.');
return '';
}
}