Skip to content

Commit

Permalink
fix: fixed Cannot access offset of type string on string
Browse files Browse the repository at this point in the history
  • Loading branch information
lewislarsen committed Jul 1, 2024
1 parent 1fb3387 commit 5e27030
Showing 1 changed file with 23 additions and 2 deletions.
25 changes: 23 additions & 2 deletions app/Services/Backup/Backup.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
use Exception;
use Illuminate\Support\Facades\Log;
use Illuminate\Support\Facades\Mail;
use InvalidArgumentException;
use phpseclib3\Crypt\Common\PrivateKey;
use phpseclib3\Crypt\PublicKeyLoader;
use RuntimeException;
Expand Down Expand Up @@ -543,6 +544,14 @@ protected function logWithTimestamp(string $message, string $timezone): string
return '[' . $timestamp . '] ' . $message . "\n";
}

/**
* @param BackupDestinationInterface $backupDestination
* @param int $backupTaskId
* @param int $backupLimit
* @param string $fileExtension
* @param string $pattern
* @return void
*/
protected function rotateOldBackups(
BackupDestinationInterface $backupDestination,
int $backupTaskId,
Expand All @@ -553,14 +562,25 @@ protected function rotateOldBackups(
$this->logInfo('Rotating old backups.', ['backup_task_id' => $backupTaskId, 'backup_limit' => $backupLimit]);

try {
$files = $backupDestination->listFiles("{$pattern}{$backupTaskId}_");
/** @var array<array<string, mixed>> $files */
$files = $backupDestination->listFiles("{$pattern}{$backupTaskId}_*{$fileExtension}");

$this->logDebug('Files filtered and sorted.', ['file_count' => count($files)]);

while (count($files) > $backupLimit) {
$oldestFile = array_pop($files);

$file = $oldestFile['Key'] ?? $oldestFile['name']; // @phpstan-ignore-line
if (!is_array($oldestFile)) {
$this->logError('Invalid file structure encountered.', ['file' => $oldestFile]);
continue;
}

$file = $oldestFile['Key'] ?? $oldestFile['name'] ?? null;

if (!is_string($file)) {
$this->logError('Invalid file name or key.', ['file' => $oldestFile]);
continue;
}

$this->logDebug('Deleting old backup.', ['file' => $file]);

Expand All @@ -570,6 +590,7 @@ protected function rotateOldBackups(
$this->logInfo('Old backups rotation completed.', ['remaining_files' => count($files)]);
} catch (Exception $e) {
$this->logError('Error rotating old backups.', ['error' => $e->getMessage()]);
// Consider re-throwing the exception or handling it according to your error management strategy
}
}

Expand Down

0 comments on commit 5e27030

Please sign in to comment.