<?php declare(strict_types = 1);
namespace Kaylo\Management\Storefront\Controller;
use Kaylo\Management\Storefront\KayloController;
use Shopware\Core\Content\Product\Events\ProductSearchCriteriaEvent;
use Shopware\Core\Content\Product\ProductEvents;
use Shopware\Core\Content\Product\SalesChannel\ProductListResponse;
use Shopware\Core\Content\Product\SalesChannel\Search\AbstractProductSearchRoute;
use Shopware\Core\Content\Product\SalesChannel\Search\ProductSearchRouteResponse;
use Shopware\Core\Framework\DataAbstractionLayer\DefinitionInstanceRegistry;
use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
use Shopware\Core\Framework\DataAbstractionLayer\Search\RequestCriteriaBuilder;
use Shopware\Core\Framework\Routing\Annotation\Entity;
use Shopware\Core\Framework\Routing\Annotation\RouteScope;
use Shopware\Core\System\SalesChannel\Entity\SalesChannelRepositoryInterface;
use Shopware\Core\System\SalesChannel\SalesChannelContext;
use Shopware\Storefront\Controller\StorefrontController;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Contracts\EventDispatcher\EventDispatcherInterface;
/**
* @RouteScope(scopes={"store-api"})
*/
class ProductController extends StorefrontController implements KayloController {
private SalesChannelRepositoryInterface $productRepository;
private AbstractProductSearchRoute $decorated;
private EventDispatcherInterface $eventDispatcher;
private DefinitionInstanceRegistry $registry;
private RequestCriteriaBuilder $criteriaBuilder;
public function __construct(
AbstractProductSearchRoute $decorated,
EventDispatcherInterface $eventDispatcher,
DefinitionInstanceRegistry $registry,
RequestCriteriaBuilder $criteriaBuilder,
SalesChannelRepositoryInterface $productRepository
) {
$this->decorated = $decorated;
$this->eventDispatcher = $eventDispatcher;
$this->registry = $registry;
$this->criteriaBuilder = $criteriaBuilder;
$this->productRepository = $productRepository;
}
public function getDecorated(): AbstractProductSearchRoute {
return $this->decorated;
}
/**
* @Entity("product")
* @Route("/product", name="store-api.kaylo.product-list", defaults={"XmlHttpRequest"=true}, methods={"POST"})
* @param Request $request
* @param SalesChannelContext $context
* @param Criteria $criteria
* @return ProductListResponse|ProductSearchRouteResponse
*/
public function products(Request $request, SalesChannelContext $context, Criteria $criteria) {
$searchTerm = $request->get('search');
$criteria = $this->criteriaBuilder->handleRequest(
$request,
$criteria,
$this->registry->getByEntityName('product'),
$context->getContext()
);
$this->eventDispatcher->dispatch(
new ProductSearchCriteriaEvent($request, $criteria, $context),
ProductEvents::PRODUCT_SEARCH_CRITERIA
);
if (!empty($searchTerm)) {
return $this->getDecorated()->load($request, $context, $criteria);
}
$result = new ProductListResponse($this->productRepository->search($criteria, $context));
return $result;
}
/**
* @Route("/send_kaylo_email", name="store-api.kaylo.send_kaylo_email", defaults={"XmlHttpRequest"=true}, methods={"POST"})
* @param Request $request
* @param SalesChannelContext $context
* @return JsonResponse
*/
public function sendEmail(Request $request, SalesChannelContext $context): JsonResponse {
return $this->json(['test']);
}
}