80 lines
2.6 KiB
PHP
80 lines
2.6 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace App\Service;
|
|
|
|
use App\BusProNet\Model\Travel;
|
|
use App\BusProNet\XmlLoader\HotelLoader;
|
|
use App\BusProNet\XmlLoader\InsuranceLoader;
|
|
use App\BusProNet\XmlLoader\PickupLoader;
|
|
use Psr\Log\LoggerInterface;
|
|
|
|
/**
|
|
* Enriches Travel objects with supplementary data from XML loaders.
|
|
*
|
|
* Handles two distinct enrichment passes:
|
|
* - XML enrichment (pickups + hotel details) for travels freshly loaded from XML
|
|
* - Insurance patching applied to every local travel, replacing any stale snapshot data
|
|
* with freshly parsed, fully-hydrated Insurance objects from the XML loader
|
|
*/
|
|
class TravelEnrichmentService
|
|
{
|
|
public function __construct(
|
|
private readonly PickupLoader $pickupLoader,
|
|
private readonly HotelLoader $hotelLoader,
|
|
private readonly InsuranceLoader $insuranceLoader,
|
|
private readonly LoggerInterface $logger,
|
|
) {
|
|
}
|
|
|
|
/**
|
|
* Add pickup and hotel details to a travel freshly loaded from XML.
|
|
*
|
|
* Returns true when both loaders succeed. Returns false on any loader failure
|
|
* (the failure is logged as a warning) so callers can decide whether to persist
|
|
* the partially-enriched travel as a snapshot.
|
|
*
|
|
* @param Travel $travel The travel object to enrich in-place
|
|
*
|
|
* @return bool True if enrichment completed fully; false if any loader failed
|
|
*/
|
|
public function enrichFromXml(Travel $travel): bool
|
|
{
|
|
try {
|
|
$this->pickupLoader->patchPickupsDetails($travel);
|
|
$this->hotelLoader->patchHotelDetails($travel);
|
|
|
|
return true;
|
|
} catch (\Exception $e) {
|
|
$this->logger->warning('Failed to enrich travel data', [
|
|
'travelId' => $travel->id,
|
|
'error' => $e->getMessage(),
|
|
]);
|
|
|
|
return false;
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Replace travel insurances with fresh data from the XML insurance loader.
|
|
*
|
|
* Always overwrites any existing insurances — including stale snapshot data — so
|
|
* callers receive fully-hydrated Insurance objects with resolved containedInsurances
|
|
* as populated by InsuranceParser at parse time.
|
|
*
|
|
* @param Travel $travel The travel object whose insurances will be replaced
|
|
*/
|
|
public function patchInsurances(Travel $travel): void
|
|
{
|
|
try {
|
|
$travel->insurances = $this->insuranceLoader->loadAll();
|
|
} catch (\Exception $e) {
|
|
$this->logger->warning('Failed to load insurance data', [
|
|
'travelId' => $travel->id,
|
|
'error' => $e->getMessage(),
|
|
]);
|
|
}
|
|
}
|
|
}
|