Skip to content

Commit

Permalink
Improved Packagist API Client
Browse files Browse the repository at this point in the history
  • Loading branch information
flavioheleno committed Apr 19, 2022
1 parent a469d82 commit 8a98514
Showing 1 changed file with 75 additions and 6 deletions.
81 changes: 75 additions & 6 deletions src/Application/Service/Packagist.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,50 @@
final class Packagist {
private FileStorageInterface $fileStorage;
private Browser $browser;
private bool $workOffline = false;

/**
* @throws \RuntimeException
*/
private function decompress(string $content): string {
$decompressed = gzdecode($content);
if ($decompressed === false) {
throw new RuntimeException('Failed to decompress content');
}

return $decompressed;
}

/**
* @throws \RuntimeException
*/
private function getFileContent(string $file): string {
if ($this->fileStorage->exists($file) === false) {
throw new RuntimeException(
sprintf(
'File not found: "%s"',
$file
)
);
}

$content = $this->fileStorage->readContent($file);
// handle compressed content
if (str_ends_with($file, '.gz')) {
$content = $this->decompress($content);
}

return $content;
}

/**
* @throws \RuntimeException
*/
private function updateFileContent(string $file, string $url): string {
if ($this->isOffline()) {
return $this->getFileContent($file);
}

$headers = [
'User-Agent' => 'php.package.health (twitter.com/flavioheleno)'
];
Expand Down Expand Up @@ -55,10 +94,18 @@ private function updateFileContent(string $file, string $url): string {
$this->fileStorage->setModificationTime($file, $lastModified);
}

// handle compressed content
if (
$response->hasHeader('Content-Type') &&
$response->getHeaderLine('Content-Type') === 'application/octet-stream'
) {
$content = $this->decompress($content);
}

return $content;
}

return $this->fileStorage->readContent($file);
return $this->getFileContent($file);
}

public function __construct(
Expand All @@ -69,6 +116,22 @@ public function __construct(
$this->browser = $browser;
}

public function isOffline(): bool {
return $this->workOffline;
}

public function workOffline(): self {
$this->workOffline = true;

return $this;
}

public function workOnline(): self {
$this->workOffline = false;

return $this;
}

/**
* @return string[]
*/
Expand All @@ -90,8 +153,9 @@ public function getPackageMetadataVersion1(
string $packageName,
string $mirror = 'https://packagist.org'
): array {
$prefix = substr($packageName, 0, 1);
$content = $this->updateFileContent(
"packagist/${packageName}-v1.json",
"packagist/${prefix}/${packageName}-v1.json",
"${mirror}/packages/${packageName}.json"
);

Expand All @@ -107,9 +171,10 @@ public function getPackageMetadataVersion2(
string $packageName,
string $mirror = 'https://repo.packagist.org'
): array {
$prefix = substr($packageName, 0, 1);
$content = $this->updateFileContent(
"packagist/${packageName}-v2.json",
"${mirror}/p2/${packageName}.json"
"packagist/${prefix}/${packageName}-v2.json.gz",
"${mirror}/p2/${packageName}.json.gz"
);

$json = json_decode($content, true, 512, JSON_THROW_ON_ERROR);
Expand Down Expand Up @@ -138,9 +203,13 @@ public function getPackageUpdates(int $since, string $mirror = 'https://packagis
return $json;
}

public function getSecurityAdvisories(string $packageName, string $mirror = 'https://packagist.org'): array {
public function getSecurityAdvisories(
string $packageName,
string $mirror = 'https://packagist.org'
): array {
$prefix = substr($packageName, 0, 1);
$content = $this->updateFileContent(
"packagist/${packageName}-security-advisories.json",
"packagist/${prefix}/${packageName}-security-advisories.json",
"${mirror}/api/security-advisories/?packages[]=${packageName}"
);

Expand Down

0 comments on commit 8a98514

Please sign in to comment.