54 lines
1.5 KiB
PHP
54 lines
1.5 KiB
PHP
<?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(
|
|
CacheInterface $cache,
|
|
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;
|
|
}
|
|
|
|
private function loadXml(?string $filename = 'versicherungen.xml'): Crawler
|
|
{
|
|
$xml = $this->xmlExport->read($filename);
|
|
|
|
return new Crawler($xml);
|
|
}
|
|
}
|