From c14854ef215c90f83dbf49d81ed45a89a6af83d7 Mon Sep 17 00:00:00 2001 From: Andrew Berry Date: Wed, 17 Apr 2024 05:53:26 -0400 Subject: [PATCH] Extract a needs download method in the binary installer (#534) * Extract a method for checking file validity * Remove file_exists() check that is always true --- src/BinaryInstaller.php | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/src/BinaryInstaller.php b/src/BinaryInstaller.php index ea84e0490..b5db8482e 100644 --- a/src/BinaryInstaller.php +++ b/src/BinaryInstaller.php @@ -208,7 +208,7 @@ protected function installBinary($binary, $version, $url, $sha, $hashalgo = 'sha // Check the cache. $fs->ensureDirectoryExists($cacheFolder); - if (!$this->cache->isEnabled() || !file_exists($cacheDestination) || (file_exists($cacheDestination) && hash_file($hashalgo, $cacheDestination) !== $sha)) { + if ($this->needsDownload($cacheDestination, $hashalgo, $sha)) { // Fetch a new copy of the binary. $httpDownloader->copy($url, $cacheDestination); } else { @@ -253,4 +253,17 @@ protected function installBinary($binary, $version, $url, $sha, $hashalgo = 'sha } } } + + /** + * Return if a file needs to be downloaded or not. + * + * @param string $cacheDestination The destination path to the downloaded file. + * @param $hashalgo The hash algorithm used to validate the file. + * @param $hash The hash used to validate the file. + * + * @return bool True if the file needs to be downloaded again, false otherwise. + */ + private function needsDownload(string $cacheDestination, $hashalgo, $hash): bool { + return !$this->cache->isEnabled() || !file_exists($cacheDestination) || hash_file($hashalgo, $cacheDestination) !== $hash; + } }