src/Controller/Front/Download/Manual/ManualController.php line 29

Open in your IDE?
  1. <?php
  2. declare(strict_types=1);
  3. namespace App\Controller\Front\Download\Manual;
  4. use App\Application\Service\Storage\DownloadService;
  5. use App\Service\Download\ManualDownloadService;
  6. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  7. use Symfony\Component\HttpFoundation\Response;
  8. use Symfony\Component\Routing\Annotation\Route;
  9. class ManualController extends AbstractController
  10. {
  11. private DownloadService $downloadService;
  12. private ManualDownloadService $manualDownloadService;
  13. public function __construct(
  14. DownloadService $downloadService,
  15. ManualDownloadService $manualDownloadService
  16. ) {
  17. $this->downloadService = $downloadService;
  18. $this->manualDownloadService = $manualDownloadService;
  19. }
  20. /**
  21. * @Route("/manual/course/{downloadId}", name="download_course_manual", methods={"GET"})
  22. */
  23. public function downloadCourseManual(int $downloadId): ?Response
  24. {
  25. if (!$this->manualDownloadService->validateCourseProduct($downloadId)) {
  26. return new Response('', Response::HTTP_CONFLICT, ['Content-Disposition' => 'attachment']);
  27. }
  28. try {
  29. $productManual = $this->downloadService->downloadProductManual($downloadId);
  30. } catch (\Throwable $t) {
  31. $productManual = null;
  32. }
  33. if (!$productManual || empty($productManual->content)) {
  34. return new Response('', Response::HTTP_CONFLICT, ['Content-Disposition' => 'attachment']);
  35. }
  36. $response = new Response();
  37. $response->setContent($productManual->content);
  38. $response->headers->set('Content-Type', $productManual->mimeType);
  39. $response->headers->set('Content-Disposition', 'attachment; filename="'.$productManual->fileName.'"');
  40. return $response;
  41. }
  42. }