src/V4/Action/Quote/DuplicateQuoteAction.php line 26

Open in your IDE?
  1. <?php
  2. namespace App\V4\Action\Quote;
  3. use ApiPlatform\Core\DataProvider\ItemDataProviderInterface;
  4. use ApiPlatform\Core\Exception\ResourceClassNotSupportedException;
  5. use App\V4\Exception\CannotDuplicateChildQuoteException;
  6. use App\V4\Exception\CannotDuplicateParentQuoteException;
  7. use App\V4\Logger\SentryLogger;
  8. use App\V4\Model\Quote\Quote;
  9. use App\V4\Service\Quote\QuoteDuplicateManager;
  10. use App\V4\Service\Quote\QuoteFormHandler;
  11. use App\V4\Voters\QuoteVoter;
  12. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  13. use Symfony\Component\HttpFoundation\JsonResponse;
  14. use Symfony\Component\HttpFoundation\Request;
  15. use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
  16. use Symfony\Component\Routing\Annotation\Route;
  17. use Symfony\Component\Security\Core\Security;
  18. use Symfony\Component\Serializer\Exception\ExceptionInterface;
  19. use Throwable;
  20. /**
  21.  * @Route("/api/V4/quotes/{quoteId}/form/duplicate", methods={"GET"}, name="v4_quotes_form_duplicate")
  22.  */
  23. class DuplicateQuoteAction extends AbstractController
  24. {
  25.     /**
  26.      * @var ItemDataProviderInterface
  27.      */
  28.     private $itemDataProvider;
  29.     /**
  30.      * @var QuoteFormHandler
  31.      */
  32.     private $quoteFormHandler;
  33.     /**
  34.      * @var SentryLogger
  35.      */
  36.     private $sentryLogger;
  37.     public function __construct(
  38.         ItemDataProviderInterface $itemDataProvider,
  39.         QuoteFormHandler $quoteFormHandler,
  40.         SentryLogger $sentryLogger
  41.     ) {
  42.         $this->itemDataProvider $itemDataProvider;
  43.         $this->quoteFormHandler $quoteFormHandler;
  44.         $this->sentryLogger $sentryLogger;
  45.     }
  46.     /**
  47.      * @throws CannotDuplicateChildQuoteException
  48.      * @throws CannotDuplicateParentQuoteException
  49.      * @throws ExceptionInterface
  50.      * @throws ResourceClassNotSupportedException
  51.      * @throws Throwable
  52.      */
  53.     public function __invoke(string $quoteIdRequest $requestSecurity $securityQuoteDuplicateManager $quoteDuplicateManager): JsonResponse
  54.     {
  55.         $this->denyAccessUnlessGranted(QuoteVoter::QUOTE_DUPLICATE);
  56.         $prospectId $request->get('prospectId');
  57.         try {
  58.             /** @var Quote $quote */
  59.             $quote $this->itemDataProvider->getItem(Quote::class, $quoteId);
  60.             if (!$quote instanceof Quote) {
  61.                 throw new NotFoundHttpException();
  62.             }
  63.             if ($quote->isParent()) {
  64.                 throw new CannotDuplicateParentQuoteException();
  65.             }
  66.             if ($quote->isChild()) {
  67.                 throw new CannotDuplicateChildQuoteException();
  68.             }
  69.         } catch (Throwable $exception) {
  70.             $this->sentryLogger->captureException(
  71.                 SentryLogger::CHANNEL_FORM_CONTROL,
  72.                 $exception,
  73.                 [
  74.                     'catchOnClass' => self::class,
  75.                     'method' => Request::METHOD_GET,
  76.                     'quoteId' => $quoteId,
  77.                 ]
  78.             );
  79.             throw $exception;
  80.         }
  81.         $duplicate $quoteDuplicateManager->duplicate($quote$prospectId);
  82.         return $this->quoteFormHandler->getQuoteFormFromModel(
  83.             $duplicate,
  84.             $request->get('prospectId'),
  85.             $request->query->get('tabId'),
  86.             (bool) $request->query->get('isLegacy'false)
  87.         );
  88.     }
  89. }