Cleanup code, improve typings

This commit is contained in:
ErickSkrauch
2019-12-13 22:27:13 +03:00
parent 830a17612b
commit d9ef27b745
28 changed files with 189 additions and 225 deletions

View File

@@ -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']);
}

View 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';
}
}

View File

@@ -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,

View 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]);
}
}

View File

@@ -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');
}
}

View File

@@ -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;
}

View File

@@ -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;
}

View File

@@ -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;
}

View File

@@ -1,4 +1,6 @@
<?php
declare(strict_types=1);
namespace common\models\confirmations;
use common\behaviors\DataBehavior;

View File

@@ -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;
}