Files
ladill-woo-manager/app/Services/Woo/IntegrationStatusService.php
T
isaaccladandCursor 1b0831b176 Extend integrations hub with more channels, caching, and refresh.
Cache per-store status for five minutes, add TikTok/Reddit/Meta cards, and let merchants refresh without reloading WordPress on every visit.

Co-authored-by: Cursor <cursoragent@cursor.com>
2026-07-08 07:37:41 +00:00

160 lines
5.1 KiB
PHP

<?php
namespace App\Services\Woo;
use App\Models\WooStore;
use Illuminate\Support\Facades\Cache;
use Illuminate\Support\Facades\Log;
class IntegrationStatusService
{
public function __construct(private PluginClient $client) {}
/**
* @return array{
* store: ?WooStore,
* reachable: bool,
* cached: bool,
* integrations: list<array<string, mixed>>
* }
*/
public function forStore(?WooStore $store, bool $refresh = false): array
{
$catalog = config('woo_integrations', []);
if (! $store) {
return [
'store' => null,
'reachable' => false,
'cached' => false,
'integrations' => $this->catalogOnly($catalog),
];
}
if ($refresh) {
$this->forgetCache($store);
}
$ttl = max(0, (int) config('woo.integrations_cache_ttl_seconds', 300));
$cacheKey = $this->cacheKey($store);
if ($ttl === 0) {
return $this->buildPayload($store, $catalog);
}
$cached = Cache::get($cacheKey);
if (is_array($cached)) {
return array_merge($cached, ['cached' => true]);
}
$payload = $this->buildPayload($store, $catalog);
Cache::put($cacheKey, $payload, $ttl);
return array_merge($payload, ['cached' => false]);
}
public function forgetCache(WooStore $store): void
{
Cache::forget($this->cacheKey($store));
}
/**
* @param array<string, array<string, mixed>> $catalog
* @return array{
* store: WooStore,
* reachable: bool,
* integrations: list<array<string, mixed>>
* }
*/
private function buildPayload(WooStore $store, array $catalog): array
{
$remote = $this->client->get($store, 'integrations/status');
$reachable = is_array($remote);
if (! $reachable) {
Log::info('Woo integration status unavailable', ['store_id' => $store->id]);
}
$remoteItems = is_array($remote['integrations'] ?? null) ? $remote['integrations'] : [];
$integrations = [];
foreach ($catalog as $key => $meta) {
$remoteStatus = is_array($remoteItems[$key] ?? null) ? $remoteItems[$key] : [];
$integrations[] = $this->mergeIntegration($key, $meta, $remoteStatus, $store, $reachable);
}
return [
'store' => $store,
'reachable' => $reachable,
'integrations' => $integrations,
];
}
/**
* @param array<string, mixed> $meta
* @param array<string, mixed> $remoteStatus
* @return array<string, mixed>
*/
private function mergeIntegration(string $key, array $meta, array $remoteStatus, WooStore $store, bool $reachable): array
{
$state = $reachable
? (string) ($remoteStatus['state'] ?? 'unknown')
: 'unavailable';
$manageUrl = $remoteStatus['manage_url'] ?? null;
if (is_string($manageUrl) && $manageUrl !== '' && ! str_starts_with($manageUrl, 'http')) {
$manageUrl = rtrim((string) $store->site_url, '/').'/'.ltrim($manageUrl, '/');
}
return [
'key' => $key,
'name' => (string) ($meta['name'] ?? $key),
'description' => (string) ($meta['description'] ?? ''),
'category' => (string) ($meta['category'] ?? 'Integration'),
'install_url' => $meta['install_url'] ?? null,
'docs_url' => $meta['docs_url'] ?? null,
'state' => $state,
'installed' => (bool) ($remoteStatus['installed'] ?? false),
'connected' => (bool) ($remoteStatus['connected'] ?? false),
'summary' => $reachable
? (string) ($remoteStatus['summary'] ?? '')
: 'Could not reach your store. Check that the Ladill plugin is active.',
'manage_url' => is_string($manageUrl) && $manageUrl !== '' ? $manageUrl : null,
'details' => is_array($remoteStatus['details'] ?? null) ? $remoteStatus['details'] : [],
];
}
/**
* @param array<string, array<string, mixed>> $catalog
* @return list<array<string, mixed>>
*/
private function catalogOnly(array $catalog): array
{
$integrations = [];
foreach ($catalog as $key => $meta) {
$integrations[] = [
'key' => $key,
'name' => (string) ($meta['name'] ?? $key),
'description' => (string) ($meta['description'] ?? ''),
'category' => (string) ($meta['category'] ?? 'Integration'),
'install_url' => $meta['install_url'] ?? null,
'docs_url' => $meta['docs_url'] ?? null,
'state' => 'no_store',
'installed' => false,
'connected' => false,
'summary' => 'Connect a WooCommerce store to see status.',
'manage_url' => null,
'details' => [],
];
}
return $integrations;
}
private function cacheKey(WooStore $store): string
{
return 'woo.integrations.'.$store->id;
}
}