forked from symfony/ux
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPackagistDataProvider.php
More file actions
35 lines (27 loc) · 975 Bytes
/
Copy pathPackagistDataProvider.php
File metadata and controls
35 lines (27 loc) · 975 Bytes
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
<?php
namespace App\Service\Packagist;
use Symfony\Contracts\Cache\CacheInterface;
use Symfony\Contracts\Cache\ItemInterface;
use Symfony\Contracts\HttpClient\HttpClientInterface;
class PackagistDataProvider
{
public function __construct(
private HttpClientInterface $httpClient,
private CacheInterface $cache,
)
{
}
public function getPackageData(string $packageName): array
{
return $this->cache->get('package-data-'.str_replace('/', '--', $packageName), function (ItemInterface $item) use ($packageName) {
$item->expiresAfter(604800); // 1 week
return $this->fetchPackageData($packageName);
});
}
private function fetchPackageData(string $packageName): array
{
$response = $this->httpClient->request('GET', 'https://packagist.org/packages/'.$packageName.'.json');
$packageData = $response->toArray();
return $packageData['package'] ?? [];
}
}