-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathUpdateStatusPackages.php
executable file
·67 lines (57 loc) · 1.82 KB
/
UpdateStatusPackages.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
59
60
61
62
63
64
65
66
67
<?php
namespace App\Jobs;
use App\Models\Package;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Queue\SerializesModels;
use Illuminate\Support\Facades\Http;
use Illuminate\Support\Facades\Log;
class UpdateStatusPackages implements ShouldQueue
{
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
/**
* Create a new job instance.
*/
public function __construct(protected Package $package)
{
//
}
/**
* Execute the job.
*/
public function handle(): void
{
// Fetch package information from Packagist API
$response = Http::get("https://packagist.org/packages/{$this->package->packagist_name}.json");
// Check if the request was successful
if (! $response->successful()) {
Log::warning("Failed to fetch package information for package '{$this->package->packagist_name}'. Response: ".$response->body());
return;
}
$info = $response
->collect('package')
->only([
'name',
'description',
'repository',
'github_stars',
'downloads',
]);
$this->package->update([
'name' => $info['name'],
'description' => $info['description'],
'stars' => $info['github_stars'],
'downloads' => $info['downloads']['total'],
'website' => $info['repository'],
]);
}
/**
* Handle a job failure.
*/
public function failed(\Throwable $exception): void
{
Log::error("Failed to update package '{$this->package->packagist_name}': {$exception->getMessage()}");
}
}