fix: answer null instead of 500 when the export marker is missing
This commit is contained in:
1 parent
8a6ce7606f
commit
c2c55bc8b2
4 files changed
+83
-15
No files matched your search
@@ -4,6 +4,9 @@ declare(strict_types=1);
|
||||
|
||||
namespace App\BusProNet\Model;
|
||||
|
||||
use League\Flysystem\FilesystemException;
|
||||
use League\Flysystem\FilesystemOperator;
|
||||
|
||||
final readonly class XmlExportInfo
|
||||
{
|
||||
private const INFO_FILENAME = 'uebertragung.info';
|
||||
@@ -20,6 +23,26 @@ final readonly class XmlExportInfo
|
||||
return self::INFO_FILENAME;
|
||||
}
|
||||
|
||||
/**
|
||||
* Reads the marker from a storage, or null when it is missing or unreadable.
|
||||
*
|
||||
* A marker caught mid-sync or truncated is treated as absent rather than as an error:
|
||||
* callers only ever want to know which export they are looking at, and "unknown" is
|
||||
* an answer they can act on.
|
||||
*/
|
||||
public static function fromStorage(FilesystemOperator $storage): ?self
|
||||
{
|
||||
try {
|
||||
if (false === $storage->fileExists(self::INFO_FILENAME)) {
|
||||
return null;
|
||||
}
|
||||
|
||||
return self::fromString($storage->read(self::INFO_FILENAME));
|
||||
} catch (FilesystemException|\InvalidArgumentException) {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
public static function fromString(string $content): self
|
||||
{
|
||||
$content = mb_convert_encoding($content, 'UTF-8', 'Windows-1252');
|
||||
|
||||
@@ -2,6 +2,7 @@
|
||||
|
||||
namespace App\Controller\Api;
|
||||
|
||||
use App\BusProNet\Model\XmlExportInfo;
|
||||
use League\Flysystem\FilesystemOperator;
|
||||
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
|
||||
use Symfony\Component\HttpFoundation\JsonResponse;
|
||||
@@ -16,23 +17,15 @@ class LastUpdateController extends AbstractController
|
||||
{
|
||||
}
|
||||
|
||||
/**
|
||||
* A missing or unreadable marker answers `{"timestamp": null}`, never a 500: consumers
|
||||
* poll this for change detection and treat null as "unknown".
|
||||
*/
|
||||
#[Route(path: '/last-update', name: 'api_last_update', methods: ['GET'])]
|
||||
public function index(): JsonResponse
|
||||
{
|
||||
if (false === $this->xmlExport->has('uebertragung.info')) {
|
||||
$lastUploadAt = null;
|
||||
} else {
|
||||
$file = $this->xmlExport->read('uebertragung.info');
|
||||
$lines = explode("\n", $file);
|
||||
$line = trim($lines[0]);
|
||||
$info = XmlExportInfo::fromStorage($this->xmlExport);
|
||||
|
||||
if (true === empty($line)) {
|
||||
$lastUploadAt = null;
|
||||
} else {
|
||||
$lastUploadAt = \DateTime::createFromFormat('d.m.Y H:i:s', $line);
|
||||
}
|
||||
}
|
||||
|
||||
return $this->json(['timestamp' => $lastUploadAt->format('Y-m-d H:i:s')]);
|
||||
return $this->json(['timestamp' => $info?->lastTransfer->format('Y-m-d H:i:s')]);
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user