generated from boboko/starter
57 lines
1.7 KiB
PHP
57 lines
1.7 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Controllers;
|
|
|
|
use App\Catalog\ProductCard;
|
|
use Illuminate\Http\RedirectResponse;
|
|
use Illuminate\Support\Collection;
|
|
use Illuminate\View\View;
|
|
use Modules\Core\Catalog\Services\ProductService;
|
|
use Modules\Core\Wishlist\Services\WishlistService;
|
|
|
|
/**
|
|
* Page rendering only — the toggle action itself lives in core
|
|
* (Modules\Core\Wishlist\Http\Controllers\WishlistController, route
|
|
* `wishlist.toggle`), since it needs no app-specific presentation.
|
|
*/
|
|
class WishlistController extends Controller
|
|
{
|
|
public function __construct(
|
|
private readonly WishlistService $wishlist,
|
|
) {}
|
|
|
|
/**
|
|
* The account's wishlist page.
|
|
*/
|
|
public function index(string $locale, ProductService $products): View
|
|
{
|
|
return view('account.wishlist', ['products' => $this->products($products)]);
|
|
}
|
|
|
|
/**
|
|
* The same list for a guest, from their cookie. Logged-in users are sent
|
|
* to the account version.
|
|
*/
|
|
public function guest(string $locale, ProductService $products): View|RedirectResponse
|
|
{
|
|
if (auth()->check()) {
|
|
return redirect()->route('account.wishlist');
|
|
}
|
|
|
|
return view('wishlist.guest', ['products' => $this->products($products)]);
|
|
}
|
|
|
|
/**
|
|
* Product cards for the current wishlist, newest first. Products no longer
|
|
* in the search index (deleted, unpublished) are simply skipped.
|
|
*/
|
|
private function products(ProductService $products): Collection
|
|
{
|
|
return collect($this->wishlist->ids())
|
|
->map(fn (int $id) => $products->getById($id))
|
|
->filter()
|
|
->map(fn (array $product) => ['id' => $product['id'], ...ProductCard::fromIndexed($product)])
|
|
->values();
|
|
}
|
|
}
|