2FAuth/app/Services/ReleaseRadarService.php

80 lines
2.0 KiB
PHP
Raw Normal View History

2022-09-21 21:50:41 +02:00
<?php
namespace App\Services;
use App\Facades\Settings;
use App\Helpers\Helpers;
2022-09-21 21:50:41 +02:00
use Illuminate\Support\Facades\Http;
use Illuminate\Support\Facades\Log;
class ReleaseRadarService
{
/**
* Run a scheduled release scan
2022-11-22 15:15:52 +01:00
*
* @return void
2022-09-21 21:50:41 +02:00
*/
public function scheduledScan() : void
2022-09-21 21:50:41 +02:00
{
if ((Settings::get('lastRadarScan') + 604800) < time()) {
$this->newRelease();
}
}
/**
* Run a manual release scan
2022-11-22 15:15:52 +01:00
*
* @return false|string False if no new release, the new release number otherwise
*/
public function manualScan() : false|string
{
return $this->newRelease();
}
/**
* Run a release scan
2022-11-22 15:15:52 +01:00
*
* @return false|string False if no new release, the new release number otherwise
*/
protected function newRelease() : false|string
{
2022-11-22 15:15:52 +01:00
if ($latestReleaseData = json_decode($this->getLatestReleaseData())) {
$githubVersion = Helpers::cleanVersionNumber($latestReleaseData->tag_name);
$installedVersion = Helpers::cleanVersionNumber(config('2fauth.version'));
if ($githubVersion > $installedVersion && $latestReleaseData->prerelease == false && $latestReleaseData->draft == false) {
Settings::set('latestRelease', $latestReleaseData->tag_name);
2022-11-22 15:15:52 +01:00
return $latestReleaseData->tag_name;
2022-11-22 15:15:52 +01:00
} else {
Settings::delete('latestRelease');
2022-09-21 21:50:41 +02:00
}
Settings::set('lastRadarScan', time());
}
2022-09-21 21:50:41 +02:00
return false;
2022-09-21 21:50:41 +02:00
}
/**
* Fetch releases on Github
2022-11-22 15:15:52 +01:00
*
2022-09-21 21:50:41 +02:00
* @return string|null
*/
protected function getLatestReleaseData() : string|null
2022-09-21 21:50:41 +02:00
{
try {
$response = Http::retry(3, 100)
->get(config('2fauth.latestReleaseUrl'));
2022-11-22 15:15:52 +01:00
2022-09-21 21:50:41 +02:00
if ($response->successful()) {
return $response->body();
}
2022-11-22 15:15:52 +01:00
} catch (\Exception $exception) {
2022-09-21 21:50:41 +02:00
Log::error('cannot reach latestReleaseUrl endpoint');
}
return null;
}
2022-11-22 15:15:52 +01:00
}