feat: parser and loader for insurance data
This commit is contained in:
1 parent
582fb9b52d
commit
9d44197246
3 files changed
+325
No files matched your search
@@ -0,0 +1,80 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\BusProNet\XmlLoader;
|
||||
|
||||
use App\BusProNet\Model\Insurance;
|
||||
use App\BusProNet\XmlParser\InsuranceParser;
|
||||
use League\Flysystem\FilesystemOperator;
|
||||
use Psr\Cache\InvalidArgumentException;
|
||||
use Symfony\Component\DomCrawler\Crawler;
|
||||
use Symfony\Contracts\Cache\CacheInterface;
|
||||
use Symfony\Contracts\Cache\ItemInterface;
|
||||
|
||||
class InsuranceLoader extends AbstractLoader
|
||||
{
|
||||
public function __construct(
|
||||
protected readonly CacheInterface $cache,
|
||||
protected readonly FilesystemOperator $xmlExport,
|
||||
private readonly InsuranceParser $insuranceParser,
|
||||
) {
|
||||
parent::__construct($cache, $xmlExport);
|
||||
}
|
||||
|
||||
public function loadAll(?string $filename = 'versicherungen.xml'): array
|
||||
{
|
||||
try {
|
||||
return $this->cache->get('bpn_insurances', function (ItemInterface $item) use ($filename) {
|
||||
$item->expiresAfter(3 * 60 * 60);
|
||||
|
||||
$crawler = $this->loadXml($filename);
|
||||
|
||||
return $this->insuranceParser->parse($crawler);
|
||||
});
|
||||
} catch (InvalidArgumentException $e) {
|
||||
return [];
|
||||
}
|
||||
}
|
||||
|
||||
public function loadById(string|int $id, ?string $filename = 'versicherungen.xml'): ?Insurance
|
||||
{
|
||||
$insurances = $this->loadAll($filename);
|
||||
|
||||
return $insurances[$id] ?? null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Loads all insurances with resolved package relationships.
|
||||
*
|
||||
* @param string|null $filename The XML filename to load from
|
||||
* @return array<string|int, Insurance> Array of Insurance objects with resolved relationships
|
||||
*/
|
||||
public function loadAllWithRelationships(?string $filename = 'versicherungen.xml'): array
|
||||
{
|
||||
$insurances = $this->loadAll($filename);
|
||||
|
||||
// Resolve package relationships
|
||||
foreach ($insurances as $insurance) {
|
||||
if ($insurance->package && !empty($insurance->containedInsuranceIds)) {
|
||||
foreach ($insurance->containedInsuranceIds as $containedId) {
|
||||
if (isset($insurances[$containedId])) {
|
||||
$insurance->containedInsurances[] = $insurances[$containedId];
|
||||
} else {
|
||||
// Log missing insurance reference for debugging
|
||||
error_log("Insurance package {$insurance->id} references missing insurance {$containedId}");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return $insurances;
|
||||
}
|
||||
|
||||
private function loadXml(?string $filename = 'versicherungen.xml'): Crawler
|
||||
{
|
||||
$xml = $this->xmlExport->read($filename);
|
||||
|
||||
return new Crawler($xml);
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user