src/Controller/Frontend/EpaperController.php line 19

Open in your IDE?
  1. <?php
  2. namespace App\Controller\Frontend;
  3. use App\Application\Abonnent\AbonnentService;
  4. use App\Application\Epaper\EpaperService;
  5. use App\Form\Abo\AbonnentLoginType;
  6. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  7. use Symfony\Component\HttpFoundation\BinaryFileResponse;
  8. use Symfony\Component\HttpFoundation\Request;
  9. use Symfony\Component\HttpFoundation\Response;
  10. use Symfony\Component\HttpKernel\Exception\UnauthorizedHttpException;
  11. use Symfony\Component\Routing\Annotation\Route;
  12. #[Route(path: '/epaper')]
  13. class EpaperController extends AbstractController
  14. {
  15. #[Route(path: '', name: 'fe.epaper')]
  16. public function index(
  17. Request $request,
  18. AbonnentService $abonnentService,
  19. EpaperService $epaperService
  20. ): Response {
  21. $abonnent = $abonnentService->authenticate($request);
  22. $ausgaben = $epaperService->getAllIssuesSinceLastYear();
  23. $epaperService->generateImagesFromPDF($ausgaben);
  24. $ausgabenMitBild = $epaperService->getIssuesWithPicture($ausgaben, 5);
  25. $abonnentLoginForm = $this->createForm(AbonnentLoginType::class, ['http_referer' => $request->getRequestUri()], ['csrf_protection' => false]);
  26. return $this->render('frontend/epaper/index.html.twig', [
  27. 'abonnent' => $abonnent,
  28. 'ausgaben' => $ausgaben,
  29. 'ausgabenMitBild' => $ausgabenMitBild,
  30. 'lastIssue' => $epaperService->getLastIssueValues(),
  31. 'abonnentLoginForm' => $abonnentLoginForm->createView(),
  32. ]);
  33. }
  34. #[Route(path: '/pdf/{filename}', name: 'fe.epaper.pdf')]
  35. public function pdf(
  36. Request $request,
  37. AbonnentService $abonnentService,
  38. string $filename
  39. ): BinaryFileResponse {
  40. // Wenn kein Referer
  41. if ($request->headers->get('referer') === null) {
  42. throw new UnauthorizedHttpException('This route cannot be accessed directly!');
  43. }
  44. // Falls nicht authentifiziert.
  45. if (!$abonnentService->isAuthenticated($request) && !$this->isGranted('MODULE_ARTIKEL')) {
  46. throw new UnauthorizedHttpException('You are not authenticated for this ressource, please login!');
  47. }
  48. $file = $this->getParameter('pdf_file_dir').'/'.$filename;
  49. return $this->file($file);
  50. }
  51. #[Route(path: '/image/{filename}', name: 'fe.epaper.image')]
  52. public function image(
  53. string $filename
  54. ): BinaryFileResponse {
  55. $file = $this->getParameter('pdf_images_file_dir').'/'.$filename;
  56. return $this->file($file);
  57. }
  58. }