feat: improved error handling of xml parsers and loaders
This commit is contained in:
1 parent
0f6b73340e
commit
4f02529b15
9 files changed
+257
-42
No files matched your search
@@ -4,6 +4,7 @@ namespace App\BusProNet\XmlLoader;
|
||||
|
||||
use App\BusProNet\Model\Hotel;
|
||||
use App\BusProNet\Model\Travel;
|
||||
use App\Exception\HotelNotFoundException;
|
||||
use Psr\Cache\InvalidArgumentException;
|
||||
use Symfony\Component\DomCrawler\Crawler;
|
||||
use Symfony\Contracts\Cache\ItemInterface;
|
||||
@@ -47,11 +48,15 @@ class HotelLoader extends AbstractLoader
|
||||
return null;
|
||||
}
|
||||
|
||||
public function loadById(int $id, ?string $filename = 'hotel.xml'): ?Hotel
|
||||
public function loadById(int $id, ?string $filename = 'hotel.xml'): Hotel
|
||||
{
|
||||
$hotels = $this->loadAll($filename);
|
||||
|
||||
return $hotels[$id] ?? null;
|
||||
if (false === isset($hotels[$id])) {
|
||||
throw new HotelNotFoundException($id);
|
||||
}
|
||||
|
||||
return $hotels[$id];
|
||||
}
|
||||
|
||||
private function loadXml(?string $filename = 'hotel.xml'): Crawler
|
||||
@@ -78,7 +83,13 @@ class HotelLoader extends AbstractLoader
|
||||
public function patchHotelDetails(Travel $travel): void
|
||||
{
|
||||
$hotelId = $travel->hotelId;
|
||||
$hotel = $this->loadById($hotelId);
|
||||
$travel->hotel = $hotel;
|
||||
try {
|
||||
$hotel = $this->loadById($hotelId);
|
||||
$travel->hotel = $hotel;
|
||||
} catch (HotelNotFoundException $e) {
|
||||
// If hotel details can't be loaded, leave travel->hotel as null
|
||||
// This allows the travel to be processed even if hotel details are missing
|
||||
$travel->hotel = null;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user