78 lines
2.1 KiB
PHP
78 lines
2.1 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace App\Service;
|
|
|
|
use League\Flysystem\FilesystemException;
|
|
use League\Flysystem\FilesystemOperator;
|
|
|
|
/**
|
|
* Provides access to XML dumps of BusProNet API communication.
|
|
*
|
|
* XML dumps are stored in the filesystem and named with a pattern that includes
|
|
* the request ID, sequence number, and type (request/response). This service
|
|
* allows finding and reading dumps by request ID.
|
|
*/
|
|
class XmlDumpReader
|
|
{
|
|
public function __construct(
|
|
private readonly FilesystemOperator $xmlDump,
|
|
) {
|
|
}
|
|
|
|
/**
|
|
* Finds all XML dump files matching the given request ID.
|
|
*
|
|
* Supports both exact matches (e.g., "abc123_1") and base ID matches (e.g., "abc123")
|
|
* which will return all files with counter suffixes for both request and response types.
|
|
*
|
|
* @return array<int, array{filename: string, type: string, size: int}>
|
|
*
|
|
* @throws FilesystemException
|
|
*/
|
|
public function findDumpsForRequestId(string $requestId): array
|
|
{
|
|
if ('' === $requestId || '-' === $requestId) {
|
|
return [];
|
|
}
|
|
|
|
$pattern = '/^'.preg_quote($requestId, '/').'(_\d+)?_(request|response)\.xml$/';
|
|
$matches = [];
|
|
|
|
foreach ($this->xmlDump->listContents('.') as $item) {
|
|
if ($item->isFile() && 1 === preg_match($pattern, $item->path(), $typeMatch)) {
|
|
$matches[] = [
|
|
'filename' => $item->path(),
|
|
'type' => $typeMatch[2],
|
|
'size' => $this->xmlDump->fileSize($item->path()),
|
|
];
|
|
}
|
|
}
|
|
|
|
usort($matches, static fn (array $a, array $b): int => strcmp($a['filename'], $b['filename']));
|
|
|
|
return $matches;
|
|
}
|
|
|
|
/**
|
|
* Reads the content of a dump file.
|
|
*
|
|
* @throws FilesystemException
|
|
*/
|
|
public function getContent(string $filename): string
|
|
{
|
|
return $this->xmlDump->read($filename);
|
|
}
|
|
|
|
/**
|
|
* Checks if a dump file exists.
|
|
*
|
|
* @throws FilesystemException
|
|
*/
|
|
public function fileExists(string $filename): bool
|
|
{
|
|
return $this->xmlDump->fileExists($filename);
|
|
}
|
|
}
|