<?php
declare(strict_types=1);
namespace App\Controller\Product;
use App\DataProvider\Product\ProductCollectionDataProvider;
use App\DataProvider\RequestDataProviderTrait;
use App\Model\Product\Product;
use App\Service\Cache\Exception\UnableToSaveKeyException;
use App\Service\Form\FormUtils;
use App\Service\Product\ProductFormHandler;
use App\Voters\ProductVoter;
use Psr\Cache\CacheException;
use Psr\Cache\InvalidArgumentException;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Serializer\Exception\ExceptionInterface;
use Symfony\Component\Serializer\Serializer;
use Symfony\Component\Serializer\SerializerInterface;
use Symfony\Contracts\HttpClient\Exception\ClientExceptionInterface;
use Symfony\Contracts\HttpClient\Exception\DecodingExceptionInterface;
use Symfony\Contracts\HttpClient\Exception\RedirectionExceptionInterface;
use Symfony\Contracts\HttpClient\Exception\ServerExceptionInterface;
use Symfony\Contracts\HttpClient\Exception\TransportExceptionInterface;
class PostProductSearchFormAction extends AbstractController
{
use FormUtils;
use RequestDataProviderTrait;
/**
* @var ProductFormHandler
*/
private $productFormHandler;
/**
* @var ProductCollectionDataProvider
*/
private $productCollectionDataProvider;
/**
* @var Serializer
*/
private $serializer;
/**
* @param ProductFormHandler $productFormHandler
* @param ProductCollectionDataProvider $productCollectionDataProvider
* @param Serializer $serializer
*/
public function __construct(
ProductFormHandler $productFormHandler,
ProductCollectionDataProvider $productCollectionDataProvider,
SerializerInterface $serializer
) {
$this->productFormHandler = $productFormHandler;
$this->productCollectionDataProvider = $productCollectionDataProvider;
$this->serializer = $serializer;
}
/**
* @param Request $request
*
* @return JsonResponse|iterable
*
* @throws ClientExceptionInterface
* @throws DecodingExceptionInterface
* @throws RedirectionExceptionInterface
* @throws ServerExceptionInterface
* @throws TransportExceptionInterface
* @throws UnableToSaveKeyException
* @throws CacheException
* @throws InvalidArgumentException
* @throws ExceptionInterface
*/
public function __invoke(Request $request)
{
$this->denyAccessUnlessGranted(ProductVoter::PRODUCT_SHOW_LIST);
$productSearchForm = $this->productFormHandler->handleProductSearchForm($request->getContent());
if (!$productSearchForm->isSubmitted() || !$productSearchForm->isValid()) {
return new JsonResponse([
'code' => Response::HTTP_BAD_REQUEST,
'data' => $this->convertFormErrorsToArray($productSearchForm),
'errorMessage' => 'product_form_search',
], Response::HTTP_BAD_REQUEST);
}
$searchBody = $this->completeSearchBodyFromRequest(
$request,
json_decode($this->serializer->serialize($productSearchForm->getData(), 'json'), true)
);
$transformResponse = !$request->query->get('disableTransformation', false);
$response = $this
->productCollectionDataProvider
->getCollection(Product::class, null, [
'filters' => $searchBody,
'transformResponse' => $transformResponse,
])
;
if ($transformResponse) {
return new JsonResponse($response);
}
return $response;
}
}