|null $allowedStoreIds */ public function activeStores(User $account, ?array $allowedStoreIds = null): Collection { $query = WooStore::query() ->where('user_id', $account->id) ->where('status', WooStore::STATUS_ACTIVE) ->orderBy('site_name') ->orderBy('site_url'); if ($allowedStoreIds !== null) { $query->whereIn('id', $allowedStoreIds); } return $query->get(); } /** @param list|null $allowedStoreIds */ public function resolve(?User $account, ?array $allowedStoreIds = null): ?WooStore { if (! $account) { return null; } $stores = $this->activeStores($account, $allowedStoreIds); if ($stores->isEmpty()) { return null; } if ($allowedStoreIds !== null && count($allowedStoreIds) === 1) { session([self::SESSION_KEY => $stores->first()->id]); return $stores->first(); } $selectedId = (int) session(self::SESSION_KEY, 0); $store = $stores->firstWhere('id', $selectedId); if ($store) { return $store; } $first = $stores->first(); session([self::SESSION_KEY => $first->id]); return $first; } /** @param list|null $allowedStoreIds */ public function switch(User $account, int $storeId, ?array $allowedStoreIds = null): WooStore { $query = WooStore::query() ->where('user_id', $account->id) ->whereKey($storeId) ->where('status', WooStore::STATUS_ACTIVE); if ($allowedStoreIds !== null) { $query->whereIn('id', $allowedStoreIds); } $store = $query->firstOrFail(); session([self::SESSION_KEY => $store->id]); return $store; } }