vendor/crehler/mojebambino-catalog/src/Controller/StoreApi/CatalogPageRouteController.php line 54

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\CatalogPageResponse;
  15. use Crehler\Catalog\Entity\Catalog\Aggregate\CatalogPage\CatalogPageEntity;
  16. use Crehler\Catalog\Service\CatalogReader\Loader\CatalogPageLoader;
  17. use Shopware\Core\Framework\DataAbstractionLayer\EntityRepositoryInterface;
  18. use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
  19. use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\ContainsFilter;
  20. use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\EqualsFilter;
  21. use Shopware\Core\Framework\DataAbstractionLayer\Search\Sorting\FieldSorting;
  22. use Shopware\Core\Framework\Plugin\Exception\DecorationPatternException;
  23. use Shopware\Core\Framework\Routing\Annotation\RouteScope;
  24. use Shopware\Core\System\SalesChannel\SalesChannelContext;
  25. use Symfony\Component\HttpFoundation\JsonResponse;
  26. use Symfony\Component\HttpFoundation\Request;
  27. use Symfony\Component\Routing\Annotation\Route;
  28. use function Symfony\Component\String\u;
  29. /**
  30.  * @RouteScope(scopes={"store-api"})
  31.  */
  32. class CatalogPageRouteController extends AbstractCatalogPageRoute
  33. {
  34.     private CatalogPageLoader $catalogPageLoader;
  35.     private EntityRepositoryInterface $catalogPageRepository;
  36.     public function __construct(CatalogPageLoader $catalogPageLoaderEntityRepositoryInterface $catalogPageRepository)
  37.     {
  38.         $this->catalogPageLoader $catalogPageLoader;
  39.         $this->catalogPageRepository $catalogPageRepository;
  40.     }
  41.     public function getDecorated(): AbstractCatalogPageRoute
  42.     {
  43.         throw new DecorationPatternException(self::class);
  44.     }
  45.     /**
  46.      * @Route("/store-api/catalog/detail", name="store-api.catalog.detail", methods={"POST"})
  47.      */
  48.     public function load(Request $requestSalesChannelContext $context): CatalogPageResponse
  49.     {
  50.         $struct $this->catalogPageLoader->load($request$context);
  51.         return new CatalogPageResponse($struct);
  52.     }
  53.     /**
  54.      * @Route("/store-api/catalog/search", name="store-api.catalog.search", methods={"POST"})
  55.      */
  56.     public function search(Request $requestSalesChannelContext $context): JsonResponse
  57.     {
  58.         $catalogId $request->get('catalogId');
  59.         $search $request->get('search');
  60.         $criteria = new Criteria();
  61.         $criteria->addFilter(new ContainsFilter('content'$search));
  62.         $criteria->addFilter(new EqualsFilter('catalogId'$catalogId));
  63.         $criteria->setLimit(100);
  64.         $criteria->addSorting(new FieldSorting('pageNumber'FieldSorting::ASCENDING));
  65.         $searchResult $this->catalogPageRepository->search($criteria$context->getContext());
  66.         $result = [
  67.         ];
  68.         /** @var CatalogPageEntity $element */
  69.         foreach ($searchResult->getElements() as $element) {
  70.             $result[] = [
  71.                 'id' => $element->getId(),
  72.                 'content' => u($element->getContent())->replaceMatches('/[^\w\s\%]++/'' ')->collapseWhitespace()->toString(),
  73.                 'pageDisplayNumber' => $element->getPageDisplayNumber(),
  74.                 'pageNumber' => $element->getPageNumber(),
  75.             ];
  76.         }
  77.         return new JsonResponse($result);
  78.     }
  79. }