mirror of
				https://github.com/elyby/accounts.git
				synced 2025-05-31 14:11:46 +05:30 
			
		
		
		
	Add additional tests cases, implemented custom error formatter
This commit is contained in:
		@@ -0,0 +1,54 @@
 | 
			
		||||
<?php
 | 
			
		||||
declare(strict_types=1);
 | 
			
		||||
 | 
			
		||||
namespace api\modules\mojang\behaviors;
 | 
			
		||||
 | 
			
		||||
use Closure;
 | 
			
		||||
use Yii;
 | 
			
		||||
use yii\base\Behavior;
 | 
			
		||||
use yii\base\Event;
 | 
			
		||||
use yii\web\NotFoundHttpException;
 | 
			
		||||
use yii\web\Response;
 | 
			
		||||
use yii\web\UnauthorizedHttpException;
 | 
			
		||||
 | 
			
		||||
final class ServiceErrorConverterBehavior extends Behavior {
 | 
			
		||||
 | 
			
		||||
    public function events(): array {
 | 
			
		||||
        return [
 | 
			
		||||
            Response::EVENT_BEFORE_SEND => Closure::fromCallable([$this, 'beforeSend']),
 | 
			
		||||
        ];
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    private function beforeSend(Event $event): void {
 | 
			
		||||
        /** @var Response $response */
 | 
			
		||||
        $response = $event->sender;
 | 
			
		||||
        $data = $response->data;
 | 
			
		||||
        if ($data === null || !isset($data['status'])) {
 | 
			
		||||
            return;
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        $request = Yii::$app->request;
 | 
			
		||||
        $type = $data['type'];
 | 
			
		||||
        switch ($type) {
 | 
			
		||||
            case UnauthorizedHttpException::class:
 | 
			
		||||
                $response->data = [
 | 
			
		||||
                    'path' => '/' . $request->getPathInfo(),
 | 
			
		||||
                    'errorType' => 'UnauthorizedOperationException',
 | 
			
		||||
                    'error' => 'UnauthorizedOperationException',
 | 
			
		||||
                    'errorMessage' => 'Unauthorized',
 | 
			
		||||
                    'developerMessage' => 'Unauthorized',
 | 
			
		||||
                ];
 | 
			
		||||
                break;
 | 
			
		||||
            case NotFoundHttpException::class:
 | 
			
		||||
                $response->data = [
 | 
			
		||||
                    'path' => '/' . $request->getPathInfo(),
 | 
			
		||||
                    'errorType' => 'NOT_FOUND',
 | 
			
		||||
                    'error' => 'NOT_FOUND',
 | 
			
		||||
                    'errorMessage' => 'The server has not found anything matching the request URI',
 | 
			
		||||
                    'developerMessage' => 'The server has not found anything matching the request URI',
 | 
			
		||||
                ];
 | 
			
		||||
                break;
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
}
 | 
			
		||||
@@ -4,6 +4,7 @@ declare(strict_types=1);
 | 
			
		||||
namespace api\modules\mojang\controllers;
 | 
			
		||||
 | 
			
		||||
use api\controllers\Controller;
 | 
			
		||||
use api\modules\mojang\behaviors\ServiceErrorConverterBehavior;
 | 
			
		||||
use api\rbac\Permissions;
 | 
			
		||||
use common\components\SkinsSystemApi;
 | 
			
		||||
use Exception;
 | 
			
		||||
@@ -12,6 +13,7 @@ use Yii;
 | 
			
		||||
use yii\filters\AccessControl;
 | 
			
		||||
use yii\filters\VerbFilter;
 | 
			
		||||
use yii\helpers\ArrayHelper;
 | 
			
		||||
use yii\web\NotFoundHttpException;
 | 
			
		||||
use function Ramsey\Uuid\v3;
 | 
			
		||||
 | 
			
		||||
final class ServicesController extends Controller {
 | 
			
		||||
@@ -44,9 +46,16 @@ final class ServicesController extends Controller {
 | 
			
		||||
        ]);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    public function init(): void {
 | 
			
		||||
        parent::init();
 | 
			
		||||
        $this->response->attachBehavior('errorFormatter', ServiceErrorConverterBehavior::class);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    public function actionProfile(SkinsSystemApi $skinsSystemApi): array {
 | 
			
		||||
        /** @var \common\models\Account $account at this point null value isn't possible */
 | 
			
		||||
        $account = Yii::$app->user->identity->getAccount();
 | 
			
		||||
        if ($account === null) {
 | 
			
		||||
            throw new NotFoundHttpException();
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        try {
 | 
			
		||||
            $textures = $skinsSystemApi->textures($account->username);
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user