From 05ac07d656d5e8de94a1339cb8e25cc7db747f11 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bj=C3=B6rn=20Fromme?= Date: Fri, 20 Mar 2026 14:23:51 +0100 Subject: [PATCH] fix: delete unavailable files during xml sync addresses #869chrmp4 --- src/Command/BpnXmlSyncCommand.php | 36 ++++++++++++++++++++++++------- 1 file changed, 28 insertions(+), 8 deletions(-) diff --git a/src/Command/BpnXmlSyncCommand.php b/src/Command/BpnXmlSyncCommand.php index d4cd5bf..e9daaa0 100644 --- a/src/Command/BpnXmlSyncCommand.php +++ b/src/Command/BpnXmlSyncCommand.php @@ -106,7 +106,7 @@ class BpnXmlSyncCommand extends Command $io->section('Syncing files'); try { - $syncedCount = $this->syncFiles($io); + $syncResult = $this->syncFiles($io); } catch (FilesystemException $e) { $io->error(sprintf('Sync failed: %s', $e->getMessage())); $this->logger->error('XML sync failed during file transfer', ['exception' => $e]); @@ -117,9 +117,14 @@ class BpnXmlSyncCommand extends Command $this->invalidateCaches(); $io->text(sprintf('Invalidated %d cache keys', count(self::CACHE_KEYS_TO_INVALIDATE))); - $io->success(sprintf('Synced %d files', $syncedCount)); + $io->success(sprintf( + 'Sync complete: %d files updated, %d files deleted', + $syncResult['updated'], + $syncResult['deleted'] + )); $this->logger->info('XML sync completed', [ - 'files_synced' => $syncedCount, + 'files_updated' => $syncResult['updated'], + 'files_deleted' => $syncResult['deleted'], 'remote_timestamp' => $remoteInfo->lastTransfer->format('c'), ]); @@ -159,17 +164,25 @@ class BpnXmlSyncCommand extends Command /** * @throws FilesystemException */ - private function syncFiles(SymfonyStyle $io): int + private function syncFiles(SymfonyStyle $io): array { - $files = $this->xmlSource + $remoteFiles = $this->xmlSource ->listContents('.') ->filter(fn (StorageAttributes $attributes) => $attributes->isFile()) ->map(fn (StorageAttributes $attributes) => $attributes->path()) ->toArray(); - $io->progressStart(count($files)); + $localFiles = $this->xmlExport + ->listContents('.') + ->filter(fn (StorageAttributes $attributes) => $attributes->isFile()) + ->map(fn (StorageAttributes $attributes) => $attributes->path()) + ->toArray(); - foreach ($files as $path) { + $filesToDelete = array_diff($localFiles, $remoteFiles); + + $io->progressStart(count($remoteFiles)); + + foreach ($remoteFiles as $path) { $content = $this->xmlSource->read($path); $this->xmlExport->write($path, $content); $io->progressAdvance(); @@ -177,7 +190,14 @@ class BpnXmlSyncCommand extends Command $io->progressFinish(); - return count($files); + foreach ($filesToDelete as $path) { + $this->xmlExport->delete($path); + } + + return [ + 'updated' => count($remoteFiles), + 'deleted' => count($filesToDelete), + ]; } private function invalidateCaches(): void