refactor: extract StationSearchService

Audit item #8. StationController::index was ~100 lines doing the
haversine join, in-PHP filter/sort/group, search-row write, and
prediction call — well past the "thin orchestrator" line in
.claude/rules/architecture.md.

- App\Services\StationSearch\SearchCriteria — DTO (lat/lng/fuelType/
  radiusKm/sort)
- App\Services\StationSearch\SearchResult — DTO (stations, prices
  summary, reliability counts, prediction payload)
- App\Services\StationSearch\StationSearchService::search(criteria,
  ?user, ?ipHash) — owns the haversine query, the per-row reliability
  memoisation, sort, count, search-row logging, and the tier-gated
  prediction.

The controller now resolves coordinates, builds a SearchCriteria, calls
the service, and shapes the JSON response. Down from 154 → 71 lines.
Public API contract unchanged — all 15 StationController tests pass
without modification.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
This commit is contained in:
Ovidiu U
2026-04-30 08:20:23 +01:00
parent aff6dd1e0f
commit 895d55439b
4 changed files with 230 additions and 123 deletions

View File

@@ -0,0 +1,16 @@
<?php
namespace App\Services\StationSearch;
use App\Enums\FuelType;
final readonly class SearchCriteria
{
public function __construct(
public float $lat,
public float $lng,
public FuelType $fuelType,
public float $radiusKm,
public string $sort,
) {}
}