Skip to content

Commit

Permalink
Small refactoring of GithubReleaseZipAdapter
Browse files Browse the repository at this point in the history
to reduce complexity retrieveArchiveUrl
  • Loading branch information
gmazzap committed Mar 1, 2022
1 parent 6aa8ed4 commit e1b3c22
Showing 1 changed file with 23 additions and 11 deletions.
34 changes: 23 additions & 11 deletions src/PreCompilation/GithubReleaseZipAdapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -190,26 +190,38 @@ private function retrieveArchiveUrl(
$assetsName .= '.zip';
}

$id = null;
foreach ((array)$json['assets'] as $assetData) {
$id = $this->findAssetId($assetsName, (array)$json['assets'], $repo);
if (!$id) {
return '';
}

return $authString
? "{$authString}api.github.com/repos/{$repo}/releases/assets/{$id}"
: "https://api.github.com/repos/{$repo}/releases/assets/{$id}";
}

/**
* @param string $assetsName
* @param array $assetsData
* @param string $repo
* @return string|null
*/
private function findAssetId(string $assetsName, array $assetsData, string $repo): ?string
{
foreach ($assetsData as $assetData) {
if (!is_array($assetData)) {
continue;
}
$name = $assetData['name'] ?? null;
$id = $name ? ($assetData['id'] ?? null) : null;
if (($name === $assetsName) && $id) {
break;
/** @var string */
return $id;
}
}

if (!$id) {
$this->io->writeVerbose(" Release zip '{$assetsName}' not found in '{$repo}'.");

return '';
}
$this->io->writeVerbose(" Release zip '{$assetsName}' not found in '{$repo}'.");

return $authString
? "{$authString}api.github.com/repos/{$repo}/releases/assets/{$id}"
: "https://api.github.com/repos/{$repo}/releases/assets/{$id}";
return null;
}
}

0 comments on commit e1b3c22

Please sign in to comment.