src/V4/Controller/Task/TaskDataPersisterAction.php line 22

Open in your IDE?
  1. <?php
  2. namespace App\V4\Controller\Task;
  3. use ApiPlatform\Core\DataProvider\ItemDataProviderInterface;
  4. use App\DataPersister\CustomerFile\CustomerFileDataPersister;
  5. use App\Model\CustomerFile\CustomerFile;
  6. use App\V4\Controller\AbstractDataPersisterAction;
  7. use App\V4\DataPersister\Task\TaskDataPersister;
  8. use App\V4\Exception\CannotRemindTaskChildQuoteException;
  9. use App\V4\Exception\CannotRemindTaskParentQuoteException;
  10. use App\V4\Logger\SentryLogger;
  11. use App\V4\Model\Quote\Quote;
  12. use App\V4\Model\Task\Task;
  13. use Exception;
  14. use InvalidArgumentException;
  15. use ReflectionException;
  16. use Symfony\Component\HttpFoundation\Request;
  17. use Symfony\Component\HttpFoundation\Response;
  18. use Symfony\Contracts\HttpClient\Exception\TransportExceptionInterface;
  19. class TaskDataPersisterAction extends AbstractDataPersisterAction
  20. {
  21.     /**
  22.      * @var CustomerFileDataPersister
  23.      */
  24.     private $customerFileDataPersister;
  25.     /**
  26.      * @var ItemDataProviderInterface
  27.      */
  28.     private $itemDataProvider;
  29.     /**
  30.      * @var SentryLogger
  31.      */
  32.     private $sentryLogger;
  33.     /**
  34.      * @param TaskDataPersister         $taskDataPersister
  35.      * @param CustomerFileDataPersister $customerFileDataPersister
  36.      * @param ItemDataProviderInterface $itemDataProvider
  37.      * @param SentryLogger              $sentryLogger
  38.      */
  39.     public function __construct(
  40.         TaskDataPersister $taskDataPersister,
  41.         CustomerFileDataPersister $customerFileDataPersister,
  42.         ItemDataProviderInterface $itemDataProvider,
  43.         SentryLogger $sentryLogger
  44.     ) {
  45.         parent::__construct($taskDataPersister);
  46.         $this->customerFileDataPersister $customerFileDataPersister;
  47.         $this->itemDataProvider $itemDataProvider;
  48.         $this->sentryLogger $sentryLogger;
  49.     }
  50.     /**
  51.      * {@inheritdoc}
  52.      *
  53.      * @throws Exception
  54.      */
  55.     public function __invoke(Request $request$data null, ?string $id null)
  56.     {
  57.         if ($request->get('quoteId')) {
  58.             try {
  59.                 $quote $this->itemDataProvider->getItem(Quote::class, $request->get('quoteId'));
  60.                 if (!$quote instanceof Quote) {
  61.                     throw new InvalidArgumentException('Quote not found');
  62.                 }
  63.                 if ($quote->isParent()) {
  64.                     throw new CannotRemindTaskParentQuoteException();
  65.                 }
  66.                 if ($quote->isChild()) {
  67.                     throw new CannotRemindTaskChildQuoteException();
  68.                 }
  69.             } catch (Exception $exception) {
  70.                 $this->sentryLogger->captureException(
  71.                     SentryLogger::CHANNEL_DATA_PERSISTER,
  72.                     $exception,
  73.                     [
  74.                         'catchOnClass' => self::class,
  75.                         'quoteId' => $request->get('quoteId'),
  76.                     ]
  77.                 );
  78.                 return [];
  79.             }
  80.         }
  81.         /** @var Task|Response $taskOrResponse */
  82.         $taskOrResponse parent::__invoke($request$data$id);
  83.         if (null === $taskOrResponse) {
  84.             return [];
  85.         }
  86.         if ($taskOrResponse instanceof Response && !$taskOrResponse->isSuccessful()) {
  87.             return $taskOrResponse;
  88.         }
  89.         $this->handleFiles($request$taskOrResponse);
  90.         return $taskOrResponse;
  91.     }
  92.     /**
  93.      * @param Request $request
  94.      * @param Task    $task
  95.      *
  96.      * @return void
  97.      *
  98.      * @throws Exception
  99.      */
  100.     private function handleFiles(Request $requestTask $task): void
  101.     {
  102.         // Handle files: should delete current files ?
  103.         foreach ($request->request->get('customerFiles', []) as $key => $currentCustomerFile) {
  104.             if ($currentCustomerFile['shouldDelete'] ?? false) {
  105.                 try {
  106.                     $realCustomerFile $task->getCustomerFiles()[$key] ?? null;
  107.                     if (null !== $realCustomerFile && null !== $realCustomerFile->getId()) {
  108.                         $this->customerFileDataPersister->remove($realCustomerFile);
  109.                         $task->removeCustomerFile($realCustomerFile);
  110.                     }
  111.                 } catch (TransportExceptionInterface $e) {
  112.                     $this->sentryLogger->captureException(
  113.                         SentryLogger::CHANNEL_DATA_PERSISTER,
  114.                         $e,
  115.                         [
  116.                             'catchOnClass' => self::class,
  117.                             'taskId' => $task->getId(),
  118.                         ]
  119.                     );
  120.                 }
  121.             }
  122.         }
  123.         // Handle files: should upload new files
  124.         foreach ($request->files->get('customerFiles', []) as $key => $newCustomerFile) {
  125.             $customerFileData = ['taskId' => $task->getId()];
  126.             try {
  127.                 $realCustomerFile $this->customerFileDataPersister->persistCustomerFile($customerFileData$newCustomerFile['customerFile']['file']);
  128.                 $task->addCustomerFile((new CustomerFile())->importFromData($realCustomerFile));
  129.                 $allCF $task->getCustomerFiles();
  130.                 unset($allCF[$key]);
  131.                 $task->setCustomerFiles($allCF);
  132.             } catch (TransportExceptionInterface ReflectionException $e) {
  133.                 $this->sentryLogger->captureException(
  134.                     SentryLogger::CHANNEL_DATA_PERSISTER,
  135.                     $e,
  136.                     [
  137.                         'catchOnClass' => self::class,
  138.                         'taskId' => $task->getId(),
  139.                     ]
  140.                 );
  141.             }
  142.         }
  143.     }
  144. }