Skip to content

Commit

Permalink
Apply suggestions from code review
Browse files Browse the repository at this point in the history
  • Loading branch information
mcaskill committed Aug 6, 2024
1 parent c95951f commit e5fba99
Showing 1 changed file with 34 additions and 28 deletions.
62 changes: 34 additions & 28 deletions src/Plugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,7 @@ public function handlePreInstallUpdateEvent(PackageEvent $event): void

/**
* Handle indirection process, like used by WP EDD
*
* The "indirection" property can contain:
* "http" and "ssl" object, as defined by https://github.com/composer/composer/blob/main/src/Composer/Util/Http/CurlDownloader.php
* "parse": {
Expand All @@ -198,37 +199,42 @@ public function fetchIndirection(PreFileDownloadEvent $event, string $url, array
{
$options = [
'http' => array_replace_recursive(['method' => 'GET'], $extra['indirection']['http'] ?? []),
'ssl' => $extra['indirection']['ssl'] ?? []
'ssl' => $extra['indirection']['ssl'] ?? [],
];

$data = $event->getHttpDownloader()->get($url, $options);
if ($extra['indirection']['parse']['format'] ?? false === 'json') {
$response = $data->decodeJson();
$key = $extra['indirection']['parse']['key'] ?? false;
if ($key) {
// Look for a succession of (nested) keys within a recursive array (from the JSON)
$jsonObj = $response;
do {
$index = is_string($key) ? $key : key($key);
if (! isset($jsonObj[$index])) {
break;
}
$jsonObj = $jsonObj[$index];
if (!is_array($key)) {
break;
}
$key = $key[$index] ?? false;
} while ($key);

return is_array($jsonObj) ? json_encode($jsonObj) : $jsonObj;
}
$response = $event->getHttpDownloader()->get($url, $options);
if ($extra['indirection']['parse']['format'] ?? false !== 'json') {
// Raw HTML
// @TODO Future usage of regexp
return $response->getBody();
}

// format=json but no key specified (!)
return json_encode($response);
} else {
// raw HTML (future usage of regexp?)
return $data->getBody();
$jsonObject = $response->decodeJson();

$key = $extra['indirection']['parse']['key'] ?? false;
if ($key === false) {
// format=json but no key specified
return json_encode($jsonObject);
}

// Look for a succession of possibly nested keys
// within a recursive array from the JSON object
do {
$index = is_array($key) ? key($key) : $key;
if (! isset($jsonObject[$index])) {
break;
}

// Go one level deeper
$jsonObject = $jsonObject[$index];
if (! is_array($key)) {
break;
}

$key = $key[$index] ?? false;
} while (is_array($key) || is_string($key));

return is_array($jsonObject) ? json_encode($jsonObject) : $jsonObject;
}

/**
Expand Down Expand Up @@ -256,7 +262,7 @@ public function handlePreDownloadEvent(PreFileDownloadEvent $event): void
// Fulfill env placeholders
$filteredProcessedUrl = $this->fulfillPlaceholders($filteredProcessedUrl);

if ($extra['indirection'] ?? false) {
if (isset($extra['indirection'])) {
$filteredCacheKey = $filteredProcessedUrl = $this->fetchIndirection($event, $filteredProcessedUrl, $extra);
}

Expand Down

0 comments on commit e5fba99

Please sign in to comment.