mirror of
https://github.com/elyby/accounts.git
synced 2025-05-31 14:11:46 +05:30
Cleanup code, improve typings
This commit is contained in:
@@ -59,6 +59,10 @@ class Account extends ActiveRecord {
|
||||
return '{{%accounts}}';
|
||||
}
|
||||
|
||||
public static function find(): AccountQuery {
|
||||
return new AccountQuery(self::class);
|
||||
}
|
||||
|
||||
public function behaviors(): array {
|
||||
return [
|
||||
TimestampBehavior::class,
|
||||
@@ -88,7 +92,7 @@ class Account extends ActiveRecord {
|
||||
$this->password_changed_at = time();
|
||||
}
|
||||
|
||||
public function getEmailActivations(): ActiveQuery {
|
||||
public function getEmailActivations(): EmailActivationQuery {
|
||||
return $this->hasMany(EmailActivation::class, ['account_id' => 'id']);
|
||||
}
|
||||
|
||||
|
||||
21
common/models/AccountQuery.php
Normal file
21
common/models/AccountQuery.php
Normal file
@@ -0,0 +1,21 @@
|
||||
<?php
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace common\models;
|
||||
|
||||
use yii\db\ActiveQuery;
|
||||
|
||||
/**
|
||||
* @see Account
|
||||
*/
|
||||
class AccountQuery extends ActiveQuery {
|
||||
|
||||
public function andWhereLogin(string $login): self {
|
||||
return $this->andWhere([$this->getLoginAttribute($login) => $login]);
|
||||
}
|
||||
|
||||
private function getLoginAttribute(string $login): string {
|
||||
return strpos($login, '@') ? 'email' : 'username';
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,4 +1,6 @@
|
||||
<?php
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace common\models;
|
||||
|
||||
use common\behaviors\DataBehavior;
|
||||
@@ -33,11 +35,15 @@ class EmailActivation extends ActiveRecord {
|
||||
public const TYPE_CURRENT_EMAIL_CONFIRMATION = 2;
|
||||
public const TYPE_NEW_EMAIL_CONFIRMATION = 3;
|
||||
|
||||
public static function tableName() {
|
||||
return '{{%email_activations}}';
|
||||
public static function tableName(): string {
|
||||
return 'email_activations';
|
||||
}
|
||||
|
||||
public function behaviors() {
|
||||
public static function find(): EmailActivationQuery {
|
||||
return new EmailActivationQuery(static::class);
|
||||
}
|
||||
|
||||
public function behaviors(): array {
|
||||
return [
|
||||
[
|
||||
'class' => TimestampBehavior::class,
|
||||
@@ -61,7 +67,7 @@ class EmailActivation extends ActiveRecord {
|
||||
];
|
||||
}
|
||||
|
||||
public function getAccount() {
|
||||
public function getAccount(): AccountQuery {
|
||||
return $this->hasOne(Account::class, ['id' => 'account_id']);
|
||||
}
|
||||
|
||||
@@ -82,7 +88,7 @@ class EmailActivation extends ActiveRecord {
|
||||
return new $classMap[$type]();
|
||||
}
|
||||
|
||||
public static function getClassMap() {
|
||||
public static function getClassMap(): array {
|
||||
return [
|
||||
self::TYPE_REGISTRATION_EMAIL_CONFIRMATION => confirmations\RegistrationConfirmation::class,
|
||||
self::TYPE_FORGOT_PASSWORD_KEY => confirmations\ForgotPassword::class,
|
||||
|
||||
17
common/models/EmailActivationQuery.php
Normal file
17
common/models/EmailActivationQuery.php
Normal file
@@ -0,0 +1,17 @@
|
||||
<?php
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace common\models;
|
||||
|
||||
use yii\db\ActiveQuery;
|
||||
|
||||
/**
|
||||
* @see EmailActivation
|
||||
*/
|
||||
class EmailActivationQuery extends ActiveQuery {
|
||||
|
||||
public function withType(int ...$typeId): self {
|
||||
return $this->andWhere(['type' => $typeId]);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,50 +0,0 @@
|
||||
<?php
|
||||
namespace common\models;
|
||||
|
||||
use yii\helpers\ArrayHelper;
|
||||
|
||||
class OauthScopeQuery {
|
||||
|
||||
private $scopes;
|
||||
|
||||
private $internal;
|
||||
|
||||
private $owner;
|
||||
|
||||
public function __construct(array $scopes) {
|
||||
$this->scopes = $scopes;
|
||||
}
|
||||
|
||||
public function onlyPublic(): self {
|
||||
$this->internal = false;
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function onlyInternal(): self {
|
||||
$this->internal = true;
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function usersScopes(): self {
|
||||
$this->owner = 'user';
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function machineScopes(): self {
|
||||
$this->owner = 'machine';
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function all(): array {
|
||||
return ArrayHelper::getColumn(array_filter($this->scopes, function($value) {
|
||||
$shouldCheckInternal = $this->internal !== null;
|
||||
$isInternalMatch = $value['internal'] === $this->internal;
|
||||
$shouldCheckOwner = $this->owner !== null;
|
||||
$isOwnerMatch = $value['owner'] === $this->owner;
|
||||
|
||||
return (!$shouldCheckInternal || $isInternalMatch)
|
||||
&& (!$shouldCheckOwner || $isOwnerMatch);
|
||||
}), 'value');
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,12 +1,19 @@
|
||||
<?php
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace common\models\confirmations;
|
||||
|
||||
use common\models\EmailActivation;
|
||||
use common\models\EmailActivationQuery;
|
||||
use yii\helpers\ArrayHelper;
|
||||
|
||||
class CurrentEmailConfirmation extends EmailActivation {
|
||||
|
||||
public function behaviors() {
|
||||
public static function find(): EmailActivationQuery {
|
||||
return parent::find()->withType(EmailActivation::TYPE_CURRENT_EMAIL_CONFIRMATION);
|
||||
}
|
||||
|
||||
public function behaviors(): array {
|
||||
return ArrayHelper::merge(parent::behaviors(), [
|
||||
'expirationBehavior' => [
|
||||
'repeatTimeout' => 6 * 60 * 60, // 6h
|
||||
@@ -15,7 +22,7 @@ class CurrentEmailConfirmation extends EmailActivation {
|
||||
]);
|
||||
}
|
||||
|
||||
public function init() {
|
||||
public function init(): void {
|
||||
parent::init();
|
||||
$this->type = EmailActivation::TYPE_CURRENT_EMAIL_CONFIRMATION;
|
||||
}
|
||||
|
||||
@@ -1,12 +1,19 @@
|
||||
<?php
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace common\models\confirmations;
|
||||
|
||||
use common\models\EmailActivation;
|
||||
use common\models\EmailActivationQuery;
|
||||
use yii\helpers\ArrayHelper;
|
||||
|
||||
class ForgotPassword extends EmailActivation {
|
||||
|
||||
public function behaviors() {
|
||||
public static function find(): EmailActivationQuery {
|
||||
return parent::find()->withType(EmailActivation::TYPE_FORGOT_PASSWORD_KEY);
|
||||
}
|
||||
|
||||
public function behaviors(): array {
|
||||
return ArrayHelper::merge(parent::behaviors(), [
|
||||
'expirationBehavior' => [
|
||||
'repeatTimeout' => 30 * 60,
|
||||
@@ -15,7 +22,7 @@ class ForgotPassword extends EmailActivation {
|
||||
]);
|
||||
}
|
||||
|
||||
public function init() {
|
||||
public function init(): void {
|
||||
parent::init();
|
||||
$this->type = EmailActivation::TYPE_FORGOT_PASSWORD_KEY;
|
||||
}
|
||||
|
||||
@@ -1,7 +1,10 @@
|
||||
<?php
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace common\models\confirmations;
|
||||
|
||||
use common\models\EmailActivation;
|
||||
use common\models\EmailActivationQuery;
|
||||
use yii\helpers\ArrayHelper;
|
||||
|
||||
/**
|
||||
@@ -10,7 +13,11 @@ use yii\helpers\ArrayHelper;
|
||||
*/
|
||||
class NewEmailConfirmation extends EmailActivation {
|
||||
|
||||
public function behaviors() {
|
||||
public static function find(): EmailActivationQuery {
|
||||
return parent::find()->withType(EmailActivation::TYPE_NEW_EMAIL_CONFIRMATION);
|
||||
}
|
||||
|
||||
public function behaviors(): array {
|
||||
return ArrayHelper::merge(parent::behaviors(), [
|
||||
'expirationBehavior' => [
|
||||
'repeatTimeout' => 5 * 60,
|
||||
@@ -21,7 +28,7 @@ class NewEmailConfirmation extends EmailActivation {
|
||||
]);
|
||||
}
|
||||
|
||||
public function init() {
|
||||
public function init(): void {
|
||||
parent::init();
|
||||
$this->type = EmailActivation::TYPE_NEW_EMAIL_CONFIRMATION;
|
||||
}
|
||||
|
||||
@@ -1,4 +1,6 @@
|
||||
<?php
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace common\models\confirmations;
|
||||
|
||||
use common\behaviors\DataBehavior;
|
||||
|
||||
@@ -1,11 +1,18 @@
|
||||
<?php
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace common\models\confirmations;
|
||||
|
||||
use common\models\EmailActivation;
|
||||
use common\models\EmailActivationQuery;
|
||||
|
||||
class RegistrationConfirmation extends EmailActivation {
|
||||
|
||||
public function init() {
|
||||
public static function find(): EmailActivationQuery {
|
||||
return parent::find()->withType(EmailActivation::TYPE_REGISTRATION_EMAIL_CONFIRMATION);
|
||||
}
|
||||
|
||||
public function init(): void {
|
||||
parent::init();
|
||||
$this->type = EmailActivation::TYPE_REGISTRATION_EMAIL_CONFIRMATION;
|
||||
}
|
||||
|
||||
@@ -1,7 +1,10 @@
|
||||
<?php
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace common\tests\unit\tasks;
|
||||
|
||||
use common\models\Account;
|
||||
use common\models\AccountQuery;
|
||||
use common\models\confirmations\CurrentEmailConfirmation;
|
||||
use common\tasks\SendCurrentEmailConfirmation;
|
||||
use common\tests\unit\TestCase;
|
||||
@@ -15,13 +18,14 @@ class SendCurrentEmailConfirmationTest extends TestCase {
|
||||
$account->email = 'mock@ely.by';
|
||||
$account->lang = 'id';
|
||||
|
||||
/** @var \Mockery\Mock|CurrentEmailConfirmation $confirmation */
|
||||
$confirmation = mock(CurrentEmailConfirmation::class)->makePartial();
|
||||
$accountQuery = $this->createMock(AccountQuery::class);
|
||||
$accountQuery->method('findFor')->willReturn($account);
|
||||
|
||||
$confirmation = $this->createPartialMock(CurrentEmailConfirmation::class, ['getAccount']);
|
||||
$confirmation->method('getAccount')->willReturn($accountQuery);
|
||||
$confirmation->key = 'ABCDEFG';
|
||||
$confirmation->shouldReceive('getAccount')->andReturn($account);
|
||||
|
||||
$result = SendCurrentEmailConfirmation::createFromConfirmation($confirmation);
|
||||
$this->assertInstanceOf(SendCurrentEmailConfirmation::class, $result);
|
||||
$this->assertSame('mock-username', $result->username);
|
||||
$this->assertSame('mock@ely.by', $result->email);
|
||||
$this->assertSame('ABCDEFG', $result->code);
|
||||
@@ -33,7 +37,7 @@ class SendCurrentEmailConfirmationTest extends TestCase {
|
||||
$task->email = 'mock@ely.by';
|
||||
$task->code = 'GFEDCBA';
|
||||
|
||||
$task->execute(mock(Queue::class));
|
||||
$task->execute($this->createMock(Queue::class));
|
||||
|
||||
$this->tester->canSeeEmailIsSent(1);
|
||||
/** @var \yii\swiftmailer\Message $email */
|
||||
|
||||
@@ -1,7 +1,10 @@
|
||||
<?php
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace common\tests\unit\tasks;
|
||||
|
||||
use common\models\Account;
|
||||
use common\models\AccountQuery;
|
||||
use common\models\confirmations\NewEmailConfirmation;
|
||||
use common\tasks\SendNewEmailConfirmation;
|
||||
use common\tests\unit\TestCase;
|
||||
@@ -14,14 +17,15 @@ class SendNewEmailConfirmationTest extends TestCase {
|
||||
$account->username = 'mock-username';
|
||||
$account->lang = 'id';
|
||||
|
||||
/** @var \Mockery\Mock|NewEmailConfirmation $confirmation */
|
||||
$confirmation = mock(NewEmailConfirmation::class)->makePartial();
|
||||
$accountQuery = $this->createMock(AccountQuery::class);
|
||||
$accountQuery->method('findFor')->willReturn($account);
|
||||
|
||||
$confirmation = $this->createPartialMock(NewEmailConfirmation::class, ['getAccount']);
|
||||
$confirmation->method('getAccount')->willReturn($accountQuery);
|
||||
$confirmation->setNewEmail('new-email@ely.by');
|
||||
$confirmation->key = 'ABCDEFG';
|
||||
$confirmation->shouldReceive('getAccount')->andReturn($account);
|
||||
$confirmation->shouldReceive('getNewEmail')->andReturn('new-email@ely.by');
|
||||
|
||||
$result = SendNewEmailConfirmation::createFromConfirmation($confirmation);
|
||||
$this->assertInstanceOf(SendNewEmailConfirmation::class, $result);
|
||||
$this->assertSame('mock-username', $result->username);
|
||||
$this->assertSame('new-email@ely.by', $result->email);
|
||||
$this->assertSame('ABCDEFG', $result->code);
|
||||
@@ -33,7 +37,7 @@ class SendNewEmailConfirmationTest extends TestCase {
|
||||
$task->email = 'mock@ely.by';
|
||||
$task->code = 'GFEDCBA';
|
||||
|
||||
$task->execute(mock(Queue::class));
|
||||
$task->execute($this->createMock(Queue::class));
|
||||
|
||||
$this->tester->canSeeEmailIsSent(1);
|
||||
/** @var \yii\swiftmailer\Message $email */
|
||||
|
||||
@@ -5,6 +5,7 @@ namespace common\tests\unit\tasks;
|
||||
|
||||
use common\emails\RendererInterface;
|
||||
use common\models\Account;
|
||||
use common\models\AccountQuery;
|
||||
use common\models\confirmations\ForgotPassword;
|
||||
use common\tasks\SendPasswordRecoveryEmail;
|
||||
use common\tests\unit\TestCase;
|
||||
@@ -24,10 +25,12 @@ class SendPasswordRecoveryEmailTest extends TestCase {
|
||||
$account->email = 'mock@ely.by';
|
||||
$account->lang = 'id';
|
||||
|
||||
/** @var \Mockery\Mock|ForgotPassword $confirmation */
|
||||
$confirmation = mock(ForgotPassword::class)->makePartial();
|
||||
$accountQuery = $this->createMock(AccountQuery::class);
|
||||
$accountQuery->method('findFor')->willReturn($account);
|
||||
|
||||
$confirmation = $this->createPartialMock(ForgotPassword::class, ['getAccount']);
|
||||
$confirmation->method('getAccount')->willReturn($accountQuery);
|
||||
$confirmation->key = 'ABCDEFG';
|
||||
$confirmation->shouldReceive('getAccount')->andReturn($account);
|
||||
|
||||
$result = SendPasswordRecoveryEmail::createFromConfirmation($confirmation);
|
||||
$this->assertSame('mock-username', $result->username);
|
||||
@@ -51,7 +54,7 @@ class SendPasswordRecoveryEmailTest extends TestCase {
|
||||
'link' => 'https://account.ely.by/recover-password/ABCDEFG',
|
||||
])->willReturn('mock-template');
|
||||
|
||||
$task->execute(mock(Queue::class));
|
||||
$task->execute($this->createMock(Queue::class));
|
||||
|
||||
$this->tester->canSeeEmailIsSent(1);
|
||||
/** @var \yii\swiftmailer\Message $email */
|
||||
|
||||
@@ -5,6 +5,7 @@ namespace common\tests\unit\tasks;
|
||||
|
||||
use common\emails\RendererInterface;
|
||||
use common\models\Account;
|
||||
use common\models\AccountQuery;
|
||||
use common\models\confirmations\RegistrationConfirmation;
|
||||
use common\tasks\SendRegistrationEmail;
|
||||
use common\tests\unit\TestCase;
|
||||
@@ -24,10 +25,12 @@ class SendRegistrationEmailTest extends TestCase {
|
||||
$account->email = 'mock@ely.by';
|
||||
$account->lang = 'ru';
|
||||
|
||||
/** @var \Mockery\Mock|RegistrationConfirmation $confirmation */
|
||||
$confirmation = mock(RegistrationConfirmation::class)->makePartial();
|
||||
$accountQuery = $this->createMock(AccountQuery::class);
|
||||
$accountQuery->method('findFor')->willReturn($account);
|
||||
|
||||
$confirmation = $this->createPartialMock(RegistrationConfirmation::class, ['getAccount']);
|
||||
$confirmation->method('getAccount')->willReturn($accountQuery);
|
||||
$confirmation->key = 'ABCDEFG';
|
||||
$confirmation->shouldReceive('getAccount')->andReturn($account);
|
||||
|
||||
$result = SendRegistrationEmail::createFromConfirmation($confirmation);
|
||||
$this->assertSame('mock-username', $result->username);
|
||||
|
||||
Reference in New Issue
Block a user