-
-
Notifications
You must be signed in to change notification settings - Fork 29
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
174 additions
and
42 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,31 +1,31 @@ | ||
<?php | ||
namespace V3; | ||
|
||
use V3\Feature\Register\Register; | ||
use V3\Feature\Register\RegisterError; | ||
|
||
class Controller | ||
{ | ||
public function setupEndpoints(): void | ||
{ | ||
Peripheral::addGetRoute('/v3/register', function () { | ||
if (Peripheral::httpRequestQuery('secret') !== 'v3') { | ||
abort(404); | ||
} | ||
|
||
return Peripheral::renderTwig('view', [ | ||
'stylesheetUrl' => Peripheral::resourceUrl('css/v3.css'), | ||
'logoUrl' => '/img/v3/logo.svg', | ||
return Web\Register::registerView([ | ||
RegisterError::UsernameTaken, | ||
RegisterError::EmailTaken, | ||
RegisterError::PasswordNotSecure, | ||
RegisterError::PasswordNotRepeated, | ||
RegisterError::TermsNotAccepted, | ||
]); | ||
}); | ||
|
||
Peripheral::addPostRoute('/v3/createAccount', function () { | ||
$requestModel = new RequestModel( | ||
Peripheral::addPostRoute('/v3/register', function () { | ||
$register = new Register(new Peripheral()); | ||
return Web\Register::registerView($register->register( | ||
Peripheral::httpRequestField('registerLogin'), | ||
Peripheral::httpRequestField('registerPassword'), | ||
Peripheral::httpRequestField('registerPasswordRepeat'), | ||
Peripheral::httpRequestField('registerEmail'), | ||
); | ||
|
||
return Peripheral::renderTwig('view', [ | ||
'stylesheetUrl' => Peripheral::resourceUrl('css/v3.css'), | ||
]); | ||
Peripheral::httpRequestField('registerTermsAccepted'), | ||
)); | ||
}); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
<?php | ||
namespace V3\Feature\Register; | ||
|
||
readonly class Register | ||
{ | ||
public function __construct(private RegisterGate $gate) | ||
{ | ||
} | ||
|
||
/** | ||
* @return RegisterError[] | ||
*/ | ||
public function register( | ||
string $login, | ||
string $password, | ||
string $passwordRepeat, | ||
string $email, | ||
bool $termsAccepted, | ||
): array | ||
{ | ||
$errors = []; | ||
if (!$this->gate->isUsernameAvailable($login)) { | ||
$errors[] = RegisterError::UsernameTaken; | ||
} | ||
if (!$this->gate->isEmailAvailable($password)) { | ||
$errors[] = RegisterError::EmailTaken; | ||
} | ||
if (\mb_strLen($password) < 3) { | ||
$errors[] = RegisterError::PasswordNotSecure; | ||
} | ||
if ($password !== $passwordRepeat) { | ||
$errors[] = RegisterError::PasswordNotRepeated; | ||
} | ||
if (!$termsAccepted) { | ||
$errors[] = RegisterError::TermsNotAccepted; | ||
} | ||
if (empty($errors)) { | ||
$this->gate->createUserAndLogin($login, $email, $password); | ||
} | ||
return $errors; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
<?php | ||
namespace V3\Feature\Register; | ||
|
||
enum RegisterError | ||
{ | ||
case UsernameTaken; | ||
case EmailTaken; | ||
case PasswordNotSecure; | ||
case PasswordNotRepeated; | ||
case TermsNotAccepted; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
<?php | ||
namespace V3\Feature\Register; | ||
|
||
interface RegisterGate | ||
{ | ||
public function isUsernameAvailable(string $login): bool; | ||
|
||
public function isEmailAvailable(string $password): bool; | ||
|
||
public function createUserAndLogin(string $login, string $email, string $password): void; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
<?php | ||
namespace V3\Web; | ||
|
||
use V3\Feature\Register\RegisterError; | ||
use V3\Peripheral; | ||
|
||
class Register | ||
{ | ||
public static function registerView(array $errors): string | ||
{ | ||
return Peripheral::renderTwig('view', [ | ||
'stylesheetUrl' => Peripheral::resourceUrl('css/v3.css'), | ||
'logoUrl' => '/img/v3/logo.svg', | ||
'postSubmitUrl' => '/v3/register', | ||
'registerErrorUsernameTaken' => \in_array(RegisterError::UsernameTaken, $errors), | ||
'registerErrorEmailTaken' => \in_array(RegisterError::EmailTaken, $errors), | ||
'registerErrorPasswordNotSecure' => \in_array(RegisterError::PasswordNotSecure, $errors), | ||
'registerErrorPasswordNotRepeated' => \in_array(RegisterError::PasswordNotRepeated, $errors), | ||
'registerErrorTermsNotAccepted' => \in_array(RegisterError::TermsNotAccepted, $errors), | ||
]); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters