<?php declare(strict_types=1);
namespace Nd\Theme\Storefront\Controller;
use Shopware\Core\Content\Seo\SeoUrlPlaceholderHandlerInterface;
use Shopware\Core\Framework\DataAbstractionLayer\EntityRepositoryInterface;
use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
use Shopware\Core\Framework\Routing\Annotation\RouteScope;
use Shopware\Core\Framework\Uuid\Exception\InvalidUuidException;
use Shopware\Core\PlatformRequest;
use Shopware\Core\System\SalesChannel\Entity\SalesChannelRepositoryInterface;
use Shopware\Core\System\SalesChannel\SalesChannelContext;
use Shopware\Storefront\Controller\StorefrontController;
use Shopware\Storefront\Framework\Routing\RequestTransformer;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;
use Twig\Environment;
/**
* @RouteScope(scopes={"store-api"})
*/
//echo "test";
class RecentProductsController extends StorefrontController
{
private SalesChannelRepositoryInterface $productRepository;
private Environment $twig;
public function __construct(SalesChannelRepositoryInterface $productRepository, Environment $twig)
{
$this->productRepository = $productRepository;
$this->twig = $twig;
}
/**
* @Route("/store-api/nd/recent-products", name="store-api.nd.recent.products", methods={"GET"})
*/
public function getRecentProducts(Request $request, SalesChannelContext $salesChannelContext): Response
{
$response = new Response();
$request->get('productIds');
if (!$request->get('productIds')) {
$response = $this->getResponseTemplate($request, $salesChannelContext, '@NdTheme/storefront/page/recent-products/none.html.twig', []);
$response->setStatusCode(204);
return $response;
}
$productIds = explode(',', $request->get('productIds'));
if (!is_array($productIds)) {
$response = $this->getResponseTemplate($request, $salesChannelContext, '@NdTheme/storefront/page/recent-products/none.html.twig', []);
$response->setStatusCode(204);
return $response;
}
$criteria = new Criteria($productIds);
$criteria->addAssociation("media");
$criteria->getAssociation("cover");
$criteria->getAssociation("prices");
try {
$products = $this->productRepository->search($criteria, $salesChannelContext);
} catch (InvalidUuidException $exception) {
$response = $this->getResponseTemplate($request, $salesChannelContext, '@NdTheme/storefront/page/recent-products/none.html.twig', []);
$response->setStatusCode(204);
return $response;
}
if ($products->count() == 0) {
return $this->getResponseTemplate($request, $salesChannelContext, '@NdTheme/storefront/page/recent-products/none.html.twig', []);
}
return $this->getResponseTemplate($request, $salesChannelContext, '@NdTheme/storefront/page/recent-products/products.html.twig', ['products' => $products->getEntities()]);
}
private function getResponseTemplate(Request $request, $salesChannelContext, $view, $payload): Response
{
$response = new Response();
if (isset($this->twig)) {
$response->setContent($this->twig->render($view, $payload));
$content = $response->getContent();
if ($content !== false) {
$seoUrlReplacer = $this->get(SeoUrlPlaceholderHandlerInterface::class);
$host = $request->attributes->get(RequestTransformer::STOREFRONT_URL);
$response->setContent(
$seoUrlReplacer->replace($content, $request->getSchemeAndHttpHost(), $salesChannelContext)
);
}
}
return $response;
}
}