Drop usage of goaop, replace implementation with events

This commit is contained in:
ErickSkrauch
2023-11-20 04:39:13 +01:00
parent 2bc83f39cf
commit 16877d502d
27 changed files with 365 additions and 778 deletions

View File

@@ -0,0 +1,85 @@
<?php
declare(strict_types=1);
namespace api\eventListeners;
use api\controllers\AuthenticationController;
use api\controllers\SignupController;
use api\modules\accounts\actions;
use Closure;
use Yii;
use yii\base\ActionEvent;
use yii\base\BootstrapInterface;
use yii\base\Controller;
use yii\base\Event;
final class LogMetricsToStatsd implements BootstrapInterface {
public function bootstrap($app): void {
Event::on(Controller::class, Controller::EVENT_BEFORE_ACTION, Closure::fromCallable([$this, 'beforeAction']));
Event::on(Controller::class, Controller::EVENT_AFTER_ACTION, Closure::fromCallable([$this, 'afterAction']));
}
private function beforeAction(ActionEvent $event): void {
$prefix = $this->getPrefix($event);
if ($prefix === null) {
return;
}
Yii::$app->statsd->inc($prefix . '.attempt');
}
private function afterAction(ActionEvent $event): void {
$prefix = $this->getPrefix($event);
if ($prefix === null) {
return;
}
$result = $event->result;
if (isset($result['success'])) {
if ($result['success']) {
Yii::$app->statsd->inc($prefix . '.success');
} else {
$errors = $result['errors'];
Yii::$app->statsd->inc($prefix . '.' . $errors[array_key_first($errors)]);
}
}
}
private function getPrefix(ActionEvent $event): ?string {
$action = $event->action;
switch (get_class($action)) {
case actions\AcceptRulesAction::class: return 'accounts.acceptRules';
case actions\ChangeEmailAction::class: return 'accounts.changeEmail';
case actions\ChangeLanguageAction::class: return 'accounts.switchLanguage';
case actions\ChangePasswordAction::class: return 'accounts.changePassword';
case actions\ChangeUsernameAction::class: return 'accounts.changeUsername';
case actions\EnableTwoFactorAuthAction::class: return 'accounts.enableTwoFactorAuth';
case actions\DisableTwoFactorAuthAction::class: return 'accounts.disableTwoFactorAuth';
case actions\EmailVerificationAction::class: return 'accounts.sendEmailVerification';
case actions\NewEmailVerificationAction::class: return 'accounts.sendNewEmailVerification';
}
$controller = $action->controller;
if ($controller instanceof SignupController) {
switch ($action->id) {
case 'index': return 'signup.register';
case 'repeatMessage': return 'signup.repeatEmail';
case 'confirm': return 'signup.confirmEmail';
}
}
if ($controller instanceof AuthenticationController) {
switch ($action->id) {
case 'login': return 'authentication.login';
case 'logout': return 'authentication.logout';
case 'forgotPassword': return 'authentication.forgotPassword';
case 'recoverPassword': return 'authentication.recoverPassword';
case 'refreshToken': return 'authentication.renew';
}
}
return null;
}
}

View File

@@ -0,0 +1,153 @@
<?php
declare(strict_types=1);
namespace api\eventListeners;
use api\controllers\AuthenticationController;
use api\controllers\SignupController;
use api\modules\accounts\actions\ChangeEmailAction;
use api\modules\accounts\actions\EmailVerificationAction;
use api\modules\accounts\actions\NewEmailVerificationAction;
use api\modules\accounts\controllers\DefaultController;
use Closure;
use yii\base\ActionEvent;
use yii\base\BootstrapInterface;
use yii\base\Controller;
use yii\base\Event;
use yii\web\Response;
final class MockDataResponse implements BootstrapInterface {
public function bootstrap($app): void {
Event::on(Controller::class, Controller::EVENT_BEFORE_ACTION, Closure::fromCallable([$this, 'beforeAction']));
}
private function beforeAction(ActionEvent $event): void {
$result = $this->getResponse($event);
if ($result === null) {
return;
}
$response = $event->action->controller->response;
$response->format = Response::FORMAT_JSON;
$response->data = $result;
$event->handled = true;
$event->isValid = false;
}
private function getResponse(ActionEvent $event): ?array {
$action = $event->action;
$controller = $action->controller;
$request = $controller->request;
if ($controller instanceof SignupController && $action->id === 'index') {
$email = $request->post('email');
if ($email === 'let-me-register@ely.by') {
return ['success' => true];
}
}
if ($controller instanceof SignupController && $action->id === 'repeatMessage') {
$email = $request->post('email');
if ($email === 'let-me-register@ely.by' || $email === 'let-me-repeat@ely.by') {
return ['success' => true];
}
}
if ($controller instanceof SignupController && $action->id === 'confirm') {
$key = $request->post('key');
if ($key === 'LETMEIN') {
return [
'success' => true,
'access_token' => 'dummy_token',
'expires_in' => time() + 60,
];
}
}
if ($controller instanceof AuthenticationController && $action->id === 'forgotPassword') {
$login = $request->post('login');
if ($login === 'let-me-recover@ely.by') {
return [
'success' => true,
'data' => [
'canRepeatIn' => time() + 60,
'repeatFrequency' => 60,
],
];
}
}
if ($controller instanceof AuthenticationController && $action->id === 'recoverPassword') {
$key = $request->post('key');
if ($key === 'LETMEIN') {
return [
'success' => true,
'access_token' => 'dummy_token',
'expires_in' => time() + 60,
];
}
}
if ($controller instanceof DefaultController && $action->id === 'get') {
$httpAuth = $request->getHeaders()->get('authorization');
if ($httpAuth === 'Bearer dummy_token') {
return [
'id' => 1,
'uuid' => 'f63cd5e1-680f-4c2d-baa2-cc7bb174b71a',
'username' => 'dummy',
'isOtpEnabled' => false,
'registeredAt' => time(),
'lang' => 'en',
'elyProfileLink' => 'http://ely.by/u1',
'email' => 'let-me-register@ely.by',
'isActive' => true,
'passwordChangedAt' => time(),
'hasMojangUsernameCollision' => false,
'shouldAcceptRules' => false,
];
}
}
if ($action instanceof EmailVerificationAction) {
$httpAuth = $request->getHeaders()->get('authorization');
if ($httpAuth === 'Bearer dummy_token') {
$password = $request->post('password');
if (empty($password)) {
return [
'success' => false,
'errors' => [
'password' => 'error.password_required',
],
];
}
return [
'success' => true,
];
}
}
if ($action instanceof NewEmailVerificationAction) {
$key = $request->post('key');
if ($key === 'LETMEIN') {
return [
'success' => true,
];
}
}
if ($action instanceof ChangeEmailAction) {
$key = $request->post('key');
if ($key === 'LETMEIN') {
return [
'success' => true,
'email' => 'brand-new-email@ely.by',
];
}
}
return null;
}
}