<?php
namespace App\V4\Action\Quote;
use ApiPlatform\Core\DataProvider\ItemDataProviderInterface;
use ApiPlatform\Core\Exception\ResourceClassNotSupportedException;
use App\V4\Exception\CannotDuplicateChildQuoteException;
use App\V4\Exception\CannotDuplicateParentQuoteException;
use App\V4\Logger\SentryLogger;
use App\V4\Model\Quote\Quote;
use App\V4\Service\Quote\QuoteDuplicateManager;
use App\V4\Service\Quote\QuoteFormHandler;
use App\V4\Voters\QuoteVoter;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Component\Security\Core\Security;
use Symfony\Component\Serializer\Exception\ExceptionInterface;
use Throwable;
/**
* @Route("/api/V4/quotes/{quoteId}/form/duplicate", methods={"GET"}, name="v4_quotes_form_duplicate")
*/
class DuplicateQuoteAction extends AbstractController
{
/**
* @var ItemDataProviderInterface
*/
private $itemDataProvider;
/**
* @var QuoteFormHandler
*/
private $quoteFormHandler;
/**
* @var SentryLogger
*/
private $sentryLogger;
public function __construct(
ItemDataProviderInterface $itemDataProvider,
QuoteFormHandler $quoteFormHandler,
SentryLogger $sentryLogger
) {
$this->itemDataProvider = $itemDataProvider;
$this->quoteFormHandler = $quoteFormHandler;
$this->sentryLogger = $sentryLogger;
}
/**
* @throws CannotDuplicateChildQuoteException
* @throws CannotDuplicateParentQuoteException
* @throws ExceptionInterface
* @throws ResourceClassNotSupportedException
* @throws Throwable
*/
public function __invoke(string $quoteId, Request $request, Security $security, QuoteDuplicateManager $quoteDuplicateManager): JsonResponse
{
$this->denyAccessUnlessGranted(QuoteVoter::QUOTE_DUPLICATE);
$prospectId = $request->get('prospectId');
try {
/** @var Quote $quote */
$quote = $this->itemDataProvider->getItem(Quote::class, $quoteId);
if (!$quote instanceof Quote) {
throw new NotFoundHttpException();
}
if ($quote->isParent()) {
throw new CannotDuplicateParentQuoteException();
}
if ($quote->isChild()) {
throw new CannotDuplicateChildQuoteException();
}
} catch (Throwable $exception) {
$this->sentryLogger->captureException(
SentryLogger::CHANNEL_FORM_CONTROL,
$exception,
[
'catchOnClass' => self::class,
'method' => Request::METHOD_GET,
'quoteId' => $quoteId,
]
);
throw $exception;
}
$duplicate = $quoteDuplicateManager->duplicate($quote, $prospectId);
return $this->quoteFormHandler->getQuoteFormFromModel(
$duplicate,
$request->get('prospectId'),
$request->query->get('tabId'),
(bool) $request->query->get('isLegacy', false)
);
}
}