vendor/crehler/mojebambino-catalog/src/Controller/StoreApi/CatalogController.php line 182

Open in your IDE?
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4.  * @copyright 2020 Crehler Sp. z o. o.
  5.  * @link https://crehler.com/
  6.  * @support support@crehler.com
  7.  *
  8.  * @author Mateusz FlasiƄski
  9.  *
  10.  * For the full copyright and license information, please view the LICENSE
  11.  * file that was distributed with this source code.
  12.  */
  13. namespace Crehler\Catalog\Controller\StoreApi;
  14. use Crehler\Catalog\Content\BookmarksResponse;
  15. use Crehler\Catalog\Content\CatalogBasicDataResponse;
  16. use Crehler\Catalog\Content\CatalogFileResponse;
  17. use Crehler\Catalog\Content\CatalogFlatDataResponse;
  18. use Crehler\Catalog\Content\CatalogPageDetailResponse;
  19. use Crehler\Catalog\Content\StoreApiCatalogResponse;
  20. use Crehler\Catalog\Content\TableOfContentsResponse;
  21. use Crehler\Catalog\Entity\Catalog\CatalogEntity;
  22. use Crehler\Catalog\Service\CatalogCacheManager;
  23. use Crehler\Catalog\Service\CatalogReader\BookmarksTreeService;
  24. use Crehler\Catalog\Service\CatalogReader\Loader\CatalogPageLoader;
  25. use Crehler\Catalog\Service\CatalogReader\Loader\TableOfContentsLoader;
  26. use Crehler\Catalog\Service\FlatDataManager\FlatLoaderInterface;
  27. use Crehler\Catalog\Service\PdfMergeService;
  28. use OpenApi\Annotations as OA;
  29. use Shopware\Core\Framework\DataAbstractionLayer\EntityRepositoryInterface;
  30. use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
  31. use Shopware\Core\Framework\Routing\Annotation\RouteScope;
  32. use Shopware\Core\System\SalesChannel\SalesChannelContext;
  33. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  34. use Symfony\Component\HttpFoundation\JsonResponse;
  35. use Symfony\Component\HttpFoundation\Request;
  36. use Symfony\Component\HttpFoundation\Response;
  37. use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
  38. use Symfony\Component\Routing\Annotation\Route;
  39. /**
  40.  * @RouteScope(scopes={"store-api"})
  41.  */
  42. class CatalogController
  43. {
  44.     private EntityRepositoryInterface $catalogPageRepository;
  45.     private TableOfContentsLoader $contentsLoader;
  46.     private EntityRepositoryInterface $catalogRepository;
  47.     private BookmarksTreeService $bookmarksTreeService;
  48.     private CatalogPageLoader $catalogPageLoader;
  49.     private CatalogCacheManager $cacheManager;
  50.     private FlatLoaderInterface $catalogFlatLoader;
  51.     private PdfMergeService $pdfMergeService;
  52.     public function __construct(EntityRepositoryInterface $catalogPageRepository,
  53.                                 TableOfContentsLoader $contentsLoader,
  54.                                 EntityRepositoryInterface $catalogRepository,
  55.                                 BookmarksTreeService $bookmarksTreeService,
  56.                                 CatalogPageLoader $catalogPageLoader,
  57.                                 CatalogCacheManager $cacheManager,
  58.                                 FlatLoaderInterface $catalogFlatLoader,
  59.                                 PdfMergeService $pdfMergeService)
  60.     {
  61.         $this->catalogPageRepository $catalogPageRepository;
  62.         $this->contentsLoader $contentsLoader;
  63.         $this->catalogRepository $catalogRepository;
  64.         $this->bookmarksTreeService $bookmarksTreeService;
  65.         $this->catalogPageLoader $catalogPageLoader;
  66.         $this->cacheManager $cacheManager;
  67.         $this->catalogFlatLoader $catalogFlatLoader;
  68.         $this->pdfMergeService $pdfMergeService;
  69.     }
  70.     /**
  71.      * @OA\Post(
  72.      *      path="/catalog/page",
  73.      *      description="Load a catalog",
  74.      *      operationId="readCatalogDetail",
  75.      *      tags={"Store API", "Catalog"},
  76.      * )
  77.      *
  78.      * @Route("/store-api/catalog/page", name="store-api.catalog.page", methods={"POST"})
  79.      */
  80.     public function page(Request $requestSalesChannelContext $context): StoreApiCatalogResponse
  81.     {
  82.         $pageId $request->get('pageId'null);
  83.         if ($pageId === null) {
  84.             throw new NotFoundHttpException();
  85.         }
  86.         $cached $this->cacheManager->getPage($pageId$context);
  87.         if ($cached !== null) {
  88.             return new StoreApiCatalogResponse($cached);
  89.         }
  90.         $criteria = new Criteria([$pageId]);
  91.         $page $this->catalogPageRepository->search($criteria$context->getContext())->first();
  92.         if ($page === null) {
  93.             throw new NotFoundHttpException();
  94.         }
  95.         $this->cacheManager->savePage($page$context);
  96.         return new StoreApiCatalogResponse($page);
  97.     }
  98.     /**
  99.      * @Route("/store-api/catalog/print-merge-prepare/{leftPdfId}/{rightPdfId}", name="store-api.catalog.print-merge-prepare", methods={"GET"})
  100.      */
  101.     public function printMergePrepare(string $leftPdfIdstring $rightPdfIdSalesChannelContext $context): Response
  102.     {
  103.         $media $this->pdfMergeService->getMedia($leftPdfId$rightPdfId$context);
  104.         return new JsonResponse(['success' => true'uuid' => $media->getId()]);
  105.     }
  106.     /**
  107.      * @Route("/store-api/catalog/flat", name="store-api.catalog.flat", methods={"POST"})
  108.      */
  109.     public function flat(Request $requestSalesChannelContext $context): CatalogFlatDataResponse
  110.     {
  111.         $catalogId $request->get('catalogId'null);
  112.         if ($catalogId === null) {
  113.             throw new NotFoundHttpException();
  114.         }
  115.         $struct $this->catalogFlatLoader->load($catalogId$context);
  116.         return new CatalogFlatDataResponse($struct);
  117.     }
  118.     /**
  119.      * @Route("/store-api/catalog/basic", name="store-api.catalog.basic", methods={"POST"})
  120.      */
  121.     public function basic(Request $requestSalesChannelContext $context): Response
  122.     {
  123.         $catalogId $request->get('catalogId'null);
  124.         if ($catalogId === null) {
  125.             throw new NotFoundHttpException();
  126.         }
  127. //        $cache = $this->cacheManager->getBasic($catalogId, $context);
  128. //
  129. //        if ($cache !== null) {
  130. //            return new CatalogBasicDataResponse($cache);
  131. //        }
  132.         $criteria = new Criteria([$catalogId]);
  133.         $criteria->addAssociation('productBoxImageMedia');
  134.         $catalog $this->catalogRepository->search($criteria$context->getContext())->first();
  135.         if ($catalog === null) {
  136.             throw new NotFoundHttpException();
  137.         }
  138.         $this->cacheManager->saveBasic($catalogId$catalog$context);
  139.         return new CatalogBasicDataResponse($catalog);
  140.     }
  141.     /**
  142.      * @Route("/store-api/catalog/tableOfContents", name="store-api.catalog.table-of-contents", methods={"POST"})
  143.      */
  144.     public function tableOfContents(Request $requestSalesChannelContext $context): TableOfContentsResponse
  145.     {
  146.         $catalogId $request->get('catalogId'null);
  147.         if ($catalogId === null) {
  148.             throw new NotFoundHttpException();
  149.         }
  150.         $limit = (int) $request->get('limit'50);
  151.         $offset = (int) $request->get('offset'0);
  152.         $cached $this->cacheManager->getTableOfContents($catalogId$limit$offset);
  153.         if ($cached !== null) {
  154.             return new TableOfContentsResponse($cached);
  155.         }
  156.         $contests $this->contentsLoader->load($catalogId$context$offset$limit);
  157.         $this->cacheManager->setTableOfContents($catalogId$limit$offset$contests);
  158.         return new TableOfContentsResponse($contests);
  159.     }
  160.     /**
  161.      * @Route("/store-api/catalog/bookmark", name="store-api.catalog.bookmark", methods={"POST"})
  162.      */
  163.     public function bookmark(Request $requestSalesChannelContext $context)
  164.     {
  165.         $catalogId $request->get('catalogId'null);
  166.         if ($catalogId === null) {
  167.             throw new NotFoundHttpException();
  168.         }
  169.         $cache $this->cacheManager->getBookmark($catalogId$context);
  170.         if ($cache !== null) {
  171.             return new BookmarksResponse($cache);
  172.         }
  173.         $tree $this->bookmarksTreeService->getTree($catalogId$context->getContext(), $context);
  174.         $this->cacheManager->saveBookmark($catalogId$tree$context);
  175.         return new BookmarksResponse($tree);
  176.     }
  177.     /**
  178.      * @Route("/store-api/catalog/products", name="store-api.catalog.products", methods={"POST"})
  179.      */
  180.     public function products(Request $requestSalesChannelContext $context)
  181.     {
  182.         $pageId $request->get('pageId'null);
  183.         $catalogId $request->get('catalogId'null);
  184.         if ($pageId === null || $catalogId === null) {
  185.             throw new NotFoundHttpException();
  186.         }
  187.         $cache $this->cacheManager->getProduct($pageId$context);
  188.         if ($cache !== null) {
  189.             return new JsonResponse($cache);
  190.         }
  191.         $products $this->catalogPageLoader->getProducts($pageId);
  192.         $this->cacheManager->saveProduct($products$pageId$catalogId$context);
  193.         return new JsonResponse($products);
  194.     }
  195.     /**
  196.      * @Route("/store-api/catalog/page-detail", name="store-api.catalog.page-detail", methods={"POST"})
  197.      */
  198.     public function pageDetail(Request $requestSalesChannelContext $context)
  199.     {
  200.         $pageId $request->get('pageId'null);
  201.         $displayMode = (int) $request->get('displayMode'1);
  202.         if ($pageId === null) {
  203.             throw new NotFoundHttpException();
  204.         }
  205. //        $cache = $this->cacheManager->getPageDetail($pageId, $displayMode, $context);
  206. //        if ($cache !== null) {
  207. //            return new CatalogPageDetailResponse($cache);
  208. //        }
  209.         $page $this->catalogPageLoader->getPageDetail($pageId$context$displayMode);
  210.         $this->cacheManager->savePageDetail($pageId$displayMode$page$context);
  211.         return new CatalogPageDetailResponse($page);
  212.     }
  213.     /**
  214.      * @Route("/store-api/catalog/file", name="store-api.catalog.file", methods={"POST"})
  215.      */
  216.     public function file(Request $requestSalesChannelContext $context//TODO add cache
  217.     {
  218.         $catalogId $request->get('catalogId'null);
  219.         if ($catalogId === null) {
  220.             throw new NotFoundHttpException();
  221.         }
  222.         $criteria = new Criteria([$catalogId]);
  223.         $criteria->addAssociation('pdf');
  224.         /** @var CatalogEntity $catalog */
  225.         $catalog $this->catalogRepository->search($criteria$context->getContext())->first();
  226.         return new CatalogFileResponse($catalog->getPdf());
  227.     }
  228. }