refactor: migrate sales API from LMI to Boris

This commit is contained in:
Rico
2026-03-02 00:15:33 +01:00
parent ae915989cc
commit a3adfe1987

View File

@@ -2,6 +2,7 @@
namespace App\Http\Controllers; namespace App\Http\Controllers;
use RuntimeException;
use Illuminate\Support\Facades\Cache; use Illuminate\Support\Facades\Cache;
use Illuminate\Support\Facades\Http; use Illuminate\Support\Facades\Http;
use Illuminate\Support\Facades\Log; use Illuminate\Support\Facades\Log;
@@ -16,31 +17,62 @@ class SaleController extends Controller
} }
try { try {
$sales = Cache::remember('sales_data', 60 * 60 * 8, static function () { $sales = Cache::remember('sales_data', 60 * 60 * 8, function () {
$lmi_api_key = config('services.lmi.api_key'); $borisUrl = rtrim((string) config('services.boris.url'), '/');
$borisApiKey = (string) config('services.boris.api_key');
$response = Http::withHeaders([ $headers = [
'Authorization' => 'Bearer ' . $lmi_api_key, 'X-API-Key' => $borisApiKey,
])->get('https://lmi.orianna.dev/api/lol-sales'); ];
$response = $response->json(); $championSalesResponse = Http::withHeaders($headers)->get($borisUrl.'/api/champions/sales');
$skinSalesResponse = Http::withHeaders($headers)->get($borisUrl.'/api/skins/sales');
if (! isset($response['champion_sales']) || $response['champion_sales'] === null) { if (! $championSalesResponse->successful() || ! $skinSalesResponse->successful()) {
Log::error('LMI has broken'); Log::error('Boris sales request failed.', [
return abort(503, 'Trying to access array offset on value of type null'); 'champions_status' => $championSalesResponse->status(),
'skins_status' => $skinSalesResponse->status(),
]);
throw new RuntimeException('Boris sales request failed.');
} }
return $response; $championSales = $championSalesResponse->json();
$skinSales = $skinSalesResponse->json();
if (
! isset($championSales['items']) || ! is_array($championSales['items']) ||
! isset($skinSales['items']) || ! is_array($skinSales['items'])
) {
Log::error('Boris sales payload is invalid.');
throw new RuntimeException('Boris sales payload is invalid.');
}
return [
'champion_sales' => array_map(static fn (array $item): array => [
'item_id' => $item['id'],
'rp' => $item['sale_rp'],
'percent_off' => $item['percent_off'],
], $championSales['items']),
'skin_sales' => array_map(static fn (array $item): array => [
'item_id' => $item['id'],
'rp' => $item['sale_rp'],
'percent_off' => $item['percent_off'],
], $skinSales['items']),
];
}); });
} catch (\Exception $exception) { } catch (\Exception $exception) {
if ($exception->getMessage() === 'Trying to access array offset on value of type null') { if (
Log::error('LMI has broken'); $exception->getMessage() === 'Boris sales request failed.' ||
$exception->getMessage() === 'Boris sales payload is invalid.'
) {
abort(503, 'Sorry, the Sale Rotation is currently under maintenance. Please try again later.'); abort(503, 'Sorry, the Sale Rotation is currently under maintenance. Please try again later.');
} else { }
Log::error('An error occurred while trying to fetch the Sale Rotation', ['error' => $exception->getMessage()]); Log::error('An error occurred while trying to fetch the Sale Rotation', ['error' => $exception->getMessage()]);
abort(500, 'Sorry, an error occurred while trying to fetch the Sale Rotation. Please try again later.'); abort(500, 'Sorry, an error occurred while trying to fetch the Sale Rotation. Please try again later.');
} }
}
return view('sales.index', ['sales' => $sales]); return view('sales.index', ['sales' => $sales]);
} }