-
-
Notifications
You must be signed in to change notification settings - Fork 64
/
Copy pathGitHub.php
58 lines (45 loc) · 1.29 KB
/
GitHub.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
<?php
namespace App\Support;
use Illuminate\Support\Facades\Cache;
use Illuminate\Support\Facades\Http;
class GitHub
{
public const PACKAGE_ELECTRON = 'nativephp/electron';
public const PACKAGE_LARAVEL = 'nativephp/laravel';
public function __construct(
private string $package
) {}
public static function electron(): static
{
return new static(static::PACKAGE_ELECTRON);
}
public static function laravel(): static
{
return new static(static::PACKAGE_LARAVEL);
}
public function latestVersion()
{
$version = Cache::remember(
$this->getCacheKey('latest-version'),
now()->addHour(),
function () {
return $this->fetchLatestVersion();
}
);
return $version['name'] ?? 'Unknown';
}
private function fetchLatestVersion()
{
// Make a request to GitHub
$response = Http::get('https://api.github.com/repos/'.$this->package.'/releases/latest');
// Check if the request was successful
if ($response->failed()) {
return null;
}
return $response->json();
}
private function getCacheKey(string $string): string
{
return sprintf('%s-%s', $this->package, $string);
}
}