AspireCloud/app/Services/Plugins/PluginUpdateService.php
Chuck Adams d9db849e11
Convert Resource Request/Response classes to Bag (#225)
* refactor: use Bag transformers for (Plugin|Theme)HotTagsResponse

* refactor: replace more static constructors with Transforms

* zap: rm unused ThemeUpdateCheckTranslationCollection (whew) VO

* refactor: inline ThemeUpdateData::fromModelCollection

* refactor: embaggify QueryPluginsRequest

* refactor: Bag up PluginInformationRequest

* refactor: throw PluginUpdateRequest into the Bag

* test: move phpstan.neon -> phpstan.dist.neon

* refactor: tighten up update check request types

* test: use more realistic plugin filenames in update check tests

* refactor: use single query for plugin update check

* refactor: convert plugin updates to Bag (i'm out of "bag" neologisms)

* refactor: use ThemeResponse VO for theme info requests

* refactor: commit baggravated assault on ThemeCollection/ThemeResource ;)

* refactor: bag up the rest of the Resource types

* tweak: slipstream in dependabot upgrade
2025-04-02 20:40:32 -06:00

44 lines
1.5 KiB
PHP

<?php
namespace App\Services\Plugins;
use App\Models\WpOrg\Plugin;
use App\Values\WpOrg\Plugins\PluginUpdateCheckRequest;
use App\Values\WpOrg\Plugins\PluginUpdateCheckResponse;
use App\Values\WpOrg\Plugins\PluginUpdateData;
class PluginUpdateService
{
public function checkForUpdates(PluginUpdateCheckRequest $req): PluginUpdateCheckResponse
{
$bySlug = collect($req->plugins)
->mapWithKeys(
fn($pluginData, $pluginFile) => [$this->extractSlug($pluginFile) => [$pluginFile, $pluginData]],
);
$isUpdated = fn($plugin) => version_compare($plugin->version, $bySlug[$plugin->slug][1]['Version'] ?? '', '>');
$mkUpdate = function ($plugin) use ($bySlug) {
$file = $bySlug[$plugin->slug][0];
return [$file => PluginUpdateData::from($plugin)->with(plugin: $file)];
};
[$updates, $no_updates] = Plugin::query()
->whereIn('slug', $bySlug->keys())
->get()
->partition($isUpdated)
->map(fn($collection) => $collection->mapWithKeys($mkUpdate));
return PluginUpdateCheckResponse::from(plugins: $updates, no_update: $no_updates, translations: collect([]));
}
/**
* Extract the plugin slug from the plugin file path
*/
private function extractSlug(string $pluginFile): string
{
return str_contains($pluginFile, '/')
? explode('/', $pluginFile)[0]
: pathinfo($pluginFile, PATHINFO_FILENAME);
}
}