Merged functionality from UpdateAvailableManager and UpdateChecker

This commit is contained in:
Jan Böhmer 2026-02-02 18:44:44 +01:00
parent 6dbead6d10
commit 68ff0721ce
7 changed files with 34 additions and 57 deletions

View file

@ -35,17 +35,18 @@ use Version\Version;
/**
* This class checks if a new version of Part-DB is available.
*/
class UpdateAvailableManager
class UpdateAvailableFacade
{
private const API_URL = 'https://api.github.com/repos/Part-DB/Part-DB-server/releases/latest';
private const CACHE_KEY = 'uam_latest_version';
private const CACHE_TTL = 60 * 60 * 24 * 2; // 2 day
public function __construct(private readonly HttpClientInterface $httpClient,
private readonly CacheInterface $updateCache, private readonly VersionManagerInterface $versionManager,
private readonly PrivacySettings $privacySettings, private readonly LoggerInterface $logger,
#[Autowire(param: 'kernel.debug')] private readonly bool $is_dev_mode)
public function __construct(
private readonly CacheInterface $updateCache,
private readonly PrivacySettings $privacySettings,
private readonly UpdateChecker $updateChecker,
)
{
}
@ -89,9 +90,7 @@ class UpdateAvailableManager
}
$latestVersion = $this->getLatestVersion();
$currentVersion = $this->versionManager->getVersion();
return $latestVersion->isGreaterThan($currentVersion);
return $this->updateChecker->isNewerVersionThanCurrent($latestVersion);
}
/**
@ -111,34 +110,7 @@ class UpdateAvailableManager
return $this->updateCache->get(self::CACHE_KEY, function (ItemInterface $item) {
$item->expiresAfter(self::CACHE_TTL);
try {
$response = $this->httpClient->request('GET', self::API_URL);
$result = $response->toArray();
$tag_name = $result['tag_name'];
// Remove the leading 'v' from the tag name
$version = substr($tag_name, 1);
return [
'version' => $version,
'url' => $result['html_url'],
];
} catch (\Exception $e) {
//When we are in dev mode, throw the exception, otherwise just silently log it
if ($this->is_dev_mode) {
throw $e;
}
//In the case of an error, try it again after half of the cache time
$item->expiresAfter(self::CACHE_TTL / 2);
$this->logger->error('Checking for updates failed: ' . $e->getMessage());
return [
'version' => '0.0.1',
'url' => 'update-checking-error'
];
}
return $this->updateChecker->getLatestVersion();
});
}
}
}

View file

@ -75,7 +75,7 @@ class UpdateChecker
* Get Git repository information.
* @return array{branch: ?string, commit: ?string, has_local_changes: bool, commits_behind: int, is_git_install: bool}
*/
public function getGitInfo(): array
private function getGitInfo(): array
{
$info = [
'branch' => null,
@ -113,7 +113,7 @@ class UpdateChecker
return 0;
}
$cacheKey = self::CACHE_KEY_COMMITS . '_' . md5($branch);
$cacheKey = self::CACHE_KEY_COMMITS . '_' . hash('xxh3', $branch);
return $this->updateCache->get($cacheKey, function (ItemInterface $item) use ($branch) {
$item->expiresAfter(self::CACHE_TTL);
@ -135,9 +135,9 @@ class UpdateChecker
*/
public function refreshVersionInfo(): void
{
$gitInfo = $this->getGitInfo();
if ($gitInfo['branch']) {
$this->updateCache->delete(self::CACHE_KEY_COMMITS . '_' . md5($gitInfo['branch']));
$gitBranch = $this->gitVersionInfoProvider->getBranchName();
if ($gitBranch) {
$this->updateCache->delete(self::CACHE_KEY_COMMITS . '_' . hash('xxh3', $gitBranch));
}
$this->updateCache->delete(self::CACHE_KEY_RELEASES);
}
@ -150,7 +150,10 @@ class UpdateChecker
public function getAvailableReleases(int $limit = 10): array
{
if (!$this->privacySettings->checkForUpdates) {
return [];
return [ //If we don't want to check for updates, we can return dummy data
'version' => '0.0.1',
'url' => 'update-checking-disabled'
];
}
return $this->updateCache->get(self::CACHE_KEY_RELEASES, function (ItemInterface $item) use ($limit) {
@ -212,7 +215,7 @@ class UpdateChecker
* Get the latest stable release.
* @return array{version: string, tag: string, name: string, url: string, published_at: string, body: string, prerelease: bool, assets: array}|null
*/
public function getLatestRelease(bool $includePrerelease = false): ?array
public function getLatestVersion(bool $includePrerelease = false): ?array
{
$releases = $this->getAvailableReleases();
@ -236,11 +239,13 @@ class UpdateChecker
/**
* Check if a specific version is newer than current.
*/
public function isNewerVersion(string $version): bool
public function isNewerVersionThanCurrent(Version|string $version): bool
{
if ($version instanceof Version) {
return $version->isGreaterThan($this->getCurrentVersion());
}
try {
$targetVersion = Version::fromString(ltrim($version, 'v'));
return $targetVersion->isGreaterThan($this->getCurrentVersion());
return Version::fromString(ltrim($version, 'v'))->isGreaterThan($this->getCurrentVersion());
} catch (\Exception) {
return false;
}
@ -254,7 +259,7 @@ class UpdateChecker
public function getUpdateStatus(): array
{
$current = $this->getCurrentVersion();
$latest = $this->getLatestRelease();
$latest = $this->getLatestVersion();
$gitInfo = $this->getGitInfo();
$installInfo = $this->installationTypeDetector->getInstallationInfo();