custom/plugins/KayloManagement/src/Storefront/Controller/ProductController.php line 63

Open in your IDE?
  1. <?php declare(strict_types 1);
  2. namespace Kaylo\Management\Storefront\Controller;
  3. use Kaylo\Management\Storefront\KayloController;
  4. use Shopware\Core\Content\Product\Events\ProductSearchCriteriaEvent;
  5. use Shopware\Core\Content\Product\ProductEvents;
  6. use Shopware\Core\Content\Product\SalesChannel\ProductListResponse;
  7. use Shopware\Core\Content\Product\SalesChannel\Search\AbstractProductSearchRoute;
  8. use Shopware\Core\Content\Product\SalesChannel\Search\ProductSearchRouteResponse;
  9. use Shopware\Core\Framework\DataAbstractionLayer\DefinitionInstanceRegistry;
  10. use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
  11. use Shopware\Core\Framework\DataAbstractionLayer\Search\RequestCriteriaBuilder;
  12. use Shopware\Core\Framework\Routing\Annotation\Entity;
  13. use Shopware\Core\Framework\Routing\Annotation\RouteScope;
  14. use Shopware\Core\System\SalesChannel\Entity\SalesChannelRepositoryInterface;
  15. use Shopware\Core\System\SalesChannel\SalesChannelContext;
  16. use Shopware\Storefront\Controller\StorefrontController;
  17. use Symfony\Component\HttpFoundation\JsonResponse;
  18. use Symfony\Component\HttpFoundation\Request;
  19. use Symfony\Component\Routing\Annotation\Route;
  20. use Symfony\Contracts\EventDispatcher\EventDispatcherInterface;
  21. /**
  22.  * @RouteScope(scopes={"store-api"})
  23.  */
  24. class ProductController extends StorefrontController implements KayloController {
  25.     private SalesChannelRepositoryInterface $productRepository;
  26.     private AbstractProductSearchRoute      $decorated;
  27.     private EventDispatcherInterface        $eventDispatcher;
  28.     private DefinitionInstanceRegistry      $registry;
  29.     private RequestCriteriaBuilder          $criteriaBuilder;
  30.     public function __construct(
  31.         AbstractProductSearchRoute      $decorated,
  32.         EventDispatcherInterface        $eventDispatcher,
  33.         DefinitionInstanceRegistry      $registry,
  34.         RequestCriteriaBuilder          $criteriaBuilder,
  35.         SalesChannelRepositoryInterface $productRepository
  36.     ) {
  37.         $this->decorated         $decorated;
  38.         $this->eventDispatcher   $eventDispatcher;
  39.         $this->registry          $registry;
  40.         $this->criteriaBuilder   $criteriaBuilder;
  41.         $this->productRepository $productRepository;
  42.     }
  43.     public function getDecorated(): AbstractProductSearchRoute {
  44.         return $this->decorated;
  45.     }
  46.     /**
  47.      * @Entity("product")
  48.      * @Route("/product", name="store-api.kaylo.product-list", defaults={"XmlHttpRequest"=true}, methods={"POST"})
  49.      * @param Request $request
  50.      * @param SalesChannelContext $context
  51.      * @param Criteria $criteria
  52.      * @return ProductListResponse|ProductSearchRouteResponse
  53.      */
  54.     public function products(Request $requestSalesChannelContext $contextCriteria $criteria) {
  55.         $searchTerm $request->get('search');
  56.         $criteria   $this->criteriaBuilder->handleRequest(
  57.             $request,
  58.             $criteria,
  59.             $this->registry->getByEntityName('product'),
  60.             $context->getContext()
  61.         );
  62.         $this->eventDispatcher->dispatch(
  63.             new ProductSearchCriteriaEvent($request$criteria$context),
  64.             ProductEvents::PRODUCT_SEARCH_CRITERIA
  65.         );
  66.         if (!empty($searchTerm)) {
  67.             return $this->getDecorated()->load($request$context$criteria);
  68.         }
  69.         $result = new ProductListResponse($this->productRepository->search($criteria$context));
  70.         return $result;
  71.     }
  72.     /**
  73.      * @Route("/send_kaylo_email", name="store-api.kaylo.send_kaylo_email", defaults={"XmlHttpRequest"=true}, methods={"POST"})
  74.      * @param Request $request
  75.      * @param SalesChannelContext $context
  76.      * @return JsonResponse
  77.      */
  78.     public function sendEmail(Request $requestSalesChannelContext $context): JsonResponse {
  79.         return $this->json(['test']);
  80.     }
  81. }