fix: remove redundant insurance hydration, cache snapshots and files map
This commit is contained in:
@@ -7,7 +7,6 @@ namespace App\Service;
|
||||
use App\BusProNet\ApiClient;
|
||||
use App\BusProNet\Exception\ApiClientException;
|
||||
use App\BusProNet\Model\BaseData;
|
||||
use App\BusProNet\Model\Insurance;
|
||||
use App\BusProNet\Model\Notification;
|
||||
use App\BusProNet\Model\ServiceAvailabilityResponse;
|
||||
use App\BusProNet\Model\Travel;
|
||||
@@ -38,6 +37,9 @@ class TravelDataService
|
||||
private const int AVAILABILITY_CACHE_TTL = 600;
|
||||
private const int MUTABILITY_CACHE_TTL = 300;
|
||||
|
||||
/** @var array<int, array<string, mixed>>|null */
|
||||
private ?array $filesMapCache = null;
|
||||
|
||||
public function __construct(
|
||||
private readonly TravelLoader $travelLoader,
|
||||
private readonly HotelLoader $hotelLoader,
|
||||
@@ -147,7 +149,6 @@ class TravelDataService
|
||||
|
||||
if (null !== $travel) {
|
||||
$this->patchInsuranceData($travel);
|
||||
$this->hydrateInsurancePackageRelationships($travel->insurances);
|
||||
}
|
||||
|
||||
return $travel;
|
||||
@@ -275,6 +276,9 @@ class TravelDataService
|
||||
'travelId' => $result->id,
|
||||
]);
|
||||
|
||||
$this->patchInsuranceData($result);
|
||||
$this->travelSnapshotService->upsertFromTravel($result);
|
||||
|
||||
return $result;
|
||||
} catch (ApiClientException $e) {
|
||||
$this->logger->error('Failed to load travel data from API', [
|
||||
@@ -508,6 +512,10 @@ class TravelDataService
|
||||
*/
|
||||
public function generateFilesMap(): array
|
||||
{
|
||||
if (null !== $this->filesMapCache) {
|
||||
return $this->filesMapCache;
|
||||
}
|
||||
|
||||
$mapping = [];
|
||||
|
||||
try {
|
||||
@@ -537,6 +545,8 @@ class TravelDataService
|
||||
'count' => count($mapping),
|
||||
]);
|
||||
|
||||
$this->filesMapCache = $mapping;
|
||||
|
||||
return $mapping;
|
||||
}
|
||||
|
||||
@@ -792,35 +802,4 @@ class TravelDataService
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Reconstructs containedInsurances arrays for insurance packages.
|
||||
*
|
||||
* Insurance packages reference other insurances via containedInsuranceIds.
|
||||
* After deserialization, the containedInsurances array is empty due to
|
||||
* circular reference prevention. This method rebuilds those relationships.
|
||||
*
|
||||
* @param array<Insurance> $insurances All insurances including packages
|
||||
*/
|
||||
private function hydrateInsurancePackageRelationships(array $insurances): void
|
||||
{
|
||||
// Build lookup map of all insurances by ID (includes complementary insurances)
|
||||
$insuranceById = [];
|
||||
foreach ($insurances as $insurance) {
|
||||
$insuranceById[$insurance->id] = $insurance;
|
||||
}
|
||||
|
||||
// Reconstruct containedInsurances for each package
|
||||
foreach ($insurances as $insurance) {
|
||||
if (!$insurance->package || empty($insurance->containedInsuranceIds)) {
|
||||
continue;
|
||||
}
|
||||
|
||||
$insurance->containedInsurances = [];
|
||||
foreach ($insurance->containedInsuranceIds as $containedId) {
|
||||
if (isset($insuranceById[$containedId])) {
|
||||
$insurance->containedInsurances[] = $insuranceById[$containedId];
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user