src/Controller/CustomerFile/CustomerFileController.php line 136

Open in your IDE?
  1. <?php
  2. namespace App\Controller\CustomerFile;
  3. use App\DataPersister\CustomerFile\CustomerFileDataPersister;
  4. use App\DataProvider\CustomerFile\CustomerFileItemDataProvider;
  5. use App\Model\CustomerFile\CustomerFile;
  6. use App\Service\CustomerFile\CustomerFileDownloadManager;
  7. use App\V4\Logger\SentryLogger;
  8. use ReflectionException;
  9. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  10. use Symfony\Component\HttpFoundation\BinaryFileResponse;
  11. use Symfony\Component\HttpFoundation\JsonResponse;
  12. use Symfony\Component\HttpFoundation\Request;
  13. use Symfony\Component\HttpFoundation\Response;
  14. use Symfony\Component\HttpFoundation\ResponseHeaderBag;
  15. use Symfony\Component\Routing\Annotation\Route;
  16. use Symfony\Contracts\HttpClient\Exception\ClientExceptionInterface;
  17. use Symfony\Contracts\HttpClient\Exception\DecodingExceptionInterface;
  18. use Symfony\Contracts\HttpClient\Exception\RedirectionExceptionInterface;
  19. use Symfony\Contracts\HttpClient\Exception\ServerExceptionInterface;
  20. use Symfony\Contracts\HttpClient\Exception\TransportExceptionInterface;
  21. use Throwable;
  22. /**
  23.  * Class CustomerFileController.
  24.  */
  25. final class CustomerFileController extends AbstractController
  26. {
  27.     /**
  28.      * CustomerFiles POST Action.
  29.      *
  30.      * @Route("/api/customer_files", name="customer_files_post_route", methods={"POST"})
  31.      *
  32.      * @param Request                   $request
  33.      * @param CustomerFileDataPersister $customerFileDataPersister
  34.      *
  35.      * @return JsonResponse
  36.      *
  37.      * @throws ClientExceptionInterface
  38.      * @throws DecodingExceptionInterface
  39.      * @throws RedirectionExceptionInterface
  40.      * @throws ServerExceptionInterface
  41.      * @throws TransportExceptionInterface
  42.      */
  43.     public function customerFilesPostAction(Request $requestCustomerFileDataPersister $customerFileDataPersister): JsonResponse
  44.     {
  45.         $json json_decode($request->get('request'), true);
  46.         if ($request->files->get('customerFile')) {
  47.             return new JsonResponse($customerFileDataPersister->persistCustomerFile($json$request->files->get('customerFile')));
  48.         }
  49.         if (is_array($request->files->get('customerFiles'))) {
  50.             $fileResponses = [];
  51.             foreach ($request->files->get('customerFiles') as $customerFile) {
  52.                 $fileResponses[] = $customerFileDataPersister->persistCustomerFile($json$customerFile);
  53.             }
  54.             return new JsonResponse($fileResponses);
  55.         }
  56.         return new JsonResponse(['error' => 'No file uploaded'Response::HTTP_INTERNAL_SERVER_ERROR]);
  57.     }
  58.     /**
  59.      * CustomerFiles PUT Action.
  60.      *
  61.      * @Route("/api/customer_files", name="customer_files_put_route", methods={"put"})
  62.      *
  63.      * @param Request                   $request
  64.      * @param CustomerFileDataPersister $customerFileDataPersister
  65.      *
  66.      * @return JsonResponse
  67.      *
  68.      * @throws ClientExceptionInterface
  69.      * @throws DecodingExceptionInterface
  70.      * @throws RedirectionExceptionInterface
  71.      * @throws ServerExceptionInterface
  72.      * @throws TransportExceptionInterface
  73.      */
  74.     public function customerFilesPutAction(Request $requestCustomerFileDataPersister $customerFileDataPersister): JsonResponse
  75.     {
  76.         $json json_decode($request->getContent(), true);
  77.         return new JsonResponse($customerFileDataPersister->persistCustomerFile($json$request->files->get('customerFile')));
  78.     }
  79.     /**
  80.      * CustomerFiles DELETE Action.
  81.      *
  82.      * @Route("/api/customer_files/{id}", name="customer_files_delete_route", methods={"DELETE"})
  83.      *
  84.      * @param string                       $id
  85.      * @param Request                      $request
  86.      * @param CustomerFileDataPersister    $customerFileDataPersister
  87.      * @param CustomerFileItemDataProvider $customerFileItemDataProvider
  88.      *
  89.      * @return JsonResponse
  90.      *
  91.      * @throws ClientExceptionInterface
  92.      * @throws DecodingExceptionInterface
  93.      * @throws RedirectionExceptionInterface
  94.      * @throws ServerExceptionInterface
  95.      * @throws TransportExceptionInterface
  96.      * @throws ReflectionException
  97.      */
  98.     public function customerFilesDeleteAction(
  99.         string $id,
  100.         Request $request,
  101.         CustomerFileDataPersister $customerFileDataPersister,
  102.         CustomerFileItemDataProvider $customerFileItemDataProvider
  103.     ): JsonResponse {
  104.         $customerFile $customerFileItemDataProvider->getItem(CustomerFile::class, $id);
  105.         return new JsonResponse($customerFileDataPersister->remove($customerFile), Response::HTTP_NO_CONTENT);
  106.     }
  107.     /**
  108.      * CustomerFiles DOWNLOAD Action.
  109.      *
  110.      * @Route("/api/files/download/{id}", name="files_download_route", methods={"GET"})
  111.      *
  112.      * @param string                       $id
  113.      * @param CustomerFileDownloadManager  $customerFileDownloadManager
  114.      * @param CustomerFileItemDataProvider $customerFileItemDataProvider
  115.      *
  116.      * @return void
  117.      *
  118.      * @throws ClientExceptionInterface
  119.      * @throws DecodingExceptionInterface
  120.      * @throws RedirectionExceptionInterface
  121.      * @throws ReflectionException
  122.      * @throws ServerExceptionInterface
  123.      * @throws TransportExceptionInterface
  124.      */
  125.     public function customerFilesDownloadPostAction(
  126.         string $id,
  127.         CustomerFileDownloadManager $customerFileDownloadManager,
  128.         CustomerFileItemDataProvider $customerFileItemDataProvider
  129.     ): BinaryFileResponse {
  130.         /** @var CustomerFile $customerFile */
  131.         $customerFile $customerFileItemDataProvider->getItem(CustomerFile::class, $id);
  132.         $label $this->getCustomerFileLabel($customerFile);
  133.         return (new BinaryFileResponse($customerFileDownloadManager->getFile($customerFile)))
  134.             ->setContentDisposition(ResponseHeaderBag::DISPOSITION_ATTACHMENT$label)
  135.             ->deleteFileAfterSend();
  136.     }
  137.     /**
  138.      * CustomerFiles VIEW Action.
  139.      *
  140.      * @Route("/api/files/view/{id}", name="files_view_route", methods={"GET"})
  141.      *
  142.      * @param string                       $id
  143.      * @param CustomerFileDownloadManager  $customerFileDownloadManager
  144.      * @param CustomerFileItemDataProvider $customerFileItemDataProvider
  145.      *
  146.      * @return void
  147.      *
  148.      * @throws ClientExceptionInterface
  149.      * @throws DecodingExceptionInterface
  150.      * @throws RedirectionExceptionInterface
  151.      * @throws ReflectionException
  152.      * @throws ServerExceptionInterface
  153.      * @throws TransportExceptionInterface
  154.      */
  155.     public function customerFilesViewAction(
  156.         string $id,
  157.         CustomerFileDownloadManager $customerFileDownloadManager,
  158.         CustomerFileItemDataProvider $customerFileItemDataProvider
  159.     ): BinaryFileResponse {
  160.         /** @var CustomerFile $customerFile */
  161.         $customerFile $customerFileItemDataProvider->getItem(CustomerFile::class, $id);
  162.         $label $this->getCustomerFileLabel($customerFile);
  163.         return (new BinaryFileResponse($customerFileDownloadManager->getFile($customerFile)))
  164.             ->setContentDisposition(ResponseHeaderBag::DISPOSITION_INLINE$label)
  165.             ->deleteFileAfterSend();
  166.     }
  167.     /**
  168.      * CustomerFiles Show logo Action.
  169.      *
  170.      * @Route("/api/logo/{id}", name="customer_logo_dl", methods={"GET"})
  171.      *
  172.      * @param CustomerFileDownloadManager $customerFileDownloadManager
  173.      * @param string                      $id
  174.      * @param SentryLogger                $sentryLogger
  175.      *
  176.      * @return BinaryFileResponse
  177.      *
  178.      * @throws \Exception
  179.      */
  180.     public function downloadLogoAction(CustomerFileDownloadManager $customerFileDownloadManagerstring $idSentryLogger $sentryLogger): Response
  181.     {
  182.         try {
  183.             $response = new BinaryFileResponse($customerFileDownloadManager->getLogoWithId($id));
  184.             $response
  185.                 ->setContentDisposition(ResponseHeaderBag::DISPOSITION_INLINE'logo.jpg')
  186.                 ->deleteFileAfterSend();
  187.             return $response;
  188.         } catch (Throwable $e) {
  189.             $sentryLogger->captureException(
  190.                 SentryLogger::CHANNEL_CUSTOMER_SERVICE,
  191.                 $e,
  192.                 [
  193.                     'catchOnClass' => self::class,
  194.                     'idLogo' => $id,
  195.                 ]
  196.             );
  197.         }
  198.         return new Response(nullResponse::HTTP_NOT_FOUND);
  199.     }
  200.     private function getCustomerFileLabel(CustomerFile $customerFile): string
  201.     {
  202.         $label $customerFile->getRealFileName();
  203.         if (!empty($customerFile->getFileLabel())) {
  204.             $info pathinfo($customerFile->getFileName());
  205.             $label mb_ereg_replace("([^\w\s\d\-_~,;\[\]\(\).])"''$customerFile->getFileLabel());
  206.             $label mb_ereg_replace("([\.]{2,})"''$label);
  207.             $label = (!empty($info['extension'])) ? $label.'.'.$info['extension'] : $label;
  208.         }
  209.         return $label;
  210.     }
  211. }