<?php
declare(strict_types=1);
/**
* @copyright 2020 Crehler Sp. z o. o.
* @link https://crehler.com/
* @support support@crehler.com
*
* @author Mateusz FlasiĆski
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Crehler\Catalog\Controller\StoreApi;
use Crehler\Catalog\Content\CatalogPageResponse;
use Crehler\Catalog\Entity\Catalog\Aggregate\CatalogPage\CatalogPageEntity;
use Crehler\Catalog\Service\CatalogReader\Loader\CatalogPageLoader;
use Shopware\Core\Framework\DataAbstractionLayer\EntityRepositoryInterface;
use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\ContainsFilter;
use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\EqualsFilter;
use Shopware\Core\Framework\DataAbstractionLayer\Search\Sorting\FieldSorting;
use Shopware\Core\Framework\Plugin\Exception\DecorationPatternException;
use Shopware\Core\Framework\Routing\Annotation\RouteScope;
use Shopware\Core\System\SalesChannel\SalesChannelContext;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Routing\Annotation\Route;
use function Symfony\Component\String\u;
/**
* @RouteScope(scopes={"store-api"})
*/
class CatalogPageRouteController extends AbstractCatalogPageRoute
{
private CatalogPageLoader $catalogPageLoader;
private EntityRepositoryInterface $catalogPageRepository;
public function __construct(CatalogPageLoader $catalogPageLoader, EntityRepositoryInterface $catalogPageRepository)
{
$this->catalogPageLoader = $catalogPageLoader;
$this->catalogPageRepository = $catalogPageRepository;
}
public function getDecorated(): AbstractCatalogPageRoute
{
throw new DecorationPatternException(self::class);
}
/**
* @Route("/store-api/catalog/detail", name="store-api.catalog.detail", methods={"POST"})
*/
public function load(Request $request, SalesChannelContext $context): CatalogPageResponse
{
$struct = $this->catalogPageLoader->load($request, $context);
return new CatalogPageResponse($struct);
}
/**
* @Route("/store-api/catalog/search", name="store-api.catalog.search", methods={"POST"})
*/
public function search(Request $request, SalesChannelContext $context): JsonResponse
{
$catalogId = $request->get('catalogId');
$search = $request->get('search');
$criteria = new Criteria();
$criteria->addFilter(new ContainsFilter('content', $search));
$criteria->addFilter(new EqualsFilter('catalogId', $catalogId));
$criteria->setLimit(100);
$criteria->addSorting(new FieldSorting('pageNumber', FieldSorting::ASCENDING));
$searchResult = $this->catalogPageRepository->search($criteria, $context->getContext());
$result = [
];
/** @var CatalogPageEntity $element */
foreach ($searchResult->getElements() as $element) {
$result[] = [
'id' => $element->getId(),
'content' => u($element->getContent())->replaceMatches('/[^\w\s\%]++/', ' ')->collapseWhitespace()->toString(),
'pageDisplayNumber' => $element->getPageDisplayNumber(),
'pageNumber' => $element->getPageNumber(),
];
}
return new JsonResponse($result);
}
}