Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Clean code #7

Merged
merged 9 commits into from
Nov 2, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,8 @@
},
"config-plugin": {
"web": "web/*.php",
"params": "params.php"
"params": "params.php",
"params-console": "params-console.php"
},
"config-plugin-options": {
"source-directory": "config"
Expand All @@ -69,6 +70,10 @@
"tests/web/*.php"
],
"params": "params.php",
"params-console": [
"$params",
"tests/params-console.php"
],
"params-web": [
"$params",
"tests/params-web.php"
Expand Down
10 changes: 10 additions & 0 deletions config/params-console.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php

declare(strict_types=1);

return [
'console.aliases' => [
'@yii-user' => '@vendor/yii2-extensions/user',
'@yii-user/migration' => '@yii-user/src/Framework/Migration',
],
];
48 changes: 27 additions & 21 deletions config/params.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,29 +23,35 @@
'class' => RegisterController::class,
],
],
'app.events' => [
__DIR__ . '/events/AccountModel.php',
],
'app.menu.isguest' => [
[
'label' => \Yii::t('yii.user', 'Register'),
'url' => ['/register/index'],
'order' => 1,
],
[
'label' => \Yii::t('yii.user', 'Login'),
'url' => ['/login/index'],
'order' => 2,
'app.params' => [
'app.menu.isguest' => [
[
'label' => 'Register',
'url' => ['/register/index'],
'order' => 3,
'category' => 'yii.user',
],
[
'label' => 'Login',
'url' => ['/login/index'],
'order' => 4,
'category' => 'yii.user',
],
],
],
'app.menu.islogged' => [
[
'label' => \Yii::t('yii.user', 'Logout'),
'url' => ['/logout/index'],
'order' => 1,
'linkOptions' => [
'data-method' => 'post',
'app.menu.islogged' => [
[
'label' => 'Logout',
'url' => ['/logout/index'],
'order' => 1,
'category' => 'yii.user',
'linkOptions' => [
'data-method' => 'post',
],
],
],
],
'console.aliases' => [
'@yii-user' => '@vendor/yii2-extensions/user',
'@yii-user/migration' => '@yii-user/src/Framework/Migration',
],
];
4 changes: 3 additions & 1 deletion config/tests/params-web.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,7 @@
'class' => SiteController::class,
],
],
'icons' => '@npm/fortawesome--fontawesome-free/svgs/{family}/{name}.svg',
'app.params' => [
'icons' => '@npm/fortawesome--fontawesome-free/svgs/{family}/{name}.svg',
],
];
2 changes: 1 addition & 1 deletion config/tests/web/app.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,6 @@
'on beforeAction' => static function (): void {
$_SERVER['REMOTE_ADDR'] = '127.0.0.1';
},
'params' => $params,
'params' => $params['app.params'] ?? [],
'runtimePath' => dirname(__DIR__, 3) . '/tests/Support/Data/public/runtime',
];
4 changes: 2 additions & 2 deletions src/Framework/Migration/M211126112534Identity.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@

declare(strict_types=1);

use Yii\User\Framework\Migration\BaseMigration;
use Yii\User\Framework\Migration\Migration;

final class M211126112534Identity extends BaseMigration
final class M211126112534Identity extends Migration
{
public function up(): bool
{
Expand Down
4 changes: 2 additions & 2 deletions src/Framework/Migration/M211126112600Account.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@

declare(strict_types=1);

use Yii\User\Framework\Migration\BaseMigration;
use Yii\User\Framework\Migration\Migration;

final class M211126112600Account extends BaseMigration
final class M211126112600Account extends Migration
{
public function up(): bool
{
Expand Down
4 changes: 2 additions & 2 deletions src/Framework/Migration/M211126112801Token.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@

declare(strict_types=1);

use Yii\User\Framework\Migration\BaseMigration;
use Yii\User\Framework\Migration\Migration;

final class M211126112801Token extends BaseMigration
final class M211126112801Token extends Migration
{
public function up(): bool
{
Expand Down
4 changes: 2 additions & 2 deletions src/Framework/Migration/M211126112850SocialAccount.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@

declare(strict_types=1);

use Yii\User\Framework\Migration\BaseMigration;
use Yii\User\Framework\Migration\Migration;

final class M211126112850SocialAccount extends BaseMigration
final class M211126112850SocialAccount extends Migration
{
public function up(): bool
{
Expand Down
4 changes: 2 additions & 2 deletions src/Framework/Migration/M211126113053Profile.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@

declare(strict_types=1);

use Yii\User\Framework\Migration\BaseMigration;
use Yii\User\Framework\Migration\Migration;

final class M211126113053Profile extends BaseMigration
final class M211126113053Profile extends Migration
{
public function up(): bool
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,8 @@
namespace Yii\User\Framework\Migration;

use RuntimeException;
use yii\db\Migration;

class BaseMigration extends Migration
class Migration extends \yii\db\Migration
{
protected string $tableOptions = '';
protected string $restrict = 'RESTRICT';
Expand Down
11 changes: 9 additions & 2 deletions src/Model/Identity.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace Yii\User\Model;

use Exception;
use yii\base\NotSupportedException;
use yii\db\ActiveQueryInterface;
use yii\db\ActiveRecord;
Expand All @@ -27,18 +28,24 @@ public function getAccount(): ActiveQueryInterface
}

/**
* @param int|string $id the user ID to be looked for (primary key) in the database.
* @param int|string $id the user ID to be looked for (a primary key) in the database.
*/
public static function findIdentity($id): IdentityInterface|null
{
return self::findOne($id);
}

public static function findIdentityByAccessToken(mixed $token, mixed $type = null)
/**
* @throws NotSupportedException
*/
public static function findIdentityByAccessToken(mixed $token, mixed $type = null): void
{
throw new NotSupportedException('Method "' . __CLASS__ . '::findIdentityByAccessToken" is not implemented.');
}

/**
* @throws Exception
*/
public function generateAuthKey(): void
{
$this->setAttribute('auth_key', Random::string());
Expand Down
6 changes: 5 additions & 1 deletion src/UseCase/Controller.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,18 @@
namespace Yii\User\UseCase;

use Yii;
use yii\base\ExitException;
use yii\base\Model;
use yii\bootstrap5\ActiveForm;
use yii\web\Request;
use yii\web\Response;

class Controller extends \yii\web\Controller
{
protected function performAjaxValidation(Model $model)
/**
* @throws ExitException
*/
protected function performAjaxValidation(Model $model): void
{
if (
$this->request instanceof Request &&
Expand Down
4 changes: 2 additions & 2 deletions src/UseCase/Login/view/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@
'autofocus' => true,
'oninput' => 'this.setCustomValidity("")',
'oninvalid' => 'this.setCustomValidity("' . Yii::t('yii.user', 'Enter Login Here') . '")',
'required' => (YII_ENV === 'tests') ? false : true,
'required' => !((YII_ENV === 'tests')),
'tabindex' => '1',
]
) ?>
Expand All @@ -53,7 +53,7 @@
[
'oninput' => 'this.setCustomValidity("")',
'oninvalid' => 'this.setCustomValidity("' . Yii::t('yii.user', 'Enter Password Here') . '")',
'required' => (YII_ENV === 'tests') ? false : true,
'required' => !((YII_ENV === 'tests')),
'tabindex' => '2',
],
) ?>
Expand Down
44 changes: 16 additions & 28 deletions src/UseCase/Register/RegisterForm.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,55 +40,43 @@ public function rules(): array
{
return [
// acceptTerms rules
'acceptTermsRequired' => [
[
'accept_terms',
'compare',
'compareValue' => true,
'message' => Yii::t('yii.user', 'You must accept the terms and conditions.'),
],
// create_at only first register
'createdAtDefault' => ['created_at', 'default', 'value' => time()],
['created_at', 'default', 'value' => time()],
// confirmed_at only if $this->userModule->confirmation === false
'confirmedAtDefault' => [
'confirmed_at',
'default',
'value' => $this->userModule->confirmation ? 0 : time(),
],
['confirmed_at', 'default', 'value' => $this->userModule->confirmation ? 0 : time()],
// username rules
'usernameTrim' => ['username', 'trim'],
'usernameLength' => ['username', 'string', 'min' => 3, 'max' => 255],
'usernamePattern' => ['username', 'match', 'pattern' => $this->userModule->usernameRegex],
'usernameRequired' => ['username', 'required'],
'usernameUnique' => [
['username', 'trim'],
['username', 'string', 'min' => 3, 'max' => 255],
['username', 'match', 'pattern' => $this->userModule->usernameRegex],
['username', 'required'],
[
'username',
'unique',
'targetClass' => Account::class,
'message' => Yii::t('yii.user', 'This username has already been taken.'),
],
// email rules
'emailTrim' => ['email', 'trim'],
'emailRequired' => ['email', 'required'],
'emailPattern' => ['email', 'email'],
'emailUnique' => [
['email', 'trim'],
['email', 'required'],
['email', 'email'],
[
'email',
'unique',
'targetClass' => Account::class,
'message' => Yii::t('yii.user', 'This email address has already been taken.'),
],
// password rules
'passwordRequired' => [
'password',
'required',
'skipOnEmpty' => $this->userModule->generatePassword,
],
'passwordLength' => ['password', 'string', 'min' => 6, 'max' => 72],
['password', 'required', 'skipOnEmpty' => $this->userModule->generatePassword],
['password', 'string', 'min' => 6, 'max' => 72],
// password repeat rules
'passwordRepeatRequired' => [
'passwordRepeat',
'required',
'skipOnEmpty' => $this->userModule->generatePassword,
],
'passwordRepeatCompare' => [
['passwordRepeat', 'required', 'skipOnEmpty' => $this->userModule->generatePassword],
[
'passwordRepeat',
'compare',
'compareAttribute' => 'password',
Expand Down
2 changes: 2 additions & 0 deletions src/UseCase/Register/RegisterService.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,9 @@ public function __construct(
private readonly Profile $profile,
private readonly SocialAccount $socialAccount,
private readonly UserModule $userModule,
array $config = [],
) {
parent::__construct($config);
}

public function run(RegisterForm $registerForm): bool
Expand Down
10 changes: 5 additions & 5 deletions src/UseCase/Register/view/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@
'oninput' => 'this.setCustomValidity("")',
'oninvalid' => 'this.setCustomValidity("' . Yii::t('yii.user', 'Enter Email Here.') . '")',
'placeholder' => Yii::t('yii.user', 'Email'),
'required' => (YII_ENV === 'tests') ? false : true,
'required' => !((YII_ENV === 'tests')),
'tabindex' => '1',
],
)
Expand All @@ -59,7 +59,7 @@
'oninput' => 'this.setCustomValidity("")',
'oninvalid' => 'this.setCustomValidity("' . Yii::t('yii.user', 'Enter Username Here.') . '")',
'placeholder' => Yii::t('yii.user', 'Username'),
'required' => (YII_ENV === 'tests') ? false : true,
'required' => !((YII_ENV === 'tests')),
'tabindex' => '2',
],
)
Expand All @@ -71,7 +71,7 @@
'oninput' => 'this.setCustomValidity("")',
'oninvalid' => 'this.setCustomValidity("' . Yii::t('yii.user', 'Enter Password Here.') . '")',
'placeholder' => Yii::t('yii.user', 'Password'),
'required' => (YII_ENV === 'tests') ? false : true,
'required' => !((YII_ENV === 'tests')),
'tabindex' => '3',
]
)
Expand All @@ -82,7 +82,7 @@
'oninput' => 'this.setCustomValidity("")',
'oninvalid' => 'this.setCustomValidity("' . Yii::t('yii.user', 'Enter Password Here.') . '")',
'placeholder' => Yii::t('yii.user', 'Password'),
'required' => (YII_ENV === 'tests') ? false : true,
'required' => !((YII_ENV === 'tests')),
'tabindex' => '4',
]
)
Expand All @@ -93,7 +93,7 @@
[
'class' => 'form-check-input',
'oninvalid' => 'this.setCustomValidity("' . Yii::t('yii.user', 'You must accept the terms and conditions.') . '")',
'required' => (YII_ENV === 'tests') ? false : true,
'required' => !((YII_ENV === 'tests')),
'tabindex' => '5',
'template' => "<div class=\"form-check form-switch\">\n{input}\n{label}\n{error}\n{hint}\n</div>",
],
Expand Down