2023-01-22 19:47:21 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace App\Features;
|
|
|
|
|
2023-04-28 18:45:59 +01:00
|
|
|
use App\Contracts\AnimeRepository;
|
2023-01-22 19:47:21 +00:00
|
|
|
use App\Contracts\RequestHandler;
|
|
|
|
use App\Dto\QueryAnimeSeasonCommand;
|
|
|
|
use App\Enums\AnimeSeasonEnum;
|
2023-01-23 20:07:41 +00:00
|
|
|
use App\Enums\AnimeTypeEnum;
|
2023-01-22 19:47:21 +00:00
|
|
|
use App\Http\Resources\V4\AnimeCollection;
|
|
|
|
use App\Support\CachedData;
|
2023-01-23 20:07:41 +00:00
|
|
|
use Illuminate\Contracts\Database\Query\Builder;
|
2023-01-22 19:47:21 +00:00
|
|
|
use Illuminate\Http\JsonResponse;
|
|
|
|
use Illuminate\Support\Carbon;
|
|
|
|
use Symfony\Component\HttpFoundation\Exception\BadRequestException;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @template TRequest of QueryAnimeSeasonCommand
|
|
|
|
* @implements RequestHandler<TRequest, JsonResponse>
|
|
|
|
*/
|
|
|
|
abstract class QueryAnimeSeasonHandlerBase implements RequestHandler
|
|
|
|
{
|
2023-04-28 18:45:59 +01:00
|
|
|
public function __construct(protected readonly AnimeRepository $repository)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2023-01-22 19:47:21 +00:00
|
|
|
/**
|
|
|
|
* @param QueryAnimeSeasonCommand $request
|
|
|
|
* @return JsonResponse
|
|
|
|
*/
|
|
|
|
public function handle($request): JsonResponse
|
|
|
|
{
|
2023-04-28 18:45:59 +01:00
|
|
|
$requestParams = collect($request->all());
|
|
|
|
$type = $requestParams->has("filter") ? $request->filter : null;
|
2023-10-24 09:29:24 +05:00
|
|
|
$results = $this->getSeasonItems($request, $type);
|
2023-05-01 16:52:52 +01:00
|
|
|
// apply sfw, kids and unapproved filters
|
|
|
|
/** @noinspection PhpUndefinedMethodInspection */
|
|
|
|
$results = $results->filter($requestParams);
|
2023-01-22 19:47:21 +00:00
|
|
|
$results = $results->paginate($request->limit, ["*"], null, $request->page);
|
|
|
|
|
|
|
|
$animeCollection = new AnimeCollection($results);
|
|
|
|
$response = $animeCollection->response();
|
|
|
|
|
|
|
|
return $response->addJikanCacheFlags($request->getFingerPrint(), CachedData::from($animeCollection->collection));
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param TRequest $request
|
2023-01-23 20:07:41 +00:00
|
|
|
* @param ?AnimeTypeEnum $type
|
|
|
|
* @return Builder
|
2023-01-22 19:47:21 +00:00
|
|
|
*/
|
2023-10-24 09:27:35 +05:00
|
|
|
protected abstract function getSeasonItems($request, ?AnimeTypeEnum $type): Builder;
|
2023-01-22 19:47:21 +00:00
|
|
|
|
|
|
|
protected function getSeasonRange(int $year, AnimeSeasonEnum $season): array
|
|
|
|
{
|
|
|
|
[$monthStart, $monthEnd] = match ($season->value) {
|
|
|
|
AnimeSeasonEnum::winter()->value => [1, 3],
|
|
|
|
AnimeSeasonEnum::spring()->value => [4, 6],
|
|
|
|
AnimeSeasonEnum::summer()->value => [7, 9],
|
|
|
|
AnimeSeasonEnum::fall()->value => [10, 12],
|
|
|
|
default => throw new BadRequestException('Invalid season supplied'),
|
|
|
|
};
|
|
|
|
|
2023-06-11 06:58:24 +05:00
|
|
|
$from = Carbon::createFromDate($year, $monthStart, 1)
|
|
|
|
->setTimezone(new \DateTimeZone('UTC'))
|
|
|
|
->setTime(0, 0);
|
2023-01-27 22:19:31 +00:00
|
|
|
|
2023-06-11 06:58:24 +05:00
|
|
|
$to = Carbon::createFromDate($year, $monthEnd, 1)
|
|
|
|
->setTimezone(new \DateTimeZone('UTC'))
|
|
|
|
->setTime(0,0);
|
2023-01-27 22:19:31 +00:00
|
|
|
|
2023-01-22 19:47:21 +00:00
|
|
|
return [
|
2023-01-27 22:19:31 +00:00
|
|
|
$from,
|
|
|
|
$to
|
2023-01-22 19:47:21 +00:00
|
|
|
];
|
|
|
|
}
|
|
|
|
}
|