Skip to content

Commit

Permalink
Extract a needs download method in the binary installer (#534)
Browse files Browse the repository at this point in the history
* Extract a method for checking file validity

* Remove file_exists() check that is always true
  • Loading branch information
deviantintegral authored Apr 17, 2024
1 parent 04484c9 commit c14854e
Showing 1 changed file with 14 additions and 1 deletion.
15 changes: 14 additions & 1 deletion src/BinaryInstaller.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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;
}
}

0 comments on commit c14854e

Please sign in to comment.