src/EventSubscriber/Api/EstimateOrderSubscriber.php line 58

Open in your IDE?
  1. <?php
  2. // api/src/EventSubscriber/BookMailSubscriber.php
  3. namespace App\EventSubscriber\Api;
  4. use ApiPlatform\Core\EventListener\EventPriorities;
  5. use App\Entity\Legacy\Estimate;
  6. use App\Messaging\MessagingService;
  7. use Doctrine\ORM\EntityManagerInterface;
  8. use Spatie\Browsershot\Browsershot;
  9. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  10. use Symfony\Component\HttpKernel\Event\ViewEvent;
  11. use Symfony\Component\HttpKernel\KernelEvents;
  12. use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
  13. final class EstimateOrderSubscriber implements EventSubscriberInterface
  14. {
  15. /**
  16. * @var MessagingService
  17. */
  18. private $messagingService;
  19. /**
  20. * @var string
  21. */
  22. private $cacheDir;
  23. /**
  24. * @var TokenStorageInterface
  25. */
  26. private $tokenStorage;
  27. /**
  28. * @var EntityManagerInterface
  29. */
  30. private $entityManager;
  31. public function __construct(
  32. MessagingService $messagingService,
  33. $cacheDir,
  34. TokenStorageInterface $tokenStorage,
  35. EntityManagerInterface $entityManager
  36. ) {
  37. $this->messagingService = $messagingService;
  38. $this->cacheDir = $cacheDir;
  39. $this->tokenStorage = $tokenStorage;
  40. $this->entityManager = $entityManager;
  41. }
  42. public static function getSubscribedEvents()
  43. {
  44. return [
  45. KernelEvents::VIEW => ['validate', EventPriorities::POST_VALIDATE],
  46. ];
  47. }
  48. public function validate(\Symfony\Component\HttpKernel\Event\ViewEvent $event)
  49. {
  50. if ($event->getRequest()->attributes->get('_route') === 'api_estimates_order_item') {
  51. /** @var Estimate $estimate */
  52. $estimate = $event->getControllerResult();
  53. $request = $event->getRequest();
  54. $fileName = $request->request->get('filename', date('d.m.Y') . '.pdf');
  55. // sanitize file name for internet explorer string stuff
  56. $fileName = preg_replace('/[^a-zA-Z0-9\-\._]/', '', $fileName);
  57. $directory = bin2hex(random_bytes(24));
  58. $filePath = $this->cacheDir . '/rendered-pdf/' . $directory . '/';
  59. if (!file_exists($filePath)) {
  60. mkdir($filePath, 0775, true);
  61. }
  62. Browsershot::html($request->request->get('body'))
  63. ->ignoreHttpsErrors()
  64. ->showBrowserHeaderAndFooter()
  65. ->hideHeader()
  66. ->footerHtml(
  67. "
  68. <style>
  69. html, body {
  70. font-size: 10px;
  71. font-family: Helvetica;
  72. position: relative;
  73. color: #2a2a2a;
  74. }
  75. .fileName {
  76. position: absolute;
  77. left: 30px;
  78. }
  79. .meta {
  80. position: absolute;
  81. right: 30px;
  82. }
  83. </style>
  84. <div class='fileName'>$fileName</div>
  85. <div class='meta'>
  86. <span class='pageNumber'></span>
  87. <span>/</span>
  88. <span class='totalPages'></span>
  89. </div>
  90. "
  91. )
  92. ->noSandbox()
  93. ->showBackground()
  94. ->margins(10, 10, 15, 10)
  95. ->format('A4')
  96. ->savePdf($filePath . $fileName);
  97. $user = $this->tokenStorage->getToken()->getUser();
  98. $estimate->setOrderedAt(new \DateTime());
  99. $this->entityManager->persist($estimate);
  100. $this->entityManager->flush();
  101. $this->messagingService->sendOrderConfirmationMessage(
  102. $user,
  103. $estimate,
  104. $filePath . $fileName,
  105. $request->getLocale()
  106. );
  107. }
  108. }
  109. }