Skip to content

Commit

Permalink
refactor: replace ApiResultsResponse with specialized response class
Browse files Browse the repository at this point in the history
  • Loading branch information
chuckadams committed Oct 22, 2024
1 parent 0b081f4 commit db68c0f
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 105 deletions.
89 changes: 0 additions & 89 deletions app/DTO/ApiResultsResponse.php

This file was deleted.

16 changes: 15 additions & 1 deletion app/Data/WpOrg/Themes/QueryThemesResponse.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,26 @@
namespace App\Data\WpOrg\Themes;

use App\Data\WpOrg\PageInfo;
use Spatie\LaravelData\Attributes\MapOutputName;
use Spatie\LaravelData\Data;
use stdClass;

// WORK IN PROGRESS: ThemeController is still using

class QueryThemesResponse extends Data
{
/**
* @param PageInfo $pageInfo
* @param array<string,mixed> $themes // TODO: use Collection<ThemeResponse>
*/
public function __construct(
#[MapOutputName('info')]
public readonly PageInfo $pageInfo,
// TODO
public readonly array $themes,
) {}

/** for API version 1.0 responses only -- do not use this otherwise! */
public function toStdClass(): stdClass {
return (object) ['info' => $this->pageInfo->toArray(), 'themes' => $this->themes];
}
}
2 changes: 2 additions & 0 deletions app/Data/WpOrg/Themes/ThemeResponse.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
use Carbon\CarbonImmutable;
use Spatie\LaravelData\Data;

// WORK IN PROGRESS: not used yet. many fields need to be made nullable or Optional.

class ThemeResponse extends Data
{
/**
Expand Down
40 changes: 25 additions & 15 deletions app/Http/Controllers/API/WpOrg/Themes/ThemeController.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,16 @@
namespace App\Http\Controllers\API\WpOrg\Themes;

use App\Data\WpOrg\Themes\QueryThemesRequest;
use App\Data\WpOrg\Themes\QueryThemesResponse;
use App\Data\WpOrg\Themes\ThemeInformationRequest;
use App\Http\Controllers\Controller;
use App\DTO\ApiResultsResponse;
use Illuminate\Http\Request;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Response;
use Illuminate\Support\Facades\DB;

use function Safe\json_decode;

class ThemeController extends Controller
{
public function info(Request $request): JsonResponse|Response
Expand All @@ -19,23 +21,31 @@ public function info(Request $request): JsonResponse|Response
$response = match ($action) {
'query_themes' => $this->doQueryThemes(QueryThemesRequest::fromRequest($request)),
'theme_information' => $this->doThemeInformation(ThemeInformationRequest::fromRequest($request)),
'hot_tags' => $this->doHotTags($request),
'feature_list' => $this->doFeatureList($request),
'hot_tags' => $this->doHotTags(),
'feature_list' => $this->doFeatureList(),
default => $this->unknownAction()
};
return $this->sendResponse($response);
}

private function doQueryThemes(QueryThemesRequest $req): ApiResultsResponse
private function doQueryThemes(QueryThemesRequest $req): QueryThemesResponse
{
$page = $req->page;
$perPage = $req->per_page;
$skip = ($page - 1) * $perPage;

$themes = DB::table('themes')->skip($skip)->take($perPage)->get()->toArray();
// TODO: process search and other filters
$themes = DB::table('themes')
->skip($skip)
->take($perPage)
->get()
->map(fn($theme) => json_decode($theme->metadata))
->toArray();

$total = DB::table('themes')->count();

return new ApiResultsResponse('themes', $themes, $page, $perPage, $total);
$pageInfo = ['page' => $page, 'pages' => (int)ceil($total / $perPage), 'results' => $total];
return QueryThemesResponse::from(['pageInfo' => $pageInfo, 'themes' => $themes]);
}

/** @return array<string, mixed> */
Expand All @@ -45,13 +55,13 @@ private function doThemeInformation(ThemeInformationRequest $req): array
}

/** @return array<string, mixed> */
private function doHotTags(Request $request): array
private function doHotTags(): array
{
return ['error' => 'not implemented'];
}

/** @return array<string, mixed> */
private function doFeatureList(Request $request): array
private function doFeatureList(): array
{
return ['error' => 'not implemented'];
}
Expand All @@ -65,23 +75,23 @@ private function unknownAction(): Response
);
}


/**
* Send response based on API version.
*
* @param mixed $data
* @param array<string,mixed>|QueryThemesResponse $response
* @param int $statusCode
* @return Response|JsonResponse
*/
private function sendResponse($data, $statusCode = 200): JsonResponse|Response
private function sendResponse(array|QueryThemesResponse $response, int $statusCode = 200): JsonResponse|Response
{
$version = request()->route('version');
if ($version === '1.0') {
if (is_object($data) && method_exists($data, 'toStdClass')) {
$data = $data->toStdClass();
if (is_object($response) && method_exists($response, 'toStdClass')) {
/** @noinspection CallableParameterUseCaseInTypeContextInspection */
$response = $response->toStdClass();
}
return response(serialize((object) $data), $statusCode);
return response(serialize((object) $response), $statusCode);
}
return response()->json($data, $statusCode);
return response()->json($response, $statusCode);
}
}

0 comments on commit db68c0f

Please sign in to comment.