mirror of
https://github.com/jikan-me/jikan-rest.git
synced 2025-02-20 11:23:35 +08:00
refactor
This commit is contained in:
parent
7e4fce1b8a
commit
b5b2c48b0a
@ -107,6 +107,16 @@ class Handler extends ExceptionHandler
|
||||
], 400);
|
||||
}
|
||||
|
||||
if ($e instanceof ValidationException) {
|
||||
return response()
|
||||
->json([
|
||||
'status' => 400,
|
||||
'type' => 'BadRequestException',
|
||||
'message' => $e->getMessage(),
|
||||
'error' => null
|
||||
], 400);
|
||||
}
|
||||
|
||||
// BadResponseException from Jikan PHP API
|
||||
// This is basically the response MyAnimeList returns to Jikan
|
||||
if ($e instanceof BadResponseException) {
|
||||
|
22
app/Helpers/ScraperHelper.php
Normal file
22
app/Helpers/ScraperHelper.php
Normal file
@ -0,0 +1,22 @@
|
||||
<?php
|
||||
|
||||
namespace App\Helpers;
|
||||
|
||||
use App\Providers\SerializerFactory;
|
||||
|
||||
class ScraperHelper
|
||||
{
|
||||
|
||||
/**
|
||||
* @param object|array $response
|
||||
* @return array
|
||||
*/
|
||||
public static function getSerializedJSON(object|array $response): array
|
||||
{
|
||||
return \json_decode(
|
||||
SerializerFactory::createV4()
|
||||
->serialize($response, 'json'),
|
||||
true
|
||||
);
|
||||
}
|
||||
}
|
@ -2,6 +2,7 @@
|
||||
|
||||
namespace App\Http\Controllers\V4DB;
|
||||
|
||||
use App\Helpers\ScraperHelper;
|
||||
use App\Http\HttpHelper;
|
||||
use App\Http\HttpResponse;
|
||||
use App\Http\QueryBuilder\Scraper\Query;
|
||||
@ -711,6 +712,16 @@ class MangaController extends Controller
|
||||
*/
|
||||
public function reviews(Request $request, int $id)
|
||||
{
|
||||
$validation = $this->validate($request, [
|
||||
'page' => 'nullable|numeric',
|
||||
'sort' => 'nullable|string'
|
||||
]);
|
||||
|
||||
$page = $request->get('page') ?? 1;
|
||||
$spoilers = $request->get('spoilers') ?? false;
|
||||
$preliminary = $request->get('preliminary') ?? false;
|
||||
$sort = $request->get('sort');
|
||||
|
||||
$results = DB::table($this->getRouteTable($request))
|
||||
->where('request_hash', $this->fingerprint)
|
||||
->get();
|
||||
@ -720,21 +731,18 @@ class MangaController extends Controller
|
||||
|| $this->isExpired($request, $results)
|
||||
) {
|
||||
|
||||
$response = (new ScraperHandler(
|
||||
'MangaReviews',
|
||||
MangaReviewsRequest::class,
|
||||
(new QueryResolver())
|
||||
->setNewQuery(new Query('id', $id))
|
||||
->setNewQuery(new Query('page', $request->get('page') ?? 1))
|
||||
->setNewQuery(new Query(
|
||||
'sort',
|
||||
$request->get('sort') ?? Constants::REVIEWS_SORT_MOST_VOTED,
|
||||
new ValidationTypeEnum([Constants::REVIEWS_SORT_MOST_VOTED, Constants::REVIEWS_SORT_NEWEST, Constants::REVIEWS_SORT_OLDEST]),
|
||||
new BadRequestException('Invalid sort for reviews. Please refer to the documentation: https://docs.api.jikan.moe/')
|
||||
))
|
||||
->setNewQuery(new Query('spoilers', $request->get('spoilers') ?? false))
|
||||
->setNewQuery(new Query('preliminary', $request->get('preliminary') ?? false))
|
||||
))->getSerializedJSON();
|
||||
$response = ScraperHelper::getSerializedJSON(
|
||||
app('JikanParser')
|
||||
->getMangaReviews(
|
||||
new MangaReviewsRequest(
|
||||
$id,
|
||||
$page,
|
||||
in_array($sort, [Constants::REVIEWS_SORT_MOST_VOTED, Constants::REVIEWS_SORT_NEWEST, Constants::REVIEWS_SORT_OLDEST]) ? $sort : Constants::REVIEWS_SORT_MOST_VOTED,
|
||||
$spoilers,
|
||||
$preliminary
|
||||
)
|
||||
)
|
||||
);
|
||||
|
||||
$results = $this->updateCache($request, $results, $response);
|
||||
}
|
||||
|
@ -1,53 +0,0 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\QueryBuilder\Scraper;
|
||||
|
||||
use App\Http\Validation\Validation;
|
||||
use Symfony\Component\HttpFoundation\Exception\BadRequestException;
|
||||
|
||||
class Query
|
||||
{
|
||||
/**
|
||||
* @var string
|
||||
*/
|
||||
private string $name;
|
||||
/**
|
||||
* @var mixed
|
||||
*/
|
||||
private mixed $value;
|
||||
|
||||
/**
|
||||
* @throws \Exception
|
||||
*/
|
||||
public function __construct(
|
||||
string $name,
|
||||
mixed $value,
|
||||
?Validation $validation = null,
|
||||
?\Exception $exceptionOnValidationFail = null,
|
||||
)
|
||||
{
|
||||
$this->name = $name;
|
||||
|
||||
if ($validation !== null && !$validation->validate($value)) {
|
||||
throw $exceptionOnValidationFail ?? new BadRequestException("Invalid value supplied for {$name}. Please refer to the documentation: https://docs.api.jikan.moe/");
|
||||
}
|
||||
|
||||
$this->value = $value;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return string
|
||||
*/
|
||||
public function getName(): string
|
||||
{
|
||||
return $this->name;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return mixed
|
||||
*/
|
||||
public function getValue(): mixed
|
||||
{
|
||||
return $this->value;
|
||||
}
|
||||
}
|
@ -1,33 +0,0 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\QueryBuilder\Scraper;
|
||||
|
||||
class QueryResolver
|
||||
{
|
||||
/**
|
||||
* @var array
|
||||
*/
|
||||
private array $queries = [];
|
||||
|
||||
/**
|
||||
* @param Query $query
|
||||
* @return $this
|
||||
*/
|
||||
public function setNewQuery(Query $query): self
|
||||
{
|
||||
$this->queries[] = $query;
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array
|
||||
*/
|
||||
public function getQueryValuesAsArray(): array
|
||||
{
|
||||
$values = [];
|
||||
foreach ($this->queries as $query) {
|
||||
$values[] = $query->getValue();
|
||||
}
|
||||
return $values;
|
||||
}
|
||||
}
|
@ -1,43 +0,0 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\QueryBuilder\Scraper;
|
||||
|
||||
use App\Providers\SerializerFactory;
|
||||
|
||||
class ScraperHandler
|
||||
{
|
||||
/**
|
||||
* @var mixed
|
||||
*/
|
||||
private mixed $response;
|
||||
|
||||
/**
|
||||
* @param string $getterFuncName
|
||||
* @param string $requestResolverClassName
|
||||
* @param QueryResolver $queryResolver
|
||||
*/
|
||||
public function __construct(string $getterFuncName, string $requestResolverClassName, QueryResolver $queryResolver)
|
||||
{
|
||||
$getterFuncName = "get".ucfirst(strtolower($getterFuncName));
|
||||
|
||||
$this->response = app('JikanParser')
|
||||
->$getterFuncName(
|
||||
new $requestResolverClassName(
|
||||
...$queryResolver->getQueryValuesAsArray()
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @return array
|
||||
*/
|
||||
public function getSerializedJSON(): array
|
||||
{
|
||||
return \json_decode(
|
||||
SerializerFactory::createV4()
|
||||
->serialize($this->response, 'json'),
|
||||
true
|
||||
);
|
||||
}
|
||||
}
|
@ -1,18 +0,0 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Validation;
|
||||
|
||||
abstract class Validation implements ValidationInterface
|
||||
{
|
||||
/**
|
||||
* @var array|int|float|string
|
||||
*/
|
||||
public array|int|float|string $criteria;
|
||||
|
||||
/**
|
||||
* @param array|int|float|string $criteria
|
||||
*/
|
||||
public function __construct(array|int|float|string $criteria) {
|
||||
$this->criteria = $criteria;
|
||||
}
|
||||
}
|
@ -1,12 +0,0 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Validation;
|
||||
|
||||
interface ValidationInterface
|
||||
{
|
||||
/**
|
||||
* @param mixed $value
|
||||
* @return bool
|
||||
*/
|
||||
public function validate(mixed $value):bool;
|
||||
}
|
@ -1,14 +0,0 @@
|
||||
<?php
|
||||
namespace App\Http\Validation;
|
||||
|
||||
class ValidationTypeEnum extends Validation
|
||||
{
|
||||
/**
|
||||
* @param mixed $value
|
||||
* @return bool
|
||||
*/
|
||||
public function validate(mixed $value) : bool
|
||||
{
|
||||
return in_array($value, $this->criteria);
|
||||
}
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user