<?php
declare(strict_types=1);
namespace EsmConfigurator\Storefront\Controller;
use EsmConfigurator\Core\Content\ResultText\ResultTextEntity;
use Shopware\Core\Framework\DataAbstractionLayer\EntityRepository;
use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\RangeFilter;
use Shopware\Core\Framework\DataAbstractionLayer\Search\Sorting\FieldSorting;
use Shopware\Core\System\SalesChannel\SalesChannelContext;
use Shopware\Storefront\Controller\StorefrontController;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\Request;
use Shopware\Core\Framework\Routing\Annotation\RouteScope;
use Symfony\Component\Routing\Annotation\Route;
/**
* @RouteScope(scopes={"storefront"})
*/
class ConfigurationController extends StorefrontController
{
/**
* @Route("/configuration/text", name="frontend.configuration.text", methods={"GET"})
* @return JsonResponse
*/
public function getResultText(Request $request, SalesChannelContext $context): JsonResponse
{
$score = $request->get('score');
/** @var EntityRepository $resultTextRepository */
$resultTextRepository = $this->container->get('esm_result_text.repository');
$criteria = new Criteria();
$criteria->addFilter(new RangeFilter('score', [RangeFilter::LTE => $score]));
$criteria->addSorting(new FieldSorting('score', FieldSorting::DESCENDING));
$criteria->setLimit(1);
/** @var ResultTextEntity $resultText */
$resultText = $resultTextRepository->search($criteria, $context->getContext())->getEntities()->first();
return new JsonResponse(["resultText" => $resultText]);
}
}