From 0bc3e2d251627fbd90b6f07e16800947ea6b7c7f Mon Sep 17 00:00:00 2001 From: Aaron Piotrowski Date: Sat, 9 Mar 2024 10:05:11 -0600 Subject: [PATCH] Add Filesystem arg to FileMutex --- src/FileMutex.php | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/src/FileMutex.php b/src/FileMutex.php index e773ce1..d500559 100644 --- a/src/FileMutex.php +++ b/src/FileMutex.php @@ -11,11 +11,14 @@ final class FileMutex implements Mutex { private const LATENCY_TIMEOUT = 0.01; + private readonly Filesystem $filesystem; + /** * @param string $fileName Name of temporary file to use as a mutex. */ - public function __construct(private readonly string $fileName) + public function __construct(private readonly string $fileName, ?Filesystem $filesystem = null) { + $this->filesystem = $filesystem ?? filesystem(); } public function acquire(): Lock @@ -24,7 +27,7 @@ final class FileMutex implements Mutex // has the lock, so set an asynchronous timer and try again. while (true) { try { - $file = openFile($this->fileName, 'x'); + $file = $this->filesystem->openFile($this->fileName, 'x'); // Return a lock object that can be used to release the lock on the mutex. $lock = new Lock($this->release(...)); @@ -46,12 +49,11 @@ final class FileMutex implements Mutex private function release(): void { try { - deleteFile($this->fileName); + $this->filesystem->deleteFile($this->fileName); } catch (\Throwable $exception) { throw new SyncException( 'Failed to unlock the mutex file: ' . $this->fileName, - 0, - $exception + previous: $exception, ); } }